5

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();
    }
Avishekh Bharati
  • 1,858
  • 4
  • 27
  • 44

1 Answers1

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" />
kennyzx
  • 12,845
  • 6
  • 39
  • 83
NattyMan0007
  • 103
  • 13
  • I run the application in administrator mode. There is no problem with it. But when the installer is created, distributed to QA and then when he runs, it gives the Access denied error. So but when the installed application is then restarted, it solves the problem. – Avishekh Bharati Apr 19 '17 at 06:20
  • You have to set the installer to require Administrative privileges either using its manifest or another method. So that it will request the system for admin access before executing. – NattyMan0007 Apr 19 '17 at 06:25
  • You can set a msi, in Visual Studio, to require Admin privledges by using the AdminUser or Privledged property on a new Launch condition. – NattyMan0007 Apr 19 '17 at 06:27
  • Looks like I am not allowed to do this. 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 if I requesteExecutionLevel to requireAdministrator then this might open up a dialog box (which it shouldn't). – Avishekh Bharati Apr 19 '17 at 07:11
  • If the user is not privledged then UAC will open prompting for credentials. If the Watchdog is running first then try setting the watchdog to requireAdministrator. – NattyMan0007 Apr 19 '17 at 11:54