首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

bcb如何实现win xp自动关机

2012-05-28 
bcb怎么实现win xp自动关机bcb怎么实现win xp自动关机[解决办法]C/C++ codeif(IsReboot){int OSver GetO

bcb怎么实现win xp自动关机
bcb怎么实现win xp自动关机

[解决办法]

C/C++ code
    if(IsReboot)    {    int OSver = GetOSNum() ;    if(OSver ==0 )      {      Application->MessageBox(L"获取系统版本出错,请手动重新启动计算机使设置生效.",L"重启计算机",MB_OK|MB_ICONWARNING);//MB_OKCANCEL      return;      }    HANDLE hToken;    TOKEN_PRIVILEGES tkp;    LUID luid;    if(OSver> 4)    {        // Windows NT 3.51, Windows NT 4.0, Windows 2000,        // Windows XP, or Windows .NET Server        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))        {          Application->MessageBox(L"打开当前进程出错,请手动重新启动计算机使设置生效.",L"重启计算机",MB_OK|MB_ICONWARNING);//MB_OKCANCEL          return;        }        else        {            LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &luid);            tkp.PrivilegeCount = 1;  // one privilege to set            tkp.Privileges[0].Luid = luid;            tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;            AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL, 0);        }    }   Application->MessageBox(L"请保存好当前数据,单击确定重新启动计算机使设置生效.",L"重启计算机",MB_OK|MB_ICONWARNING);//MB_OKCANCEL   ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0);  }
[解决办法]
http://www.cyberforum.ru/cpp-builder/thread17102.html
C/C++ code
int ShutdownWindows(bool isReboot){  HANDLE            hToken;  LUID              takeOwnershipValue;  TOKEN_PRIVILEGES  tkp;  if (!OpenProcessToken(GetCurrentProcess(),                        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))    return 0;  if (!LookupPrivilegeValue(0, SE_SHUTDOWN_NAME, &takeOwnershipValue))    return 0;  tkp.PrivilegeCount = 1;  tkp.Privileges[0].Luid = takeOwnershipValue;  tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  AdjustTokenPrivileges(hToken, false, &tkp, sizeof(TOKEN_PRIVILEGES), 0, 0);  if (GetLastError())    return 0;  if(!isReboot)     //выключение     return ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN, 0);  else     //перезагрузка     return ExitWindowsEx(EWX_FORCE | EWX_REBOOT, 0);}
[解决办法]
C/C++ code
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();}
[解决办法]
编译了,没问题,楼主试下
void PreProcess(void)
{
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);

}



void __fastcall TForm1::Button1Click(TObject *Sender)


{
//重新登录计算机
PreProcess();
ExitWindowsEx(EWX_LOGOFF,0);

}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//关闭计算机
PreProcess();
ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0);
}
//---------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
//chongqi
PreProcess();
ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0);
}
//---------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
//qiangzhiguanbi
PreProcess();
ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0);
}

热点排行