Can a C# .dll assembly contain an entry point?
Asked Answered
P

3

20

My goal is to create an executable that will start a shadow copied application. The trick is, I want this starter program to have no external dependencies and not have to contain any knowledge about the program it has to start.

I also want it to be the only executable in the directory. In other words, I want it to "run" a .dll assembly not an .exe assembly. (I can require that the name of the .dll file being loaded into a new AppDomain be the same everytime, like Main.dll or something like that.)

It looked like AppDomain.ExecuteAssembly would do exactly what I wanted. It says it will start execution at the "entry point specified in the .NET Framework header.".

When I try to use that function I get the error "Entry point not found in assembly 'DllApp'".

The starter program I have, just trying to run the assembly:

static void Main()
{
    AppDomain domain = AppDomain.CreateDomain( "DllApp" );
    domain.ExecuteAssembly( "DllApp.dll" );
}

The application code, in a .dll file, with a default entry point:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault( false );
        Application.Run( new Form1() );
    }
}

This page on Main() functions says that "Libraries and services do not require a Main method as an entry point". It doesn't say they cannot have a default entry point either.

I have tried all the various permutations of public/private static void main, an int return type, string[] args as arguments, with a namespace, no namespace, static/non-static class, etc.

I was able to change my code to inherit from MarshalByRefObject and then use CreateInstance to create an object, but that seems like it will more tightly couple the starter to the program it is supposed to start. If I could use ExecuteAssembly, the application being started would just need a static void Main, and that is really simple and hard to mess up.

Is it possible for a .dll assembly to have a default entry point, and for ExecuteAssembly to find it, or do I just have to resign myself to going another route?

Pathan answered 6/3, 2012 at 3:35 Comment(1)
It does not make sense to have an entry point for a .NET assembly (dll) as by itself is not launched as a process.Dedans
F
27

You can compile a .NET app as an exe (which is an assembly) and rename it to a .DLL and it will act as a normal .NET .dll assembly. It will then have your Entry point.

Fortenberry answered 6/3, 2012 at 3:41 Comment(1)
This worked perfectly without having to change anything else. Wonder why I didn't think of it... Thanks Keith.Pathan
B
1

I found the advice not so easy to follow. After some experimenting this is how I came to success:

I created a console application with a simple main and included the rest of the code from my original DLL. Below is a simplified program which includes a DLL:

namespace FIT.DLLTest
{
  public class DLLTest
  {
    [STAThread]
    static void Main(string[] args)
    {
      int a = 1;
    }

    public DLLTest()
    {
      int b = 17;
    }

    public int Add(int int1, int int2)
    {
      return int1 + int2;
    }
  }
}

After compilation I renamed the generated .exe to a .DLL.

In the mother program, which uses the DLL I first added the DLLTest.dll as a Reference, then I added the code to execute the DLL.

namespace TestDLLTest
{
  class TestDLLTest
  {
    static void Main(string[] args)
    {
      AppDomain domain = AppDomain.CreateDomain( "DLLTest" );
      domain.ExecuteAssembly( "DllTest.dll" );

      DLLTest dt = new DLLTest();
      int res2 = dt.Add(6, 8);
      int a = 1;
    }
  }
}

Violà I could execute and add a breakpoint in the DLLTest.Main method and see that I could invoke the Main of DLLTest. Thanks for the discussion folks!

Buffington answered 18/2, 2014 at 17:10 Comment(0)
A
-4

Short answer is no. a .DLL file is a dynamically linked library which is code called by another library. I guess what Keith said would technically work.. you could probably rename it to what ever you want even .txt or .pdf as long as you start the application in the proper manner. The main question I have is this; What are you trying to do?? Unless you're trying to write malware why would you want to do this? Not that I condone the writing of malware for bad purposes but I know people like to experiment in their own lab so I for one don't condemn it. If you're writing malware something like c++, c or assembler might be a better bet I guess C# could get the job done but meh...

Alrick answered 6/3, 2012 at 4:18 Comment(4)
point is with my answer is that the compiler generates either dlls or exes, but both are assemblies, its just exes are assemblies with an entry point defined, which is what he was asking for.Fortenberry
Nope, no malware writing here. I'm trying to create a small starter program to shadow copy my main application. Then I can easily update my main application without having to jump through other hoops of separate executables for the update, etc. I just wanted the starter to be completely decoupled if possible, then I could use it with all of my applications.Pathan
"Unless you're trying to write malware " LOL!!Outsole
The 'normal' win32 dll's have entrypoints to initialize the library when a process attaches to it, or finalize when all processes have unloaded. This is normal practice, so accusations of malware are, imo, out of order.Sinusoid

© 2022 - 2024 — McMap. All rights reserved.