我在使用ExitWindowsEx(EWX_SHUTDOWN,0)时,发现它并不能关机.EWX_LOGOFF倒是可以注销.请教高手为什么?同时详细解释一下第二个参数的意思.谢谢!

解决方案 »

  1.   

    Shutting DownYou can use the ExitWindowsEx function to shut down the system. Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer.
    The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.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, 0)) 
          return FALSE;    return TRUE;
    }
      

  2.   

    From MSDN
    Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section. From re
    Windows NT: To shut down or restart the system, the calling process must use theAdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, seePrivileges. it means void CShutdownDlg::OnButton1() 
    {
    HANDLE hdl;  
    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hdl);  
      PTOKEN_PRIVILEGES ptoken = (PTOKEN_PRIVILEGES) new BYTE[sizeof(DWORD) +  
    sizeof(LUID_AND_ATTRIBUTES)];  
    ptoken->PrivilegeCount = 1;  
    LUID uid;  
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &uid);   ptoken->Privileges[0].Luid = uid;  
    ptoken->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
    AdjustTokenPrivileges(hdl, FALSE, ptoken, 0, NULL, NULL);    //向进程表内写入数据
      CloseHandle(hdl);  
    delete [](BYTE *)ptoken;  
    ExitWindowsEx(EWX_FORCE|EWX_POWEROFF,0);
    }
      

  3.   

    原型如下:
    The ExitWindowsEx function either logs off, shuts down, or shuts down and restarts the system. BOOL ExitWindowsEx(    UINT uFlags, // shutdown operation
        DWORD dwReserved  // reserved
       );第二个参数的意思:DWORD dwReserved  // reserved:保留字,不用
      

  4.   

    Please do not paste from MSDN
      

  5.   

    <转载>---该文章为定时关机程序,你自己整理一下
    因为2000属于多用户操作系统,要想关机就要获得权限。在查阅了MSDN后整理出了这个关机程序。         TOKEN_PRIVILEGES tkp;
    HANDLE hToken;
        
            if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    {
        MessageBox("OpenProcessToken failed!");
    }        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 (GetLastError() != ERROR_SUCCESS) 
    {
                MessageBox("AdjustTokenPrivileges enable failed!");
    }        fResult =InitiateSystemShutdown( 
                 NULL,                 // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
                 "由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!",  // 显示的消息
                 10,                                // 关机所需的时间
                 TRUE,                                 
                 TRUE);                             //设为TRUE为重起,设为FALSE为关机        if(!fResult) 
            { 
                 MessageBox("InitiateSystemShutdown failed."); 
    }         tkp.Privileges[0].Attributes = 0; 
            AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);         if (GetLastError() != ERROR_SUCCESS) 
    {
                 MessageBox("AdjustTokenPrivileges disable failed."); 
    }     ExitWindowsEx(EWX_SHUTDOWN,0);     //开始关机