C++ Windows 7上的中止系统关机不工作

C++ Windows 7上的中止系统关机不工作,c++,windows,winapi,shutdown,system-administration,C++,Windows,Winapi,Shutdown,System Administration,我想在Windows 7上阻止关机。我成功获得了se_shutdown_权限,但AbortSystemShutdown总是失败。我尝试了AbortSystemShutdown(NULL)、AbortSystemShutdown(“127.0.0.1”)、AbortSystemShutdown(PcName) 到目前为止还没有成功。显然,AbortSystemShutDown会中止由(以及该函数的版本)调用的关机,而不是,比如说,ExitWindows InitiateSystemShutdown

我想在Windows 7上阻止关机。我成功获得了se_shutdown_权限,但AbortSystemShutdown总是失败。我尝试了AbortSystemShutdown(NULL)、AbortSystemShutdown(“127.0.0.1”)、AbortSystemShutdown(PcName)


到目前为止还没有成功。

显然,
AbortSystemShutDown
会中止由(以及该函数的版本)调用的关机,而不是,比如说,
ExitWindows

InitiateSystemShutdown和InitiateSystemShutdownEx函数 显示一个对话框,通知用户系统正在运行 关闭。在停机超时期间 AbortSystemShutdown功能可防止系统关闭 放下


这对我在Windows7x64上工作很好。因为你没有发布任何代码,我不知道你做了什么不同。
SetPrivilege
功能是从此MSDN页面复制的:

我通过在命令提示符中键入以下命令开始关机:“shutdown/s/t 500000”,然后运行该程序取消关机

#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;
}
#包括
#包括
BOOL SetPrivilege(HANDLE hToken、LPCTSTR lpszPrivilege、BOOL bEnablePrivilege)
{
令牌特权;
路易斯·路易斯;
if(!LookupPrivilegeValue(NULL、lpszPrivilege和luid))
{
printf(“LookupPrivilegeValue错误:%u\n”,GetLastError());
返回FALSE;
}
tp.privilegecont=1;
tp.Privileges[0]。Luid=Luid;
tp.Privileges[0]。Attributes=bEnablePrivilege?SE_PRIVILEGE_ENABLED:0;
if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN\u PRIVILEGES),(PTOKEN\u PRIVILEGES)NULL,(PDWORD)NULL))
{ 
printf(“AdjustTokenPrivileges错误:%u\n”,GetLastError());
返回FALSE;
} 
如果(GetLastError()==未分配所有错误)
{
printf(“令牌没有指定的权限。\n”);
返回FALSE;
} 
返回TRUE;
}
int main()
{
拉赫托肯;
OpenProcessToken(GetCurrentProcess()、TOKEN\u ADJUST\u PRIVILEGES和&hToken);
if(!SetPrivilege(hToken,SE_SHUTDOWN_NAME,TRUE))
{
printf(“无法调整权限\n”);
}
如果(!AbortSystemShutdown(NULL))
{
printf(“AbortSystemShutdown失败(%08x)”,GetLastError();
}
闭合手柄(hToken);
返回0;
}

当AbortSystemShutdown()返回0时,您是否检查GetLastError()返回的内容?我得到0x0000045C,我认为它“无法中止系统关机,因为没有进行关机。”,该死的,我很确定我在win 7之前就已经有了这个功能……也许我可以通过调整注册表键来实现它?可能相关:@VisaToHell:你确定吗?不应该。您在什么情况下调用AbortSystemShutdown,即为什么系统会关闭?@VisaToHell MSDN文档中说AbortSystemShutdown会中止启动SystemShutdown的操作。具体来说,它将在initiate函数设置的超时时间间隔内中止。如果你在周一碰巧发现它在其他未记录的情况下工作,那并不意味着它在周二也会做同样的事情。API文档是合同的一部分。