本文介绍了在 C# 中访问 Process.MainModule.FileName 时如何避免 Win32 异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了一个新项目,列出了所有正在运行的进程的完整路径.当访问某些进程时,程序崩溃并抛出 Win32Exception.描述说列出流程模块时发生错误.最初我认为可能会出现这个问题,因为我在 64 位 平台上运行它,所以我为 CPU 类型 x86AnyCPU.不过,我也遇到了同样的错误.

I started a new project listing the full paths to all running processes. When accessing some of the processes the program crashes and throws a Win32Exception. The description says an error occured while listing the process modules. Initially I thought this problem might occur because I'm running it on a 64-bit platform, so I recompiled it for the CPU types x86 and AnyCPU. I'm getting the same error, though.

Process p = Process.GetProcessById(2011);
string s = proc_by_id.MainModule.FileName;

错误发生在第 2 行.空白字段显示发生错误的进程:

The error occurs in line #2. The blank fields show processes where the error occured:

有什么办法可以绕过这个错误信息吗?

Is there any way to get around this error message?

推荐答案

当您尝试访问 MainModule 属性时抛出异常.此属性的文档没有将 Win32Exception 列为可能的异常,但查看该属性的 IL,很明显访问它可能会引发此异常.通常,如果您尝试执行操作系统中不可能或不允许的操作,它会抛出此异常.

The exception is thrown when you try to access the MainModule property. The documentation for this property does not list Win32Exception as a possible exception, but looking at the IL for the property it is evident that accessing it may throw this exception. In general it will throw this exception if you are trying to do something that is impossible or not allowed in the OS.

Win32Exception 有属性 NativeErrorCode 和一个 Message 来解释问题所在.您应该使用该信息来解决您的问题.NativeErrorCode 是 Win32 错误代码.我们可以整天猜测问题是什么,但真正解决这个问题的唯一方法是检查错误代码.

Win32Exception has the property NativeErrorCode and also a Message that will explain what the problem is. You should use that information to troubleshoot your problem. NativeErrorCode is the Win32 error code. We can guess all day long what the problem is but the only way to actually figure this out is to inspect the error code.

但继续猜测,这些异常的一个来源是从 32 位进程访问 64 位进程.这样做会抛出一个带有以下消息的 Win32Exception:

But to continue guessing, one source of these exceptions is accessing 64 bit processes from a 32 bit process. Doing that will throw a Win32Exception with the following message:

32 位进程无法访问 64 位进程的模块.

您可以通过评估 Environment.Is64BitProcess 来获取进程的位数.

You can get the number of bits of your process by evaluating Environment.Is64BitProcess.

即使作为 64 位进程运行,您也永远不会被允许访问进程 4(系统)或进程 0(系统空闲进程)的 MainModule.这将抛出一个带有消息的 Win32Exception:

Even running as a 64 bit process you will never be allowed to access MainModule of process 4 (System) or process 0 (System Idle Process). This will throw a Win32Exception with the message:

无法枚举进程模块.

如果您的问题是要创建类似于任务管理器中的进程列表,则必须以特殊方式处理进程 0 和 4,并为它们指定特定名称(就像任务管理器一样).请注意,在旧版本的 Windows 上,系统进程的 ID 为 8.

If you problem is that you want to make a process listing similar to the one in Task Manager you will have to handle process 0 and 4 in a special way and give them specific names (just as Task Manager does). Note that on older versions of Windows the system process has ID 8.

这篇关于在 C# 中访问 Process.MainModule.FileName 时如何避免 Win32 异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 20:13