Private Sub AdjustToken()
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
hdlProcessHandle = GetCurrentProcess()
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
' Get the LUID for shutdown privilege.
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.Privileges(0).pLuid = tmpLuid
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
' Enable the shutdown privilege in the access token of this process.
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
End Sub
DWORD dwVersion = GetVersion();// Get major and minor version numbers of Windows
if (dwVersion < 0x80000000) // Windows NT
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
}
else
ExitWindowsEx(EWX_FORCE+EWX_REBOOT,0);
}
else if(temp.SubString(1,8)=="shutdown")
{
ExitWindowsEx(EWX_SHUTDOWN,0);
代码如下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ExitWindowsEx(EWX_SHUTDOWN, 0);
}
参数:
EWX_FORCE Forces programs to close without sending WM_QUERYENDSESSION
and WM_ENDSESSION messages to notify them that Windows is
shutting down. With any of the other flags, programs have the
power to abort the shut down.
EWX_LOGOFF Closes all programs and then logs the user off.
EWX_POWEROFF Powers down the computer if it supports it.
EWX_REBOOT Reboots the machine.
EWX_SHUTDOWN Shuts the machine off without restarting it.