有没有一种方法,点击了一个按钮之后就让计算机重启。要在按钮事件中添加什么代码啊。我加了个
ExitWindowsEx(EWX_REBOOT, 0)或者 ExitWindowsEx(EWX_REBOOT, EWX_FORCE)都不行,只是在我关闭生成的窗口之后把我的VS2008给关了。

解决方案 »

  1.   

    权限不够。关机代码#include <windows.h>const unsigned int SE_SHUTDOWN_PRIVILEGE = 0x13;
    1 #define SE_DEBUG_PRIVILEGE 0x14 //DEBUG 权限
    int main()
    {
        HMODULE hDll = ::LoadLibrary(L"ntdll.dll");
        typedef int (* type_RtlAdjustPrivilege)(int, bool, bool, int*);
        typedef int (* type_ZwShutdownSystem)(int);
        type_RtlAdjustPrivilege RtlAdjustPrivilege = (type_RtlAdjustPrivilege)GetProcAddress(hDll, "RtlAdjustPrivilege");
        type_ZwShutdownSystem ZwShutdownSystem = (type_ZwShutdownSystem)GetProcAddress(hDll, "ZwShutdownSystem");
        int nEn = 0;
        int nResult = RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, true, true, &nEn);
        if(nResult == 0x0c000007c)
        {
            nResult = RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, true, false, &nEn);
        }
        nResult = ZwShutdownSystem(2);
        FreeLibrary(hDll);
        return 0;

      

  2.   

    参考这个:
    http://topic.csdn.net/t/20040916/08/3377046.html
      

  3.   

    我在网上看了一下,我那个之所以不行,是因为那个API只是在Win9X下有用,不过一楼的兄弟,
    int nResult = RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, true, true, &nEn);
    这句话报异常,是怎么回事呢
      

  4.   


    UINT FFlag = EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCE;// Get version info to determine operation
    OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    if (GetVersionEx(&osvi))
    {
    // Determine the platform
    if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;

    // 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))
    {
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

    tkp.PrivilegeCount = 1;  // one privilege to set
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    }
    }
    }ExitWindowsEx(FFlag, 0);
      

  5.   

    不好意思 刚才那个是关机
    用UINT FFlag = EWX_REBOOT | EWX_FORCE;
      

  6.   

    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);   
    if (strAction=="自动关机")   ExitWindowsEx(EWX_POWEROFF, 0);
    if (strAction=="自动重启")   ExitWindowsEx(EWX_REBOOT, 0);
    if (strAction=="自动注销")   ExitWindowsEx(EWX_LOGOFF, 0);
      

  7.   

    直接执行系统命令 shutdown -r -t 0
    马上系统就重启了
      

  8.   

    void CShutRebootDlg::OnShutdown() 
    {
    // TODO: Add your control notification handler code here
    int nvalue= AfxMessageBox("是否真的要关闭计算机?",4+32+256);
    if(nvalue==IDYES) //用户按下了"是"按钮
    {
    OSVERSIONINFO osv;
        osv.dwOSVersionInfoSize = sizeof OSVERSIONINFO;
        GetVersionEx(&osv);//获取操作系统的版本
        if(VER_PLATFORM_WIN32_NT == osv.dwPlatformId)
        {
            // 如果运行在NT/XP平台,必须先取得权限
            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(EWX_SHUTDOWN,0);//关闭计算机
    }
    }
      

  9.   

    void CXXXDlg::ReStart(DWORD verInfo)
    {
    try
    {
    //如果获取到的系统dwMajorVersion值大于等于5,表示为NT以上操作系统,需提升进程权限
    if(verInfo>=5)
    {
    HANDLE ToHandle;
    TOKEN_PRIVILEGES tkp;
    //打开本进程访问信令
    if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&ToHandle))
    {
    //修改本进程权限
    LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    tkp.PrivilegeCount=1;
    tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    //通知系统已修改
    AdjustTokenPrivileges(ToHandle,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
    //获得权限后关闭计算机,要实现注销或重启则对应EWX_LOGOFF,EWX_REBOOT
    ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0);
    }
    }
    //否则为98以下,直接调用ExitWindowsEx函数即可
    else
    {
    ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0);
    }
    }
    catch(...)
    {
    //出现任何错误则返回
    printf("关机失败,请确认程序是否被拦截或具有管理权限!");
    return;
    }
    }
    DWORD CXXXDlg::GetVerInfo()
    {
    OSVERSIONINFO osver={sizeof(OSVERSIONINFO)};
    GetVersionEx(&osver);
    return osver.dwMajorVersion;
    }
    void CxxxDlg::OnRestart() 
    {
    // TODO: Add your control notification handler code here
    ReStart(GetVerInfo());
    }完整的重启方法,我自己就是这样做的,没问题。
      

  10.   

    BOOL MySystemShutdown()
    {
       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(EWX_SHUTDOWN | EWX_FORCE, 
                   SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
                   SHTDN_REASON_MINOR_UPGRADE |
                   SHTDN_REASON_FLAG_PLANNED)) 
          return FALSE;    return TRUE;
    }
      

  11.   

    BOOL ShutDownComputer()

      HANDLE hToken;     
      TOKEN_PRIVILEGES tkp = {0};     
           
      if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))     
      {
      return FALSE; 
      }   
        
      if(LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid))
      {
      return FALSE;
      }
        
      tkp.PrivilegeCount  = 1;          
      tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;     
     
      if(AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
      {
      return FALSE;
      }  if(!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
      {
      return FALSE;
      }
      return TRUE;
    }