I have used Visual studio 2022 for InitSystemShutdown method to shutdown remote system,the problem is that when I try to run visual studio in admin mode,InitiateSystemShutdown is working perfectly but when I try to run same in normal user mode,it throws " ERROR_NOT_ALL_ASSIGNED # Not all privileges or groups referenced are assigned to the # caller,what would be the problem?
bool ShutdownSystem() {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL fResult;
DWORD dwSize = 0, dwResult = 0;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
return FALSE;
LookupPrivilegeValue(NULL, SE_REMOTE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if(GetLastError() != ERROR_SUCCESS)
{
std::cout << "Printing GetLastError Msg" << GetLastError();
return FALSE;
}
fResult = InitiateSystemShutdown(
(LPSTR) "DESKTOP-KH3VE5E", // shut down remote computer
(LPSTR) "Shutdown called", // message for user
10, // time-out period, in seconds
FALSE, // ask user to close apps
FALSE); // reboot after shutdown
int s = GetLastError();
std::cout << s;
if(!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
return TRUE;
}
int main()
{
bool retVal = ShutdownSystem();
return 0;
}