Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 949
aoven Avatar asked Mar 02 '11 15:03

aoven


People also ask

Can a DLL be executed?

dll cannot contain any spaces or it will fail to execute. Know the correct entry point of your . dll file before attempting to run it through Rundll32.

How does .NET DLL work?

A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications. The only thing we need to do is to add the reference/import the DLL File.


2 Answers

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.

like image 152
GvS Avatar answered Sep 27 '22 18:09

GvS


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)

like image 33
user423430 Avatar answered Sep 27 '22 19:09

user423430