6

I have created a C# DLL that has some forms in it. ( I needed it to be a DLL, not a Windows Application.) How can I run it as a Windows App? Should I create another app and load it? How? What do I need to learn to do that? please let me know if I should explain more about my question.

Bohn
  • 26,091
  • 61
  • 167
  • 254

4 Answers4

7

If you're using VS 2008:

First, create a Windows Forms Application project. You can delete the default form that's created (Form1.cs) if you don't plan to use it.

In the Solution Explorer, right-click on the References and select Add Reference. This is the point where you add your custom designed C# DLL.

Now open Program.cs, and in make the following change:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ****your DLL namespace here****
namespace WindowsFormsApplication2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new [****your startup form (from the DLL) here****]);
        }
    }
}

If the DLL contains disconnected forms, you'll probably need to add a class in the winforms project to coordinate the forms behavior.

code4life
  • 15,655
  • 7
  • 50
  • 82
  • " You can delete the default form that's created (Form1.cs) if you don't plan to use it." ...nice, I did not like to have a dummy form that I am not actually using it. – Bohn May 12 '10 at 20:41
4

You can add forms to your DLL, then make a public static function in the DLL that calls Application.Run with a form.

You can then call this public static method from a C# Application project (after adding a reference to the DLL).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • so In my new C# App, I should have a dummy form that all it does is a call to that public static function in my DLL, right? I want This new C# to be nothing more than a wrapper to run that DLL – Bohn May 12 '10 at 20:05
  • 2
    No, in the new app in the program.cs replace the `Application.Run(new Form1());` with `Application.Run(new MyDllNamespace.MyDllMainForm());` Do not forget to add a reference to the dll in your project references. – Scott Chamberlain May 12 '10 at 20:07
4

You can launch it with RunDll32 however you may need to tweek the dll a bit before it will work. You may need to put a Application.Run in the entry point. this way you do not need to compile another entire application to use it.

the below code is untested but I think it should work.

public static void myDllEntryPoint()
{
     Application.run(new MyFormInDll());   
}

Run your application as

rundll32.exe myDll.dll,myDllEntryPoint
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
2

In VS2022 (.NET 6.0):

  1. Set your DLL project to be a Class Library in it's Properties.
  2. Right click on your EXE project, and add a Project Reference.
  3. Choose the one in that list.

To open the form when already initialized:

DLLProject.FormClass myForm = new DLLProject.FormClass();
myForm.Show(); // or myForm.ShowDialog(), whichever suits you best

To open the form when not initialized (in Program.cs):

Application.Run(new DLLProject.FormClass());

Remember to delete the Program.cs in the DLL project, otherwise it won't compile.

TheTank20
  • 89
  • 1
  • 6