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

╄→гoц情女王★ 提交于 2019-12-02 21:50:52

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.

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!

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!