title: Win32Exception: Access is denied in System.Diagnostics.ProcessManager.OpenProcess()
body: <p>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.</p>
  
  <p>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 <strong>Win32Exception</strong>: Access is denied.</p>
  
  <p><em>(Program runs automatically right after installation. Actually before installing this application there would already be a <strong>watchdog service</strong> 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.)</em></p>
  
  <p>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.</p>
  
  <p>Exception from log file :</p>
  
  
  
  <pre><code>[  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></pre>
  
  
  
  <p>Code for GetVersion() :</p>
  
  <pre><code>public static Version GetVersion(UIntPtr windowUIntPtr)
      {
          var noOfTrials = 2;
          var threadSleepPeriod = 1000;
          Process imProc = null;
  
          while (noOfTrials &gt; 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 &amp;&amp; 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();
      }
  </code></pre>