利用关机的消息是:WM_QUERYENDSESSION WM_QUERYENDSESSIONnSource = (UINT) wParam; // source of end-session requestfLogOff = lParam // logoff flag ParametersnSourceReserved for future use.fLogOffValue of lParam. Indicates whether the user is logging off or shutting down the system. Supported values include: ENDSESSION_LOGOFF.Return ValuesIf an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. 当执行exitwindows函数时它会发送一个WM_QUERYENDSESSION消息给所有正在运行的程序。
用如下: 直接调用 ShutDownWin;//就可 function GetWinVer: boolean; var Getver: OSVERSIONINFO; RET: LONGBOOL; begin Result := true; Getver.dwOSVersionInfoSize := 148; RET := GETVERSIONEX(Getver); if Getver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then Result := False; // if Getver.dwPlatformId=VER_PLATFORM_WIN32_NT then end;procedure AdjustToken(); var hdlProcessHandle: Cardinal; hdlTokenHandle: Cardinal; tmpLuid: Int64; tkpPrivilegeCount: Int64; tkp: TOKEN_PRIVILEGES; tkpNewButIgnored: TOKEN_PRIVILEGES; lBufferNeeded: Cardinal; Privilege: array[0..0] of _LUID_AND_ATTRIBUTES; begin hdlProcessHandle := GetCurrentProcess; OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY), hdlTokenHandle); // Get the LUID for shutdown privilege. LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid); Privilege[0].Luid := tmpLuid; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED; tkp.PrivilegeCount := 1; // One privilege to set tkp.Privileges[0] := Privilege[0]; // Enable the shutdown privilege in the access token of this // process. AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Sizeof(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded); end;function ShutDownWin: boolean; { const EWX_FORCE = 4; //关闭所有程序并以其他用户身份登录 EWX_LOGOFF = 0; //重新启动计算机并切换到MS-DOS方式 EWX_REBOOT = 2; //重新启动计算机 EWX_SHUTDOWN = 1; //关闭计算机 } begin if GetWinVer then AdjustToken; Result := ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF, 0); end;
WM_QUERYENDSESSIONnSource = (UINT) wParam; // source of end-session requestfLogOff = lParam // logoff flag ParametersnSourceReserved for future use.fLogOffValue of lParam. Indicates whether the user is logging off or shutting down the system. Supported values include: ENDSESSION_LOGOFF.Return ValuesIf an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. 当执行exitwindows函数时它会发送一个WM_QUERYENDSESSION消息给所有正在运行的程序。
不行呀,大哥,快救我呀,谢谢
ShutDownWin;//就可
function GetWinVer: boolean;
var
Getver: OSVERSIONINFO;
RET: LONGBOOL;
begin
Result := true;
Getver.dwOSVersionInfoSize := 148;
RET := GETVERSIONEX(Getver);
if Getver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
Result := False;
// if Getver.dwPlatformId=VER_PLATFORM_WIN32_NT then
end;procedure AdjustToken();
var
hdlProcessHandle: Cardinal;
hdlTokenHandle: Cardinal;
tmpLuid: Int64;
tkpPrivilegeCount: Int64;
tkp: TOKEN_PRIVILEGES;
tkpNewButIgnored: TOKEN_PRIVILEGES;
lBufferNeeded: Cardinal;
Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),
hdlTokenHandle);
// Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
// Enable the shutdown privilege in the access token of this
// process.
AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Sizeof(tkpNewButIgnored),
tkpNewButIgnored, lBufferNeeded);
end;function ShutDownWin: boolean;
{
const
EWX_FORCE = 4; //关闭所有程序并以其他用户身份登录
EWX_LOGOFF = 0; //重新启动计算机并切换到MS-DOS方式
EWX_REBOOT = 2; //重新启动计算机
EWX_SHUTDOWN = 1; //关闭计算机
}
begin
if GetWinVer then
AdjustToken;
Result := ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF, 0);
end;