用C++Builder写的一个关闭机器的函数!
<P>bool closecomputer(int kind){
HANDLE hToken;
TOKEN_PRIVILEGES tkp; </P>
<P> // Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
return false;
}</P>
<P> // Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges.Luid); </P>
<P> tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;</P>
<P> // Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);</P>
<P> if (GetLastError() != ERROR_SUCCESS)
{
return false;
}
switch(kind)
{case 1: ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0); break; //重启
case 2: ExitWindowsEx(EWX_FORCE|EWX_SHUTDOWN|EWX_POWEROFF,0); break; //关机
case 3: ExitWindowsEx(EWX_FORCE|EWX_LOGOFF,0); break; //注销
case 4: SetSystemPowerState(false, true); break; //休眠
case 5: SetSystemPowerState(true, true); break; //待机
default: break;
}
return true;</P>
<P>}</P>
页:
[1]