首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.Diagnostics.ProcessManager.OpenProcess()中拒绝访问Win32Exception

System.Diagnostics.ProcessManager.OpenProcess()中拒绝访问Win32Exception
EN

Stack Overflow用户
提问于 2017-04-19 05:30:55
回答 1查看 8.5K关注 0票数 4

首先,我是一个天真的开发人员,对这些代码没有多少深入的理解。我在代码块中拒绝访问,该代码块试图获取其中一个应用程序的版本号。

场景:在visual中运行代码时没有问题。但是,当安装文件在不同的机器上运行并对日志进行检查时,它会抛出一个Win32Exception:访问被拒绝。

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

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

日志文件中的异常:

代码语言:javascript
运行
复制
[  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()的代码:

代码语言:javascript
运行
复制
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();
    }
EN

回答 1

Stack Overflow用户

发布于 2017-04-19 06:16:18

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

可以这样做:关闭visual studio,然后使用“右键单击->作为管理员运行”从快捷方式打开它,然后从VS实例中打开解决方案。

如果这样做有效,那么通过编辑应用程序清单并将<requesteExecutionLevel>更改为

代码语言:javascript
运行
复制
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43487133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档