Launch Dll using C# program

前端 未结 9 758
你的背包
你的背包 2021-01-06 08:52

I have a C# form application...i created a Dll...now i want to launch that dll using this program. how do i do it?

#include 

typedef int (*         


        
相关标签:
9条回答
  • 2021-01-06 08:53

    You can add reference to that dll into your project.

    To add reference use following STEPS:

    1.Go to Project Menu or Solution Explorer
    2. add reference
    3. browse your dll
    4. OK

    0 讨论(0)
  • 2021-01-06 08:54

    Make your dll executable and after that use the Process class from diagnostics:

    http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

                Process myProcess = new Process();
    
                try
                {
                    myProcess.StartInfo.UseShellExecute = false;
                    // You can start any process, HelloWorld is a do-nothing example.
                    myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    // This code assumes the process you are starting will terminate itself. 
                    // Given that is is started without a window so you cannot terminate it 
                    // on the desktop, it must terminate itself or you can do it programmatically
                    // from this application using the Kill method.
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
    
    0 讨论(0)
  • 2021-01-06 08:58

    The terms launching and DLL are somewhat incompatible concepts. The operating system launches programs which are binaries that have a defined entry point: the main method. DLLs are better viewed as binaries which have multiple entry points in the form of APIs. Launching in this case would require the operating system to pick between these many entry points.

    Were you trying to use a particular object from a DLL? If so then try the following

    • Right click on the project in "Solution Explorer" and select "Add Reference"
    • Choose the "Browse" Tab
    • Navigate to the DLL in question and hit OK

    Now you will be able to use the types from the DLL within your project.

    MyOtherDLLNamespace.TheType local = ...
    
    0 讨论(0)
  • 2021-01-06 08:58

    Add the DLL as a reference to your form application. Then you'll be able to access the namespaces and classes in it from the application code.

    0 讨论(0)
  • 2021-01-06 09:09

    In your C# application, add a reference to the assembly you created (the DLL). You can do this through the solution explorer window - right click on references, and say "Add Reference..." and choose your DLL.

    At that point, you can add "using YourDllNamespace;" at the top of your C# form's class, and use the types defined within the DLL as needed.

    0 讨论(0)
  • 2021-01-06 09:10

    To do what your code example does, use the following C# code:

    public static class DllHelper
    {
        [System.Runtime.InteropServices.DllImport("Dll1.dll")]
        public static extern int function1();
    }
    
    private void buttonStart_Click(object sender, EventArgs e)
    {
        try
        {
            DllHelper.function1();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }      
    

    The above example is a C# program that calls a function in non .NET based DLL. The example below is a C# program that calls a function in a .NET based DLL.

    try
    {
        System.Reflection.Assembly dll1 = System.Reflection.Assembly.LoadFile("Dll1.dll");
        if (dll1 != null)
        {
            object obj = dll1.CreateInstance("Function1Class");
            if (obj != null)
            {
                System.Reflection.MethodInfo mi = obj.GetType().GetMethod("function1");
                mi.Invoke(obj, new object[0]);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    

    Is any of the the 2 examples what you want? Or are you trying to call a C# function in a DLL from your example code?

    0 讨论(0)
提交回复
热议问题