这是关机的代码:
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL fResult;
   
   //打开与当前进程相关联的存取标识
 if (!::OpenProcessToken(GetCurrentProce(),TOKEN_ADJUST_PRIVILEGES
     |TOKEN_QUERY,&hToken))
 AfxMessageBox("OpenProcessToken failed");
   //查出本机系统的当前特权的Luid
::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privilege[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)
AfxMessageBox("AdjustTokenPrivileges enable failed");
fResult = ::InitiateSystemShutdown(NULL,"即将关机",20,FALSE,
             TRUE);
if (!fResult)
   AfxMessageBox("InitiateSystemShutDown failed");
tkp.Privileges[0].Attributes = 0;
::AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)   NULL,0);
if (::GetLastError()!=ERROR_SUCCESS)
      AfxMessageBox("AdjustTokenPrivileges disable failed");

解决方案 »

  1.   

    在关机以前需要取得关机的权限
    if (!OpenProcessToken(GetCurrentProcess(), 
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    MessageBox (NULL, TEXT("OpenProcessToken") ,  TEXT ("Error"),MB_ICONERROR); 
     // 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); 
    // Cannot test the return value of AdjustTokenPrivileges. 
    if (GetLastError() != ERROR_SUCCESS) 
        MessageBox (NULL, TEXT("AdjustTokenPrivileges") ,
       TEXT("Error"),MB_ICONERROR); 
    // Shut down the system and force all applications to close. 
    if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0)) 
        MessageBox (NULL, TEXT("ExitWindowsEx") ,
      TEXT("Error"),MB_ICONERROR);