larryH
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.
If not an API call any way to do the a controlled reboot.
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_RUNN ING, and ExitWindowsEx still returns 1314. Don't know what else to try. Help
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_RUNN
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(&VersionInfor mation);
if (VersionInformation.dwPlat formId==VE R_PLATFORM _WIN32_NT) {
// SE_SHUTDOWN_NAME privilege
if (!OpenProcessToken(GetCurr entProcess (),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken))
error("OpenProcessToken");
LookupPrivilegeValue(NULL, SE_SHUTDOW N_NAME,&Lu id); NewPriv.PrivilegeCount=1;
NewPriv.Privileges[0].Luid =Luid;
NewPriv.Privileges[0].Attr ibutes=SE_ PRIVILEGE_ ENABLED;
AdjustTokenPrivileges(hTok en,FALSE,& NewPriv,0,
(PTOKEN_PRIVILEGES)NULL,0) ;
if (GetLastError() !=ERROR_SUCCESS)
error("AdjustTokenPrivileg es");
}
Something like this =>
HANDLE hToken;
TOKEN_PRIVILEGES NewPriv;
LUID Luid;
OSVERSIONINFO VersionInformation;
...
//Test version
GetVersionEx(&VersionInfor
if (VersionInformation.dwPlat
// SE_SHUTDOWN_NAME privilege
if (!OpenProcessToken(GetCurr
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken))
error("OpenProcessToken");
LookupPrivilegeValue(NULL,
NewPriv.Privileges[0].Luid
NewPriv.Privileges[0].Attr
AdjustTokenPrivileges(hTok
(PTOKEN_PRIVILEGES)NULL,0)
if (GetLastError() !=ERROR_SUCCESS)
error("AdjustTokenPrivileg
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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()'
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()'
ASKER
Thanks it works just great.
ExitWindowsEx ( EXW_REBOOT, 0);
Feel free to ask if you need more information!