14

I want to handle this differently, ie. determine if I have access or not.

Is it possible to see if you have access to the main module or not?

foreach (Process p in Process.GetProcesses())
        {
            try
            {
                //This throws error for some processes.
                if (p.MainModule.FileName.ToLower().EndsWith(ExeName, StringComparison.CurrentCultureIgnoreCase))
            {
                 //Do some stuff
            }

            }
            catch (Exception)
            {  
                //Acess denied 
            }
        }
Niklas
  • 1,753
  • 4
  • 16
  • 35
  • 2
    Here we go again. Have a look @ http://stackoverflow.com/questions/2774527/access-denied-while-using-system-diagnostics-process – leppie Dec 08 '11 at 12:56

2 Answers2

13
  [Flags]
  private enum ProcessAccessFlags : uint
  {
      QueryLimitedInformation = 0x00001000
  }

  [DllImport("kernel32.dll", SetLastError = true)]  
  private static extern bool QueryFullProcessImageName(
        [In] IntPtr hProcess,
        [In] int dwFlags,
        [Out] StringBuilder lpExeName,
        ref int lpdwSize);

    [DllImport("kernel32.dll", SetLastError = true)]
  private static extern IntPtr OpenProcess(
     ProcessAccessFlags processAccess,
     bool bInheritHandle,
     int processId);

String GetProcessFilename(Process p)
{ 
 int capacity = 2000;
 StringBuilder builder = new StringBuilder(capacity);
 IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
 if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
 {
    return String.Empty;
 }

 return builder.ToString();
}

Use pinvoke with ProcessAccessFlags.QueryLimitedInformation. This will allow you to grab the filename of the process without having special admin privileges and works across x32 and x64 processes.

Clueless
  • 1,190
  • 2
  • 14
  • 30
user99999991
  • 1,351
  • 3
  • 19
  • 43
  • 1
    That just returns an empty string. – Stefan Steiger Sep 08 '21 at 21:25
  • Probably something specific to your system - either privileges or the process itself. – user99999991 Sep 13 '21 at 06:38
  • This code works, but I'm curious as to why `QueryLimitedInformation = 0x00001000` works where as in the documentation it is `0x1000` which in my case gives access denied? Thank you for the snippet, it was a life saver! – shoaib30 Apr 22 '22 at 04:02
3

I see two possible causes of the exception:

  1. It may be that your process is x86 and the process being queried is x64 or vice versa.
  2. Every process has a so called ACL (Access control list) that describes who can interact with it, the processes you are having problems with have for security reasons an empty ACL so even as administrator you cannot mess with them. For example, there's a handfull of processes (audiodg, System, and Idle from the top of my head) that throw an exception due to the access rights.

Just use a try/catch to your loop to deal with those processes.

Dmitriy Konovalov
  • 1,777
  • 14
  • 14