如何检查程序是否使用.NET?
我们可以检查正在运行的应用程序或程序是否使用.Net框架来执行自身?
Can we check if a running application or a program uses .Net framework to execute itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我曾经从 Scott Hanselman 的面试问题列表中学到了一个技巧。您可以使用以下命令轻松列出在命令提示符中运行 .NET 的所有程序:
它将列出加载模块中包含
mscor*
的所有进程。我们可以在代码中应用相同的方法:
There's a trick I once learned from Scott Hanselman's list of interview questions. You can easily list all programs running .NET in command prompt by using:
It will list all processes that have
mscor*
amongst their loaded modules.We can apply the same method in code:
使用 CLR COM 接口 ICorPublish 和 ICorPublishProcess。在 C# 中执行此操作的最简单方法是从 SharpDevelop 的调试器借用一些代码,然后执行以下操作:
Use the CLR COM interfaces ICorPublish and ICorPublishProcess. The easiest way to do this from C# is to borrow some code from SharpDevelop's debugger, and do the following:
使用
System.Reflection.Assembly.LoadFrom
函数加载.exe文件。如果您尝试加载不是 .NET 程序集的二进制文件,此函数将引发异常。Use
System.Reflection.Assembly.LoadFrom
function to load the .exe file. This function will throw exception if you try to load binary file that is not .NET assembly.我知道这大约晚了一百万年,但以防万一有帮助 - 我最喜欢的确定 exe 是否使用 .net 的方法是针对它运行 .net SDK 附带的 MSIL 反汇编程序。如果您确实拥有 .net exe,您将获得其内容的良好图形分解;如果是普通的旧 win32 exe,您会收到一条消息告诉您这一点。
I know this is about a million years too late, but in case it helps - my favourite method to figure out if an exe is using .net is to run MSIL disassembler against it which comes with .net SDK. If a .net exe you indeed have, you'll get a nice graphical breakdown of its contents; if a plain old win32 exe it be, you'll get a message telling you so.
通过编程方式,您可以使用 Win32 API(如 NtQueryInformationProcess)获取起始图像名称,或者在 .Net 中使用 System.Diagnostics.Process.GetProcesses()
并读取
Process.StartInfo.FileName
。然后使用以下 MSDN 文章中规定的详细信息打开并解码该图像的 PE 标头:
http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
警告:仅检测 .NET 构建的程序集,例如不会检测使用 CorHost API 动态托管 CLR 的 Win32 EXE。
Programmatically you'd get the starting image name using Win32 API like
NtQueryInformationProcess
, or in .Net useSystem.Diagnostics.Process.GetProcesses()
and read
Process.StartInfo.FileName
.Then open and decode the PE headers of that image using details prescribed in the MSDN article below:
http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
Caveats: will only detect .NET built assemblies e.g. won't detect Win32 EXEs dynamically hosting CLR using CorHost APIs.
性能监视器
中提供了正在运行的 .NET 进程的列表。只需运行perfmon
并在监控工具>>中即可性能监视器单击+图标或按Ctrl+N。在可用计数器列表中,在列表开头找到 .NET CLR Jit 并选择一个子项。您将在所选对象的实例列表中看到.NET进程的列表。如果您想要使用 C# 方法而不在管理员模式下运行应用程序,可以通过 Process Hacker 工具引入解决方案。
根据 Process Hacker / .NET Tools / native.c :
在 C# 中获取进程句柄列表 有点辛苦。相反,您可以从 Process Hacker
plugins
文件夹下载 DotNetTools.dll 并创建一个extern
方法来使用PhGetProcessIsDotNet
函数。A list of running .NET processes is available in
Performance Monitor
. Just runperfmon
and in the Monitoring Tools >> Performance Monitor click + Icon or press Ctrl+N. In the list of available counters, at the beginning of the list find .NET CLR Jit and select a sub item. You will see a list of .NET process in Instances of selected object list.If you want a method in C# without running your app in Administrator mode, there is solution introduced by Process Hacker tool.
According to Process Hacker / .NET Tools / native.c :
Getting a list of Process handles in C# is a bit of hard work. Instead you can download the DotNetTools.dll from Process Hacker
plugins
folder and create anextern
method to usePhGetProcessIsDotNet
function.我建议下载Redgate的DotNetReflector并检查它是否可以打开该应用程序。
I suggest downloading the Redgate's DotNetReflector and checking if it can open the application.