private static void EnsureCurrentProcessDebugPrivilegeEnabled() { IntPtr hToken = IntPtr.Zero; NativeWrapper.LUID newLuid = new NativeWrapper.LUID(); NativeWrapper.TOKEN_PRIVILEGES newPrivilege = new NativeWrapper.TOKEN_PRIVILEGES(); try { bool result = NativeWrapper.OpenProcessToken(Process.GetCurrentProcess().Handle, NativeWrapper.TOKEN_ADJUST_PRIVILEGES, out hToken); if (!result) { log.Fatal("Error ensuring currents' process access rights"); Program.Exit(ExitCodes.ERROR_WHILE_EXECUTION); } result = NativeWrapper.LookupPrivilegeValue(null, NativeWrapper.SE_DEBUG_NAME, out newLuid); if (!result) { log.Fatal("Unable to determine LUID"); Program.Exit(ExitCodes.ERROR_WHILE_EXECUTION); } newPrivilege.PrivilegeCount = 1; newPrivilege.Luid = newLuid; newPrivilege.Attributes = NativeWrapper.SE_PRIVILEGE_ENABLED; result = NativeWrapper.AdjustTokenPrivileges( hToken, false, ref newPrivilege, 0, IntPtr.Zero, 0); if (!result) { log.Fatal("Error adjusting process privileges"); Program.Exit(ExitCodes.ERROR_WHILE_EXECUTION); } result = NativeWrapper.LookupPrivilegeValue(null, NativeWrapper.SE_TCB_NAME, out newLuid); if (!result) { log.Fatal("Unable to determine LUID"); Program.Exit(ExitCodes.ERROR_WHILE_EXECUTION); } newPrivilege.PrivilegeCount = 1; newPrivilege.Luid = newLuid; newPrivilege.Attributes = NativeWrapper.SE_PRIVILEGE_ENABLED; result = NativeWrapper.AdjustTokenPrivileges( hToken, false, ref newPrivilege, 0, IntPtr.Zero, 0); if (!result) { log.Fatal("Error adjusting process privileges"); Program.Exit(ExitCodes.ERROR_WHILE_EXECUTION); } } catch (Exception ex) { } finally { if (hToken != IntPtr.Zero) { NativeWrapper.CloseHandle(hToken); } } }