c# - 为关闭计算机而创建的 Windows 服务在计算机锁定时无法运行

标签 c# .net windows-services

我用 C# 创建了一个 Windows 服务来关闭计算机。

当计算机未锁定 (Ctrl + Alt + Del) 时,该服务工作正常。

但是当我的计算机被锁定时它不会关闭的一些原因。

// call
DoExitWin(EWX_SHUTDOWN);

[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
    public int Count;
    public long Luid;
    public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private static void DoExitWin(int flg)
{
    bool ok;
    TokPriv1Luid tp;
    IntPtr hproc = GetCurrentProcess();
    IntPtr htok = IntPtr.Zero;
    ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
    tp.Count = 1;
    tp.Luid = 0;
    tp.Attr = SE_PRIVILEGE_ENABLED;
    ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
    ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,IntPtr.Zero);
    ok = ExitWindowsEx(flg, 0);
}

更新:

根据 Chris Haas 的帮助,我试图根据 ok 的值查找哪个调用返回错误:

ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
int error = Marshal.GetLastWin32Error(); //error 87 ok return true 
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid); 
error = Marshal.GetLastWin32Error(); //error 997 ok return true rest of ok true with zero error code

ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,IntPtr.Zero);
ok = ExitWindowsEx(flg, 0); //This mean error in ok 

但是 ok 始终返回 true。

最佳答案

我猜这是一个权限问题,但我不确定。一件非常重要的事情是你正在抛弃潜在的错误。您一直设置 ok 变量,但您从不检查它。这可能会告诉你哪里有问题

编辑

此外,我认为您实际上想传递 EWX_SHUTDOWN | EWX_POWEROFF

编辑 2

如果出现错误,您应该调用 Marshal.GetLastWin32Error()

编辑 3

您不必每次都调用 GetLastError,只要 ok 为 false 即可:

int error;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
if(!ok){
    error = Marshal.GetLastWin32Error();
    throw new ApplicationException('Error : ' + error);
}
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
if(!ok){
    error = Marshal.GetLastWin32Error();
    throw new ApplicationException('Error : ' + error);
}
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,IntPtr.Zero);
if(!ok){
    error = Marshal.GetLastWin32Error();
    throw new ApplicationException('Error : ' + error);
}
ok = ExitWindowsEx(flg, 0);
if(!ok){
    error = Marshal.GetLastWin32Error();
    throw new ApplicationException('Error : ' + error);
}

关于c# - 为关闭计算机而创建的 Windows 服务在计算机锁定时无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5735922/

相关文章:

java - 不支持的 major.minor 版本 java 作为带有 procrun 的 Windows 服务

c# - rijndael 128 cfb C# 和 php

c# - 基于词频的最大编辑距离和建议

.net - 每个优秀的 .NET 开发人员都应该能够回答的问题?

.net - 在 .NET WebBrowser 中禁止文本选择

windows-services - Windows-Service 中的 Serilog 未写入日志文件

c# - 遍历列表框中的项目

c# - RedirectToAction()和View()有什么区别

c# - 在 .NET 中使用正则表达式替换字符串

c# - 是否可以创建部署为 EXE 或 Windows 服务的独立 C# Web 服务?