首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

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

2012-03-05 
CONSOLE程序中如何控制计算机关机?usesSysUtils,ShellAPI//ExitWindowsEx(EWX_SHUTDOWN,0)//关机ShellEx

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);

热点排行