是否有可能执行一个.NET的DLL没有一个exe加载它?有可能、加载、DLL、NET

2023-09-03 00:22:54 作者:只想征服

我很好奇,如果有一种方法,而无需创建一个.exe它以执行一个新的进程的静态.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,这是不可能的,原生的Win32 / 64的DLL。如何.NET的DLL组件?

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

更新:我忘了提,我主要是在编程这样做有兴趣的(从C#code,是具体的)

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

谢谢!

结论:虽然没有人敢拼出来,答案似乎都倾向于不。人们需要开始通过常规方式(EXE,PowerShell的,等等),一个过程,然后诱使该进程加载DLL,并在执行code。我想我被错误地希望管理的DLL能够更多的。

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!

推荐答案

刚开始PowerShell提示符。

Just start a PowerShell prompt.

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

我看到你想要这从C#

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 });

这应该让你开始。

我并不确切地知道你的意思在一个新的进程,但它不应该是很难换到一个.EXE /的.ps1,您可以用命令行上的一些选项启动此。

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.

所以,你没有创造您想要调用DLL的每一个新的.exe文件。

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

但是,如果你想开始一个新的进程,你应该开始一个新的进程,这通常通过启动一个新的.exe文件完成的。

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