API函数:ExitWindowsEx(Long uFlags,Long dwReserved );
uFlags : 指定下述一个或多个标志(用OR运算符合并到一起)
EWX_FORCE 强迫中止没有响应的进程
EWX_LOGOFF 中止进程,然后注销
EWX_SHUTDOWN 关掉系统电源(如果可能的话,ATX电源就可以)
EWX_REBOOT 重新引导系统
EWX_SHUTDOWN 关闭系统
dwReserved : 保留,设为零
注:(1)这个函数调用后会立刻返回,系统关闭过程是在后台进行的。注意先中止自己的应用程序,使关闭过程更显平顺。当然,您的进程必须有足够的优先权,否则也不能执行这种操作;
(2)2000以上系统要提升权限,下面是提升权限的函数
1)BOOL MySystemShutdown(ff)
{
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(ff, 0)) /* 这里改 EWX_SHUTDOWN 为EWX_REBOOT */
return FALSE;
return TRUE;
}
2)bool MyExitWindowsEx(UINT uFlag)
{ OSVERSIONINFO osv;
osv.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(&osv);
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT){
HANDLE hProcess,hToken;
TOKEN_PRIVILEGES Privileges;
LUID luid;
hProcess=GetCurrentProcess();
OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);
Privileges.PrivilegeCount=1;
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
Privileges.Privileges[0].Luid=luid;
Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
}
ExitWindowsEx(uFlag,0);
}
事例:
void __fastcall TForm1::BnOKClick(TObject *Sender)
{
OSVERSIONINFO ovi;
ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx(&ovi))
{
if(ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(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, NULL, 0);
}
}
}
unsigned int m = EWX_SHUTDOWN;
if(RbPowerOff->Checked) //选择了关机
m|=EWX_POWEROFF;
else if(RbReboot->Checked) //选择了重新启动
m|=EWX_REBOOT;
else if(RbLogoff->Checked) //选择了注销
m|=EWX_LOGOFF;
ExitWindowsEx(m,0); //执行选择的命令
Close();
}
uFlags : 指定下述一个或多个标志(用OR运算符合并到一起)
EWX_FORCE 强迫中止没有响应的进程
EWX_LOGOFF 中止进程,然后注销
EWX_SHUTDOWN 关掉系统电源(如果可能的话,ATX电源就可以)
EWX_REBOOT 重新引导系统
EWX_SHUTDOWN 关闭系统
dwReserved : 保留,设为零
注:(1)这个函数调用后会立刻返回,系统关闭过程是在后台进行的。注意先中止自己的应用程序,使关闭过程更显平顺。当然,您的进程必须有足够的优先权,否则也不能执行这种操作;
(2)2000以上系统要提升权限,下面是提升权限的函数
1)BOOL MySystemShutdown(ff)
{
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(ff, 0)) /* 这里改 EWX_SHUTDOWN 为EWX_REBOOT */
return FALSE;
return TRUE;
}
2)bool MyExitWindowsEx(UINT uFlag)
{ OSVERSIONINFO osv;
osv.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(&osv);
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT){
HANDLE hProcess,hToken;
TOKEN_PRIVILEGES Privileges;
LUID luid;
hProcess=GetCurrentProcess();
OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);
Privileges.PrivilegeCount=1;
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
Privileges.Privileges[0].Luid=luid;
Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
}
ExitWindowsEx(uFlag,0);
}
事例:
void __fastcall TForm1::BnOKClick(TObject *Sender)
{
OSVERSIONINFO ovi;
ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx(&ovi))
{
if(ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(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, NULL, 0);
}
}
}
unsigned int m = EWX_SHUTDOWN;
if(RbPowerOff->Checked) //选择了关机
m|=EWX_POWEROFF;
else if(RbReboot->Checked) //选择了重新启动
m|=EWX_REBOOT;
else if(RbLogoff->Checked) //选择了注销
m|=EWX_LOGOFF;
ExitWindowsEx(m,0); //执行选择的命令
Close();
}