Shutting DownYou can use the ExitWindowsEx function to shut down the system. Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer. The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.BOOL MySystemShutdown() { HANDLE hToken; TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return( FALSE );
if (GetLastError() != ERROR_SUCCESS) return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) return FALSE; return TRUE; } For more information about setting security privileges, see Privileges.
System Shutdown FunctionsThe following functions are used with system shutdown.Function Description AbortSystemShutdown Stops a system shutdown started by using the InitiateSystemShutdown function. ExitWindows Logs off the current user. ExitWindowsEx Either logs off the current user, shuts down the system, or shuts down and restarts the system. InitiateSystemShutdown Initiates a shutdown and optional restart of the specified computer. InitiateSystemShutdownEx Initiates a shutdown and optional restart of the specified computer. LockWorkStation Locks the workstation's display, protecting it from unauthorized use.
{
// TODO: Add extra validation here
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
DWORD dwVersion;
dwVersion = GetVersion(); //获得Windows NT或Win32的版本号 switch(IsHow)
{
case 0://注销
ExitWindowsEx(EWX_LOGOFF,0);
break; case 1://重新启动
if (dwVersion < 0x80000000)
{// Windows NT系列
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // 设置一个权限
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0); ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
}
else
{//Windows 95系列
ExitWindowsEx(EWX_FORCE | EWX_REBOOT,0);
}
break; case 2://关闭计算机
if (dwVersion < 0x80000000)
{// Windows NT
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // 设置一个权限
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_SHUTDOWN,0);
}
break;
}
}
The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.BOOL MySystemShutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return( FALSE );
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE; return TRUE;
}
For more information about setting security privileges, see Privileges.
AbortSystemShutdown Stops a system shutdown started by using the InitiateSystemShutdown function.
ExitWindows Logs off the current user.
ExitWindowsEx Either logs off the current user, shuts down the system, or shuts down and restarts the system.
InitiateSystemShutdown Initiates a shutdown and optional restart of the specified computer.
InitiateSystemShutdownEx Initiates a shutdown and optional restart of the specified computer.
LockWorkStation Locks the workstation's display, protecting it from unauthorized use.
定时用SETTIMER定期检查是否到指定时间就行