【问题标题】:Win32Exception: Access is denied in System.Diagnostics.ProcessManager.OpenProcess()Win32Exception:访问在 System.Diagnostics.ProcessManager.OpenProcess() 中被拒绝
【发布时间】:2017-09-15 04:13:06
【问题描述】:

首先,我是一个天真的开发者,对这些代码没有太多深入的了解。 我在尝试获取其中一个应用程序的版本号的代码块中被拒绝访问。

场景:在 Visual Studio 中运行代码时没有问题。但是当安装文件在不同的机器上运行并检查日志时,它会抛出 Win32Exception: Access is denied.

(程序在安装后立即自动运行。实际上在安装此应用程序之前,已经安装了一个 看门狗服务,它会自动运行应用程序并在关闭时恢复它。所以,我相信我无法将 requestedExecutionLevel 设置为 requireAdmin,这可能会显示确认对话框并让用户控制运行应用程序。)

我需要解决这个问题。在阅读了一些博客后,我认为这一定是由于缺乏足够的权限来检索所需的信息。但我仍然不清楚如何解决/解决这个问题。

来自日志文件的异常:

[  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--------------------

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();
    }

【问题讨论】:

  • 是否以管理员身份运行?试过 psexc 吗?
  • 对不起.. 实际上它的 .msi(installer) 而不是 exc.

标签: c# process system.diagnostics watchdog win32exception


【解决方案1】:

一种解决方案是尝试以管理员身份运行应用程序。

您可以通过关闭 Visual Studio,然后使用“右键单击 -> 以管理员身份运行”从快捷方式打开它,然后从该 VS 实例打开您的解决方案来完成此操作

如果可行,则通过编辑应用程序清单并将<requesteExecutionLevel> 更改为

,将应用程序设置为始终以管理员身份运行
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

【讨论】:

  • 我以管理员模式运行应用程序。没有问题。但是当安装程序被创建、分发给 QA 并且当他运行时,它给出了拒绝访问错误。所以但是当安装的应用程序重新启动时,它解决了问题。
  • 您必须使用其清单或其他方法将安装程序设置为需要管理权限。这样它会在执行之前向系统请求管理员访问权限。
  • 您可以在 Visual Studio 中设置 msi,以通过在新的启动条件上使用 AdminUser 或 Privledged 属性来要求管理员权限。
  • 看来我不允许这样做。实际上在安装这个应用程序之前已经安装了一个看门狗服务,它会自动运行应用程序并在关闭时恢复它。所以我相信如果我 requesteExecutionLevel 到 requireAdministrator 那么这可能会打开一个对话框(它不应该)。
  • 如果用户没有特权,那么 UAC 将打开提示输入凭据。如果看门狗首先运行,请尝试将看门狗设置为 requireAdministrator。
猜你喜欢
  • 2021-09-04
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多