void __fastcall TMainForm::Button7Click(TObject *Sender) { HANDLE hToken; TOKEN_PRIVILEGES TP; //获取当前进程权限令牌 if(!::OpenProcessToken(::GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken)) { ::MessageBox(this->Handle,"您没有关机的权限!","关闭计算机",MB_OK+MB_ICONERROR); return; } ::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&TP.Privileges[0].Luid); TP.PrivilegeCount = 1; // one privilege to set TP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //调整权限 ::AdjustTokenPrivileges(hToken,false,&TP,sizeof(TP),NULL,0); if(::GetLastError() != ERROR_SUCCESS) { ::MessageBox(this->Handle,"调整权限失败!","关闭计算机",MB_OK+MB_ICONERROR); return; } //关机函数,参数查阅SDK //::ExitWindowsEx(EWX_LOGOFF,NULL); //锁定工作站,都不用调整权限,就可以实现 //LockWorkStation(); //显示一个对话框,计时关闭系统 ::InitiateSystemShutdown(NULL,"您无权限使用系统,即将关闭!",30,true,false); ::Sleep(5000); //取消关机 ::AbortSystemShutdown(NULL); } TOKEN_PRIVILEGES tkp,tkpnewbutignored; HANDLE hdltokenhandle; LUID tmpluid; BOOL rc; DWORD lbufferneeded; HANDLE hdlprocesshandle = GetCurrentProcess(); rc = ::OpenProcessToken(hdlprocesshandle,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hdltokenhandle); rc = LookupPrivilegeValue("","SeShutdownPrivilege", &tmpluid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Luid = tmpluid; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; rc = ::AdjustTokenPrivileges(hdltokenhandle, false, &tkp, sizeof(tkpnewbutignored), &tkpnewbutignored, &lbufferneeded); /////////////////////////////////////////////////////////////////// //如果是win98,下面一句够了;如果是win2000上面的是取得权限。 ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0);
|