双击button1,在代码窗体中填写如下代码即可::
void __fastcall TForm1::Button1Click(TObject *Sender)
{
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;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,
false,
&tkp,
0,
(PTOKEN_PRIVILEGES)NULL,
0);
// 下面几个动作中任选一种执行 ....
//ExitWindowsEx(EWX_LOGOFF,0); //注销当前用户
//ExitWindowsEx(EWX_REBOOT,0); // 重新启动系统
ExitWindowsEx(EWX_SHUTDOWN,0); //正常关闭系统
//ExitWindowsEx(EWX_POWEROFF,0); // 退出windows系統,并关闭机器电源(仅支持ATX规格)
//ExitWindowsEx(FORCE,0); //强行关闭系统
//ExitWindowsEx(FORCEIFHUNG,0);//Windows2000及其以后版本支持的强行关闭系统
}