none
Process.MainModule Access Is Denied

    Question

  • Hi,

    I'm using the System.Diagnostics.Process.GetProcessByName() to get access to a wcf service host that runs as a windows service.

    The process hosts multiple services and I'm trying to access the WorkingSet64 for one of the services. When i call System.Diagnostics.Process.GetProcessByName()  4 processes are returned for the name provided, when i loop through these processes and access the Process.MainModule.Filename i get an "Access Denied" error.

    The process runs as a system process, when i run the code on my dev machine it seems to return the information i am expecting (ACL is turned off) when i run the code on the server (ACL turned on) an access is denied exception is thrown.

    I've seen this issue mentioned in a few places but most of the answers suggest to just skip the inaccessible items with a try catch and that isn't what i want, i need to inspect the inaccessible items.

    Thanks

    Aaron

    Code to reproduce

     class Program
        {
            static void Main(string[] args)
            {
    
                Process[] service;
                service = Process.GetProcesses();
    
                Console.WriteLine(service.Count());
    
                foreach (var s in service)
                {
                    Console.WriteLine(s.ProcessName);
    
                    try
                    {
                        Console.WriteLine(s.WorkingSet64);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR---"  + ex.Message);
                    }
    
                    Console.WriteLine("-------------------");
                }
    
                Console.ReadLine();
    
            }
        }

    Tuesday, September 23, 2014 3:53 PM

Answers

  • It is a permissions issue.  You cannot access the information you're requesting.  One possible workaround would be to run your app elevated so you can access the information.  However even this doesn't guarantee you'll always have access.  It is possible to deny administrators access to things (although they can give themselves access back). 

    Also note that just because you have a Process object doesn't mean all the properties on the object will work.  This type is used for both getting access to other processes and for starting new processes.  Some properties are not available unless you start the process yourself.  The working set property should be fine though.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by AaronHammond Wednesday, September 24, 2014 8:46 AM
    Tuesday, September 23, 2014 5:27 PM
    Moderator