这段代适用于win98/me/2000/winxp 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); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) return FALSE; // Shut down the system and force all applications to close. if ( !ExitWindowsEx( EWX_POWEROFF | EWX_FORCE, 0) ) return FALSE; return TRUE;
System Shutdown Functions The 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.
UINT uFlags, // shutdown operation
DWORD dwReserved // reserved
);
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);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if ( !ExitWindowsEx( EWX_POWEROFF | EWX_FORCE, 0) )
return FALSE; return TRUE;
The 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.
UINT uFlags, // shutdown operation
DWORD dwReserved // reserved
);win2000下:if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
MessageBox("OpenProcessToken failed!");
} 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); //调整获得的权限
if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("切换系统级权限失败!");
} fResult =InitiateSystemShutdown(
NULL, // 要关的计算机用户名
"关机时间已到,WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE, // ask user to close apps
FALSE); //设为TRUE为重起,设为FALSE为关机
if(!fResult)
{
MessageBox("初始化系统关机失败!");
} tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("AdjustTokenPrivileges disable failed.");
} ExitWindowsEx(EWX_SHUTDOWN,0);