Link to home
Start Free TrialLog in
Avatar of larryH
larryHFlag for United States of America

asked on

ReBoot system

Is there an API call or calls that I can use to shutdown windows 95/98 and or NT, and reboot the PC.   I want to do a controlled shutdown and reboot from a C++ program.
If not an API call any way to do the a controlled reboot.
Avatar of jkr
jkr
Flag of Germany image

Simply use

ExitWindowsEx ( EXW_REBOOT, 0);

Feel free to ask if you need more information!

Avatar of larryH

ASKER

If it were only that simple...
      Tried using the ExitWindowsEx API call, and got an error code of 1314, which is ERROR_PRIVILEGE_NOT_HELD.  Sounds like an authorization problem.  After doing some research, I also tried using LogonUser and ImpersonateLoggedOnUser, both of which returned errors 1314, ERROR_PRIVILEGE_NOT_HELD.  Found a similar problem on Microsoft's support site, and followed the instructions there to change logon privileges for myself, going into User Manager/User Rights Policy and granting myself the privilege "Act as part of the operating system".  After doing this, the Logon User and ImpersonateLoggedOnUser now return error code 1056, which is ERROR_SERVICE_ALREADY_RUNNING, and ExitWindowsEx still returns 1314.  Don't know what else to try.  Help
Avatar of castorix
castorix

You must adjust token privileges under NT before calling ExitWindowsEx()
Something like this =>

HANDLE hToken;
TOKEN_PRIVILEGES NewPriv;
LUID  Luid;
OSVERSIONINFO  VersionInformation;
 ...
 //Test version
 GetVersionEx(&VersionInformation);
 if (VersionInformation.dwPlatformId==VER_PLATFORM_WIN32_NT) {
 // SE_SHUTDOWN_NAME privilege
   if (!OpenProcessToken(GetCurrentProcess(),
 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken))
   error("OpenProcessToken");
           LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&Luid);   NewPriv.PrivilegeCount=1;
   NewPriv.Privileges[0].Luid=Luid;
   NewPriv.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
   AdjustTokenPrivileges(hToken,FALSE,&NewPriv,0,
       (PTOKEN_PRIVILEGES)NULL,0);
   if (GetLastError() !=ERROR_SUCCESS)
       error("AdjustTokenPrivileges");
   }
ASKER CERTIFIED SOLUTION
Avatar of DarrinE
DarrinE

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
>>If it were only that simple...

It is that simple - the docs state that this privilege *is* needed on NT...

It doesn't only have to be enabled, the user calling this API must also be granted this privilege. Thus, DarrinE's code will only work for members of the administrators group.

BTW: On NT, you might also want to use 'InitiateSystemShutdown()'
Avatar of larryH

ASKER

Thanks it works just great.