在计算机锁定时,为关闭计算机而创建的Windows服务无效

Roh*_*hit 5 .net c# 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);
}
Run Code Online (Sandbox Code Playgroud)

更新:

基于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 
Run Code Online (Sandbox Code Playgroud)

但是ok一直都是真的.

Chr*_*aas 2

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

编辑

另外,我认为你实际上想通过EWX_SHUTDOWN | EWX_POWEROFF

编辑2

如果出现错误,您应该致电Marshal.GetLastWin32Error()

编辑3

您不必每次都调用 GetLastError,只要 ifok为 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);
}
Run Code Online (Sandbox Code Playgroud)