Win32Exception: Access is denied in System.Diagnostics.ProcessManager.OpenProcess()

2

Firstly, I am a naive developer and have no much indepth understanding of these codes. I am getting access denied in the code block which tries to get the version number of one of the application.

Scenario : There is no issue while running the code in a visual studio. But when the installation file is ran in different machines and log is inspected, it throws an Win32Exception: Access is denied.

(Program runs automatically right after installation. Actually before installing this application there would already be a watchdog service installed which automatically runs the application and revives it on closing. So, I believe I cannot set requestedExecutionLevel to requireAdmin which might display confirm dialog and give user the control over running the application.)

I am required to address this problem. After reading some blogs I believe this must be due to the lack of enough rights to retrieve the required information. But I am still unclear about how to address/solve this.

Exception from log file :

[  1,11216] 2017-04-17T11:43:53 - -------------------- Win32Exception caught --------------------
[  1,11216] 2017-04-17T11:43:53 - Access is denied
[  1,11216] 2017-04-17T11:43:53 - (Win32Err=0x00000005)
[  1,11216] 2017-04-17T11:43:53 - Stack trace is : 
[  1,11216] 2017-04-17T11:43:53 -    at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
   at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
   at System.Diagnostics.Process.get_MainModule()
   at IMPMDesktopClient.BaseIMClientDriver.GetVersion(UIntPtr windowUIntPtr)
   at IMPMDesktopClient.WindowDetector.Hooks.WindowsEventArgs..ctor(Int32 eventNum, UIntPtr windowHandle, Int32 idObject, Int32 idChild, String clsName, Rectangle pos)
   at IMPMDesktopClient.WindowDetector.Hooks.EventHooks.CheckForWindowOfInterest(UIntPtr hWnd, UIntPtr arg)
   at IMPMDesktopClient.WindowDetector.Hooks.EventHooks.CheckTopWindow(UIntPtr hWnd, UIntPtr arg)
   at IMPMDesktopClient.Win32Interop.EnumWindows(WndEnumCallback callback, UIntPtr param)
   at IMPMDesktopClient.WindowDetector.Hooks.EventHooks.DetectTheseWindowsNow(List`1 classes)
   at IMPMDesktopClient.WindowDetector.WindowClassManager.OnPostRegistration()
[  1,11216] 2017-04-17T11:43:53 - --------------------Win32Exception--------------------

Code for GetVersion() :

public static Version GetVersion(UIntPtr windowUIntPtr)
    {
        var noOfTrials = 2;
        var threadSleepPeriod = 1000;
        Process imProc = null;

        while (noOfTrials > 0)
        {
            try
            {
                imProc = Win32Interop.GetWindowProcess(windowUIntPtr);
                Version version;
                try
                {
                    version = GetModuleVersion(imProc.MainModule); 
                }
                catch (System.ComponentModel.Win32Exception)
                {
                    version = GetModuleVersion(imProc.Id);
                }

                // If getting version fails, retry it.
                if (version == null || (version.Major == 0 && version.Minor == 0))
                {
                    // The thread will sleep for 1s after 1st trial, 3s after 2nd trial.
                    Thread.Sleep(threadSleepPeriod);
                    noOfTrials--;
                    threadSleepPeriod += 2000;
                }
                else
                    return version;

                if (noOfTrials == 0)
                {
                    Log.Write(...);
                }
            }
            catch (Exception ex)
            {
                Log.Write(...);
            }
        }
        return new Version();
    }
c#
process
system.diagnostics
watchdog
win32exception
asked on Stack Overflow Apr 19, 2017 by Avi-B • edited Apr 19, 2017 by Avi-B

1 Answer

0

One solution would be to try running the application as an Administrator.

You can do this by closing visual studio and then opening it from a shortcut using "Right Click -> Run as administrator" then open your solution from that instance of VS

If that works then set the application to run as an administrator all the time by Editing the application manifest and changing <requesteExecutionLevel> to

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
answered on Stack Overflow Apr 19, 2017 by NattyMan0007 • edited Apr 19, 2017 by kennyzx

User contributions licensed under CC BY-SA 3.0