【问题标题】:AbortSystemShutdown on Windows 7 not workingWindows 7 上的 AbortSystemShutdown 不起作用
【发布时间】:2012-06-12 18:39:36
【问题描述】:

我想在 Windows 7 上阻止关机,我成功获得了 se_shutdown_privilege,但 AbortSystemShutdown 总是失败我尝试了 AbortSystemShutdown(NULL),AbortSystemShutdown("127.0.0.1"),AbortSystemShutdown(PcName)。

目前没有成功。

【问题讨论】:

标签: c++ windows winapi shutdown system-administration


【解决方案1】:

显然,AbortSystemShutDown 中止了由InitiateSystemShutdown(以及该函数的Ex 版本)调用的关机,而不是ExitWindows

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

【讨论】:

  • @VisaToHell:你确定吗?它不应该。在什么情况下调用AbortSystemShutdown,即系统为什么会关机?
  • @VisaToHell MSDN 文档说 AbortSystemShutdown 中止了 InitiateSystemShutdown 启动的操作。具体来说,它将在启动函数设置的超时间隔内中止。如果您在星期一碰巧发现它在其他无证情况下有效,这并不意味着它会在星期二做同样的事情。 API 文档就是合同。
【解决方案2】:

这对我在 Windows 7 x64 上运行良好。由于您没有发布任何代码,我不知道您在做什么不同。 SetPrivilege 函数复制自此 MSDN 页面:http://msdn.microsoft.com/en-us/library/windows/desktop/aa446619%28v=vs.85%29.aspx

我通过在命令提示符中键入以下内容来启动关机:'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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2017-04-26
    • 2015-06-24
    • 2011-02-19
    相关资源
    最近更新 更多