private static void SetPrivilege(string privilegeName, int attrib) { SafeTokenHandle hToken = null; Interop.LUID debugValue = new Interop.LUID(); // this is only a "pseudo handle" to the current process - no need to close it later SafeProcessHandle processHandle = Interop.mincore.GetCurrentProcess(); // get the process token so we can adjust the privilege on it. We DO need to // close the token when we're done with it. if (!Interop.mincore.OpenProcessToken(processHandle, Interop.TOKEN_ADJUST_PRIVILEGES, out hToken)) { throw new Win32Exception(); } try { if (!Interop.mincore.LookupPrivilegeValue(null, privilegeName, out debugValue)) { throw new Win32Exception(); } Interop.TokenPrivileges tkp = new Interop.TokenPrivileges(); tkp.Luid = debugValue; tkp.Attributes = attrib; Interop.mincore.AdjustTokenPrivileges(hToken, false, tkp, 0, IntPtr.Zero, IntPtr.Zero); // AdjustTokenPrivileges can return true even if it failed to // set the privilege, so we need to use GetLastError if (Marshal.GetLastWin32Error() != Interop.ERROR_SUCCESS) { throw new Win32Exception(); } } finally { #if FEATURE_TRACESWITCH Debug.WriteLineIf(_processTracing.TraceVerbose, "Process - CloseHandle(processToken)"); #endif if (hToken != null) { hToken.Dispose(); } } }
// ----------------------------- // ---- PAL layer ends here ---- // ----------------------------- static ProcessManager() { // In order to query information (OpenProcess) on some protected processes // like csrss, we need SeDebugPrivilege privilege. // After removing the depenecy on Performance Counter, we don't have a chance // to run the code in CLR performance counter to ask for this privilege. // So we will try to get the privilege here. // We could fail if the user account doesn't have right to do this, but that's fair. Interop.LUID luid = new Interop.LUID(); if (!Interop.mincore.LookupPrivilegeValue(null, Interop.SeDebugPrivilege, out luid)) { return; } SafeTokenHandle tokenHandle = null; try { if (!Interop.mincore.OpenProcessToken( Interop.mincore.GetCurrentProcess(), Interop.TOKEN_ADJUST_PRIVILEGES, out tokenHandle)) { return; } Interop.TokenPrivileges tp = new Interop.TokenPrivileges(); tp.Luid = luid; tp.Attributes = Interop.SE_PRIVILEGE_ENABLED; // AdjustTokenPrivileges can return true even if it didn't succeed (when ERROR_NOT_ALL_ASSIGNED is returned). Interop.mincore.AdjustTokenPrivileges(tokenHandle, false, tp, 0, IntPtr.Zero, IntPtr.Zero); } finally { if (tokenHandle != null) { tokenHandle.Dispose(); } } }
static ProcessManager() { // In order to query information (OpenProcess) on some protected processes // like csrss, we need SeDebugPrivilege privilege. // After removing the depenecy on Performance Counter, we don't have a chance // to run the code in CLR performance counter to ask for this privilege. // So we will try to get the privilege here. // We could fail if the user account doesn't have right to do this, but that's fair. Interop.LUID luid = new Interop.LUID(); if (!Interop.mincore.LookupPrivilegeValue(null, "SeDebugPrivilege", out luid)) { return; } SafeTokenHandle tokenHandle = null; try { if (!Interop.mincore.OpenProcessToken( Interop.mincore.GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, out tokenHandle)) { return; } Interop.TokenPrivileges tp = new Interop.TokenPrivileges(); tp.Luid = luid; tp.Attributes = Interop.SE_PRIVILEGE_ENABLED; // AdjustTokenPrivileges can return true even if it didn't succeed (when ERROR_NOT_ALL_ASSIGNED is returned). Interop.mincore.AdjustTokenPrivileges(tokenHandle, false, tp, 0, IntPtr.Zero, IntPtr.Zero); } finally { if (tokenHandle != null) { tokenHandle.Dispose(); } } }