Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

assemblies - Is it possible to execute a .NET dll without an exe to load it?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...