Launch Dll using C# program

前端 未结 9 757
你的背包
你的背包 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 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?

提交回复
热议问题