如何用ExitWindowsEX(EWX_SHUTDOWN,0)退出Windwos NT 或 Windows 2000(100分)

  • 主题发起人 主题发起人 xself
  • 开始时间 开始时间
X

xself

Unregistered / Unconfirmed
GUEST, unregistred user!
在Windows Nt 或 Windows 2000 中如何用 ExitWindowsEX(EWX_SHUTDOWN,0) 关闭系统,
Windows API 介绍说要将 SE_SHUTDOWN_NAME 特权打开,请问如何打开?
 
HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
error("OpenProcessToken");

// Get the LUID for the shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
error("AdjustTokenPrivileges");

// Shut down the system and force all applications to close.

if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
error("ExitWindowsEx");
 
来自:温柔一刀 时间:00-10-13 4:08:10 ID:364448
这是NT下关机的程序,其中需要调用AdjustTokenPrivileges,
是一个很好的例子:

procedure ExitWindowsNT(uFlags : integer);
var
hToken : THANDLE;
tkp, tkDumb : TTokenPrivileges;
DumbInt : DWORD;
begin
FillChar(tkp, sizeof(tkp), 0);
// Get a token for this process
if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, hToken)) then
raise Exception.create('OpenProcessToken failed with code '
+ inttostr(GetLastError));

// Get the LUID for the Shutdown privilege
LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'),
tkp.Privileges[0].Luid);

tkp.PrivilegeCount := 1; // one privilege to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

// Get the shutdown provolege for this process
AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

// Cannot test the return value of AdjustTokenPrivileges
if GetLastError <> ERROR_SUCCESS then
Raise Exception.create('AdjustTokenPrivileges failed with code '
+ inttostr(GetLastError));

// shut down the system and for all applications to close
if not ExitWindowsEx(uFlags, 0) then
Raise Exception.create('ExitWindowsEx failed with code '
+ inttostr(GetLastError));
end;

 
多人接受答案了。
 
后退
顶部