关键问题是如何使用Api函数 AdjustTokenProvileges
   哪为有使用经验的高手,能给出部分源代码?

解决方案 »

  1.   

    HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure BOOL fResult; // system shutdown flag // Get the current process token handle so we can get shutdown // privilege. if (!OpenProcessToken(GetCurrentProcess(), 
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) MessageBox(NULL,"OpenProcessToken failed.","Attention",0); // Get the LUID for 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 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,"AdjustTokenPrivileges enable failed.","Attention",0);
      

  2.   

    if (bIsNt ) // if OS is NT, the following is needed:
    {
    HANDLE hToken; 
    TOKEN_PRIVILEGES tkp; 

    // Get a token for this process. 
    if (!OpenProcessToken(GetCurrentProcess(), 
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    AfxMessageBox("OpenProcessToken"); 

    // 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) 
    AfxMessageBox("AdjustTokenPrivileges"); 
    } //now  we  can reboot;
             // if os is win9x, do the following directly: ExitWindowsEx(EWX_LOGOFF|EWX_REBOOT,0);
      

  3.   

    给分!
    关机需要最后调用Exitwindow(XX_poweroff)不给分。我晚上打你们家玻璃
      

  4.   

    非常感谢 freecutelei(猪八戒) 师兄!
    马上给分!
    两位都有分!!