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

问题描述

我开始一个新的项目列出所有正在运行的进程的完整路径。当访问某些进程时,程序崩溃并引发一个 Win32Exception 。描述说明列出过程模块时出现错误。最初我以为这个问题可能是因为我在一个 64位平台上运行,所以我重新编译了CPU类型 x86 AnyCPU STRONG>。我收到同样的错误。

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

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



有没有办法解决这个错误消息?

解决方案

尝试使用异常时抛出异常访问 MainModule 属性。该属性的文档不会列出 Win32Exception 作为一个可能的异常,但是查看该属性的IL是很明显的,访问它可能会抛出这个异常。一般来说,如果您尝试执行操作系统中不可能或不允许的操作,将抛出此异常。



Win32Exception 具有属性 NativeErrorCode 以及消息,这将解释问题是什么。您应该使用该信息来解决您的问题。 NativeErrorCode 是Win32错误代码。我们可以整天猜测问题是什么,但唯一的办法就是检查错误代码。



但是继续猜测,这些来源之一例外是从32位进程访问64位进程。这样做会引发一个 Win32Exception ,并显示以下消息:

通过评估 Environment.Is64BitProcess 。



甚至以64位进程运行,您将永远不能访问进程4(系统)的 MainModule )或进程0(系统空闲进程)。这将抛出一个 Win32Exception 与消息:

如果您遇到问题,您希望使进程列表类似于任务管理器中的进程,您将不得不处理进程0和4以特殊的方式给他们具体的名称(就像任务管理器一样)。请注意,在旧版Windows上,系统进程的ID为8。


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;

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?

解决方案

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

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:

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

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:

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