Link to home
Start Free TrialLog in
Avatar of mnyeu
mnyeu

asked on

Reboot in NT

How do you reboot NT? I have tried ExitWindowsEx with REBOOT  
option.  It just logs you out.  I believe I need the SE_SHUTDOWN privilege but don't know how. Any idea?
Avatar of TKII
TKII

Try to read on in the docs. You need to enable some privileges in your access token. See :
GetTokenInformation(), TOKEN_PRIVILEGES, SetTokenInformation() and so on.
When you have enabled that SE_SHUTDOWN than you should be able to reboot.

Avatar of mnyeu

ASKER

I have tried this and did not work, any idea ?

// Get a token for this process.
 
if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
                 wsprintf(szBuf, "OpenProcessToken Error #%d", GetLastError ());
        MessageBox(NULL, szBuf, NULL, MB_OK);
    }
   // error("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){
                 wsprintf(szBuf, "Erro AdjustTokenPrivileges #%d", GetLastError ());
        MessageBox(NULL, szBuf, NULL, MB_OK);
    }
 //   error("AdjustTokenPrivileges");
 
// Shut down the system and force all applications to close.
 
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
    error("ExitWindowsEx");

Avatar of mnyeu

ASKER

Adjusted points to 100
The code seems ok to me (but I havn't done this before). Have you tried to use GetTokenInformation() to look at your privileges? What happens if you are stepping through the code in the debugger?
ASKER CERTIFIED SOLUTION
Avatar of TKII
TKII

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial