Reputation: 508
I want to prevent shutdown on Windows 7. I acquire se_shutdown_privilege
successfully, but AbortSystemShutdown
is always failing. I tried AbortSystemShutdown(NULL)
, AbortSystemShutdown("127.0.0.1")
, and AbortSystemShutdown(PcName)
.
So far no success.
Upvotes: 1
Views: 1544
Reputation: 4925
This works fine for me on Windows 7 x64. Since you didn't post any code I have no idea what you're doing differently. The SetPrivilege
function was copied from this MSDN page: http://msdn.microsoft.com/en-us/library/windows/desktop/aa446619%28v=vs.85%29.aspx
I start the shutdown by typing this in a command prompt: 'shutdown /s /t 500000' and running the program cancels it.
#include <Windows.h>
#include <stdio.h>
BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if ( !LookupPrivilegeValue(NULL, lpszPrivilege, &luid ) )
{
printf("LookupPrivilegeValue error: %u\n", GetLastError() );
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0;
if ( !AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL) )
{
printf("AdjustTokenPrivileges error: %u\n", GetLastError() );
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
printf("The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
int main()
{
HANDLE hToken;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken);
if(!SetPrivilege(hToken, SE_SHUTDOWN_NAME, TRUE))
{
printf("Could not adjust privileges\n");
}
if(!AbortSystemShutdown(NULL))
{
printf("AbortSystemShutdown failed (%08x)", GetLastError());
}
CloseHandle(hToken);
return 0;
}
Upvotes: 1
Reputation: 1909
Apparently, AbortSystemShutDown
aborts a shutdown invoked by InitiateSystemShutdown (and the Ex version of that function) rather than, say, ExitWindows
.
The InitiateSystemShutdown and InitiateSystemShutdownEx functions display a dialog box that notifies the user that the system is shutting down. During the shutdown time-out period, the AbortSystemShutdown function can prevent the system from shutting down.
Upvotes: 3