我在win2000 pro 下用delphi 想编一个简单的关机程序,但是用
ExitWindowsEx(Ewx_poweroff,0) 关不了机??? 然后我又试了
ExitWindowsEx(Ewx_shutdown,0) 也不起作用,就连 ExitWindowsEx(Ewx_reboot,0) 也没有反映。 为什么?
ExitWindowsEx(Ewx_poweroff,0) 关不了机??? 然后我又试了
ExitWindowsEx(Ewx_shutdown,0) 也不起作用,就连 ExitWindowsEx(Ewx_reboot,0) 也没有反映。 为什么?
function czsyspd(shutlx:integer):boolean;
const
SE_SHUTDOWN_NAME='SeShutdownPrivilege';
var
OSInfo:OSVersionInfo;
hToken:THandle;
NewState:TTokenPrivileges;
PreviousState:TTokenPrivileges;
ReturnLength:DWord;
begin
result:=true;
OSInfo.dwOSVersionInfoSize:=Sizeof(OSVersionInfo);
GetVersionEx(OSInfo); //获取操作系统信息
if OSInfo.dwPlatformId=VER_PLATFORM_WIN32_NT then //如果为NT或2000系统,授予用户权限
begin
ReturnLength:=0;
if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken) then
begin
MessageBox(0,'Exit Error','OpenProcessToken() Failed',MB_OK);
Exit;
end;
if not LookupPrivilegeValue(nil,SE_SHUTDOWN_NAME,NewState.Privileges[0].Luid) then
begin
MessageBox(0,'Exit Error','LookupPrivilegeValue() Failed',MB_OK);
Exit;
end;
NewState.PrivilegeCount:=1;
NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,False,NewState,SizeOf(TTokenPrivileges),PreviousState,ReturnLength);
if Boolean( GetLastError() ) then
begin
MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );
Exit;
end;
end;
case shutlx of
0:ExitWindowsEx(EWX_LOGOFF,0);
1:ExitWindowsEx(EWX_POWEROFF,0);
2:ExitWindowsEx(EWX_REBOOT,0);
3:ExitWindowsEx(EWX_SHUTDOWN,0);
4:ExitWindowsEx(EWX_FORCE,0);
end;
end;
>>顺便问一下 EWX_FORCE 参数实现什么功能
強制關機, 這樣幾乎肯定能關機, 但可能有些程序的數據還沒保存!!