如何检查程序是否使用.NET?

发布于 2024-08-18 07:12:08 字数 41 浏览 3 评论 0原文

我们可以检查正在运行的应用程序或程序是否使用.Net框架来执行自身?

Can we check if a running application or a program uses .Net framework to execute itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

于我来说 2024-08-25 07:12:08

我曾经从 Scott Hanselman 的面试问题列表中学到了一个技巧。您可以使用以下命令轻松列出在命令提示符中运行 .NET 的所有程序:

任务列表/m“mscor*”

它将列出加载模块中包含 mscor* 的所有进程。

我们可以在代码中应用相同的方法:

public static bool IsDotNetProcess(this Process process)
{
  var modules = process.Modules.Cast<ProcessModule>().Where(
      m => m.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase));

  return modules.Any();
}

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:

tasklist /m "mscor*"

It will list all processes that have mscor* amongst their loaded modules.

We can apply the same method in code:

public static bool IsDotNetProcess(this Process process)
{
  var modules = process.Modules.Cast<ProcessModule>().Where(
      m => m.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase));

  return modules.Any();
}
鲜血染红嫁衣 2024-08-25 07:12:08

使用 CLR COM 接口 ICorPublish 和 ICorPublishProcess。在 C# 中执行此操作的最简单方法是从 SharpDevelop 的调试器借用一些代码,然后执行以下操作:

ICorPublish publish = new ICorPublish();
ICorPublishProcess process;

process = publish.GetProcess(PidToCheck);

if (process == null || !process.IsManaged)
{
    // Not managed.
}
else
{
    // Managed.
}

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:

ICorPublish publish = new ICorPublish();
ICorPublishProcess process;

process = publish.GetProcess(PidToCheck);

if (process == null || !process.IsManaged)
{
    // Not managed.
}
else
{
    // Managed.
}
記柔刀 2024-08-25 07:12:08

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

束缚m 2024-08-25 07:12:08

我知道这大约晚了一百万年,但以防万一有帮助 - 我最喜欢的确定 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.

凝望流年 2024-08-25 07:12:08

通过编程方式,您可以使用 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 use System.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.

洒一地阳光 2024-08-25 07:12:08

性能监视器 中提供了正在运行的 .NET 进程的列表。只需运行 perfmon 并在监控工具>>中即可性能监视器单击+图标或按Ctrl+N。在可用计数器列表中,在列表开头找到 .NET CLR Jit 并选择一个子项。您将在所选对象的实例列表中看到.NET进程的列表。

如果您想要使用 C# 方法而不在管理员模式下运行应用程序,可以通过 Process Hacker 工具引入解决方案。

根据 Process Hacker / .NET Tools / native.c

大多数 .NET 进程都有一个打开名为 \BaseNamedObjects\Cor_Private_IPCBlock(v4)部分的句柄。这与 ICorPublish 使用的对象相同::GetProcess 函数。我们不调用该函数,而是简单地检查该节对象是否存在。这意味着: * 更好的性能。
* 无需管理员权限即可获取其他用户拥有的进程的 .NET 状态。

在 C# 中获取进程句柄列表 有点辛苦。相反,您可以从 Process Hacker plugins 文件夹下载 DotNetTools.dll 并创建一个 extern 方法来使用 PhGetProcessIsDotNet 函数。

A list of running .NET processes is available in Performance Monitor. Just run perfmon 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 :

Most .NET processes have a handle open to a section named \BaseNamedObjects\Cor_Private_IPCBlock(v4)<ProcessId>. This is the same object used by the ICorPublish::GetProcess function. Instead of calling that function, we simply check for the existence of that section object. This means: * Better performance.
* No need for admin rights to get .NET status of processes owned by other users.

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 an extern method to use PhGetProcessIsDotNet function.

长梦不多时 2024-08-25 07:12:08

我建议下载Redgate的DotNetReflector并检查它是否可以打开该应用程序。

I suggest downloading the Redgate's DotNetReflector and checking if it can open the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文