/// <summary> /// Gets a boolean value indicating whether the access to a process using the AllAccess flag is denied or not. /// </summary> /// <param name="pid">The process ID of the process</param> /// <returns>True if denied and false if not.</returns> private static bool TestProcessAccessUsingAllAccessFlag(uint pid) { IntPtr processHandle = NativeMethods.OpenProcess(ProcessAccessFlags.AllAccess, true, (int)pid); if (Win32Helpers.GetLastError() == 5) { // Error 5 = ERROR_ACCESS_DENIED _ = Win32Helpers.CloseHandleIfNotNull(processHandle); return(true); } else { _ = Win32Helpers.CloseHandleIfNotNull(processHandle); return(false); } }
/// <summary> /// Gets the process name for the process ID /// </summary> /// <param name="pid">The id of the process/param> /// <returns>A string representing the process name or an empty string if the function fails</returns> internal static string GetProcessNameFromProcessID(uint pid) { IntPtr processHandle = NativeMethods.OpenProcess(ProcessAccessFlags.QueryLimitedInformation, true, (int)pid); StringBuilder processName = new StringBuilder(MaximumFileNameLength); if (NativeMethods.GetProcessImageFileName(processHandle, processName, MaximumFileNameLength) != 0) { _ = Win32Helpers.CloseHandleIfNotNull(processHandle); return(processName.ToString().Split('\\').Reverse().ToArray()[0]); } else { _ = Win32Helpers.CloseHandleIfNotNull(processHandle); return(string.Empty); } }