读书人

CONSOLE程序中怎么控制计算机关机

发布时间: 2012-03-05 11:54:03 作者: rapoo

CONSOLE程序中如何控制计算机关机?
uses
SysUtils,ShellAPI;

//ExitWindowsEx(EWX_SHUTDOWN,0);//关机
ShellExecute(0, 'open ', 'shutdown.exe ', ' -f -s -t 0 ',nil,SW_HIDE);

上述两个都不行,help

[解决办法]
procedure ExitWindowsNT(uFlags: integer);
var
hToken: THANDLE;
tkp, tkDumb: TTokenPrivileges;
DumbInt: DWORD;
begin
FillChar(tkp, sizeof(tkp), 0);
if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, hToken)) then
raise Exception.create( 'OpenProcessToken failed with code '
+ inttostr(GetLastError));

LookupPrivilegeValue(nil, pchar( 'SeShutdownPrivilege '),
tkp.Privileges[0].Luid);

tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

if GetLastError <> ERROR_SUCCESS then
raise Exception.create( 'AdjustTokenPrivileges failed with code '
+ inttostr(GetLastError));

if not ExitWindowsEx(uFlags, 0) then
raise Exception.create( 'ExitWindowsEx failed with code '
+ inttostr(GetLastError));
end;


调用
ExitWindowsNT(EWX_FORCE or EWX_POWEROFF);

读书人网 >.NET

热点推荐