是否可以在没有 exe 加载的情况下执行 .NET dll?

发布于 2024-10-19 23:27:32 字数 349 浏览 2 评论 0 原文

我很好奇是否有一种方法可以在新进程中执行静态 .DLL 方法,而无需为其创建 .EXE?

AFAIK,这对于本机 Win32/64 DLL 是不可能的。 .NET DLL 程序集怎么样?

更新:我忘了提及我主要对以编程方式执行此操作感兴趣(具体来说,来自 C# 代码)。

谢谢!

结论:虽然没有人“敢”说出来,但答案似乎都倾向于“不”。人们需要通过一种传统方式(EXE、PowerShell 等)启动一个进程,然后说服该进程加载 DLL 并执行其中的代码。我想我错误地希望托管 DLL 能够做更多事情。

再次感谢所有参与进来的人!

I'm curious if there's a way to execute a static .DLL method in a new process without having to create an .EXE for it?

AFAIK, this isn't possible with native Win32/64 DLLs. How about .NET DLL assemblies?

UPDATE: I forgot to mention I'm primarily interested in doing this programmatically (from C# code, to be specific).

Thanks!

CONCLUSION: Although no one "dared" to spell it out, the answers all seem to lean towards 'no'. One needs to start a process through one of the conventional ways (EXE, PowerShell, etc.) then convince that process to load the DLL and execute the code within. I guess I was falsely hoping that managed DLLs are capable of more.

Thanks again to everyone who chimed in!

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

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

发布评论

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

评论(5

失与倦" 2024-10-26 23:27:32

只需启动 PowerShell 提示符即可。

  [Reflection.Assembly]::LoadFile("Name of your dll")
  [Your.NameSpace.And.TypeName]::YourMethod()

我看到您希望从 C#

使用程序集限定名称创建类型:

 var t = Type.GetType("NameSpace.Type,Name Of Dll");
 var m = t.GetMethod("NameOfMethod");
 m.Invoke(null, new object[] { params });

这应该可以帮助您开始。

我不太清楚“在新进程中”是什么意思,但将其包装到 .exe/.ps1 中应该不难,您可以在命令行上通过某些选项启动。

因此,您不必为每个要调用的 DLL 创建新的 .exe。

但如果你想启动一个新进程,你应该启动一个新进程,这通常是通过启动一个新的 .EXE 来完成的。

Just start a PowerShell prompt.

  [Reflection.Assembly]::LoadFile("Name of your dll")
  [Your.NameSpace.And.TypeName]::YourMethod()

I see you want this from C#

Create the Type using the Assembly Qualified name:

 var t = Type.GetType("NameSpace.Type,Name Of Dll");
 var m = t.GetMethod("NameOfMethod");
 m.Invoke(null, new object[] { params });

This should get you started.

I don't exactly know what you mean by "In a new process", but it should not be to difficult to wrap this into a .exe/.ps1 that you can start with some option on the commandline.

So you do not have to create a new .exe for every DLL you want to invoke.

But if you want to start a new process, you should start a new process, this is typically done by starting a new .EXE.

以歌曲疗慰 2024-10-26 23:27:32

选项 1:.NET InstallUtil(正常 .NET 安装的一部分)

添加对 System.Configuration.Install 的引用,然后在程序集中放置类似的内容:

[System.ComponentModel.RunInstaller(true)]
public class Sample : System.Configuration.Install.Installer
{
    public override void Uninstall(System.Collections.IDictionary savedState)
    {
        base.Uninstall(savedState);

        this.Context.LogMessage("This can be in a .dll.");
        // Do your thing...
    }
}

然后滥用.NET InstallUtil:

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil /logtoconsole=false /logfile= /u [your assembly .dll here]

它可能有点混乱,尤其是在所有命令行参数都禁用日志记录的情况下。

选项 2:使用本机 rundll32 来逆 P/调用静态类方法

2016 编辑:现在是 Nuget 包:非托管导出 (src)!

未知 MSIL 指令 (msdn 论坛,2007 年)链接到 IL 汇编语言程序员参考的 beta 1 版本(2000 年 10 月 3 日,第 74 页),其中指出:

如果指定了 fromunmanagement,则
运行时会自动生成一个
thunk 将转换非托管
方法调用到托管调用,调用
方法,并将结果返回给
不受管理的环境。

.NET 2.0 IL 汇编专家Microsoft 内部。 NET IL 汇编器。 (最佳搜索关键词:ilasm vtentry)。

相关阅读:

非托管导出:无法编译程序集(stackoverflow,2010)

非托管导出/RGiesecke.DllExport.dll(stackoverflow,2010)

创建一个 C#可以使用 stdcall 在 Delphi 应用程序中导入的 DLL(stackoverflow,2009)

C# 项目模板非托管导出Robert Giesecke,2010)- msbuild、vs2010 集成

IKVM.Reflection 更新:将静态托管方法导出为非托管 DLL 导出(单声道、 2011)

无需 C++/CLI 的 DLL 导出简单方法 (codeproject, 2009) -没有 64 位支持?

如何自动将 .NET 函数导出到非托管程序(codeproject,2006 年)- 支持64 位

有什么添加到 .Net 4.0 以支持“反向 P/Invoke”方案?(msdn 论坛,2010)- 代码清单

非托管代码可以包装托管方法(codeproject,2004)

潜在缺点:

反向 Pinvoke 的陷阱(非托管到托管代码回调)(msdn 博客,2006)

反向 P/Invoke 和异常(msdn 博客,2008)

Option 1: .NET InstallUtil (part of the normal .NET install)

Add a reference to System.Configuration.Install then drop something like this in your assembly:

[System.ComponentModel.RunInstaller(true)]
public class Sample : System.Configuration.Install.Installer
{
    public override void Uninstall(System.Collections.IDictionary savedState)
    {
        base.Uninstall(savedState);

        this.Context.LogMessage("This can be in a .dll.");
        // Do your thing...
    }
}

Then abuse the .NET InstallUtil:

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil /logtoconsole=false /logfile= /u [your assembly .dll here]

It may be a bit messy, especially without all the command line parameters disabling logging.

Option 2: use native rundll32 to inverse P/Invoke a static class method

2016 Edit: Now a Nuget package: UnmanagedExports (src)!

Unknown MSIL directives (msdn forum, 2007) links to a beta 1 release of the IL Assembly Language Programmer's Reference (10/3/2000, pg.74) which states:

If fromunmanaged is specified, the
runtime will automatically generate a
thunk that will convert the unmanaged
method call to a managed call, call
the method, and return the result to
the unmanaged environment.

There is more detail in Expert .NET 2.0 IL assembler and Inside Microsoft. NET IL Assembler. (Best search keywords: ilasm vtentry).

Related reading:

Unmanaged Exports: Cannot compile assembly (stackoverflow, 2010)

Unmanaged Exports / RGiesecke.DllExport.dll (stackoverflow, 2010)

Create a C# DLL That Can Be Imported in a Delphi App Using stdcall (stackoverflow, 2009)

C# Project Template for Unmanaged Exports (Robert Giesecke, 2010) - msbuild, vs2010 integration

IKVM.Reflection Update: export static managed methods as unmanaged DLL exports (mono, 2011)

Simple Method of DLL Export without C++/CLI (codeproject, 2009) - no 64-bit support?

How to Automate Exporting .NET Function to Unmanaged Programs (codeproject, 2006) - supports 64-bit

Has anything been added to .Net 4.0 to support "Reverse P/Invoke" scenarios? (msdn forum, 2010) - code listing

Unmanaged code can wrap managed methods (codeproject, 2004)

Potential drawbacks:

Gotchas with Reverse Pinvoke (unmanaged to managed code callbacks) (msdn blog, 2006)

Reverse P/Invoke and exception (msdn blog, 2008)

淡水深流 2024-10-26 23:27:32

查看Windows 附带的rundll32.exe 程序。如果您知道有关 dll 的某些信息,有时可以使用此程序来运行该 dll 中的代码。

Look into the the rundll32.exe program that ships with windows. If you know certain information about the dll, you can sometimes use this program to run code in that dll.

司马昭之心 2024-10-26 23:27:32

.NET 程序集可以是 在 PowerShell 中加载。 (嗯,PowerShell 是一个 EXE,但并不专门用于运行任何特定程序集中的代码)

A .NET Assembly can be loaded in PowerShell. (Well, PowerShell is an EXE, but not specialized for running code from any particular assembly)

小苏打饼 2024-10-26 23:27:32

您可以使用 rundll32.exe dll_name,method_name 启动进程并运行 dll 函数。在 C# 中,您可以执行以下操作:

myProcess = new Process {StartInfo = {FileName = "rundll32.exe", Arguments = "my_dll_name.dll,my_function_name"}};
myProcess.Start();

只需确保 DLL 在您的 PATH 中即可。如果您正在考虑运行某种类型的单元测试,请确保使用 TearDown / CleanUp 方法终止进程:myProcess.Kill(),否则 DLL 可能会保持加载状态,而您将无法能够在开发过程中覆盖它。您还应该查看 本文介绍了一个命令行应用程序,它将杀死所有持有 DLL 的进程并将其添加到您的预构建事件中:freelib.exe my_dll_name.dll

You can launch a process and run a dll function by using rundll32.exe dll_name,method_name. In C# you can do something like this:

myProcess = new Process {StartInfo = {FileName = "rundll32.exe", Arguments = "my_dll_name.dll,my_function_name"}};
myProcess.Start();

Just make sure the DLL is in your PATH. If you are thinking about running unit testing of some kind, make sure you kill the process on the TearDown / CleanUp method: myProcess.Kill(), otherwise the DLL may remain loaded and you won't be able to overwrite it as part of your development process. You should also look into this article for a command line application that will kill all processes holding your DLL and add it to your Pre-Build events: freelib.exe my_dll_name.dll

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