从c#方法,如何调用和运行一个DLL,其中DLL名称来自一个字符串变量?

时间:2021-11-07 23:41:43

I am new to C#.NET. I am writing a method where I need to call and run a DLL file, where the DLL file name comes from a String variable-

我是c#。net的新手。我正在编写一个方法,在这个方法中,我需要调用并运行一个DLL文件,其中DLL文件名来自一个String变量-

String[] spl;

String DLLfile = spl[0];

How do I import this DLL and call a function from the DLL to get the return value? I tried the the following way..

如何从DLL中导入这个DLL并调用一个函数来获取返回值?我尝试了以下方法

String DLLfile = "MyDLL.dll";

[DllImport(DLLfile, CallingConvention = CallingConvention.StdCall)]

But it did not worked, as The string should be in 'const string' type and 'const string' does not support variables. Please help me with detail procedure. Thanks.

但它不起作用,因为字符串应该在'const string'类型中,而'const string'不支持变量。请帮我办理详细的手续。谢谢。

2 个解决方案

#1


5  

For native DLLs you can create the following static class:

对于本机dll,您可以创建以下静态类:

internal static class NativeWinAPI
{
    [DllImport("kernel32.dll")]
    internal static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    internal static extern bool FreeLibrary(IntPtr hModule);

    [DllImport("kernel32.dll")]
    internal static extern IntPtr GetProcAddress(IntPtr hModule,
        string procedureName);
}

And then use it as follows:

然后使用如下:

// DLLFileName is, say, "MyLibrary.dll"
IntPtr hLibrary = NativeWinAPI.LoadLibrary(DLLFileName);

if (hLibrary != IntPtr.Zero) // DLL is loaded successfully
{
    // FunctionName is, say, "MyFunctionName"
    IntPtr pointerToFunction = NativeWinAPI.GetProcAddress(hLibrary, FunctionName);

    if (pointerToFunction != IntPtr.Zero)
    {
        MyFunctionDelegate function = (MyFunctionDelegate)Marshal.GetDelegateForFunctionPointer(
            pointerToFunction, typeof(MyFunctionDelegate));
        function(123);
    }

    NativeWinAPI.FreeLibrary(hLibrary);
}

Where MyFunctionDelegate is a delegate. E.g.:

MyFunctionDelegate是委托。例如:

delegate void MyFunctionDelegate(int i);

#2


3  

You can use LoadAssembly method , and CreateInstance method in order to invoke method

您可以使用LoadAssembly方法和CreateInstance方法来调用方法。

        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance. 
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);

#1


5  

For native DLLs you can create the following static class:

对于本机dll,您可以创建以下静态类:

internal static class NativeWinAPI
{
    [DllImport("kernel32.dll")]
    internal static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    internal static extern bool FreeLibrary(IntPtr hModule);

    [DllImport("kernel32.dll")]
    internal static extern IntPtr GetProcAddress(IntPtr hModule,
        string procedureName);
}

And then use it as follows:

然后使用如下:

// DLLFileName is, say, "MyLibrary.dll"
IntPtr hLibrary = NativeWinAPI.LoadLibrary(DLLFileName);

if (hLibrary != IntPtr.Zero) // DLL is loaded successfully
{
    // FunctionName is, say, "MyFunctionName"
    IntPtr pointerToFunction = NativeWinAPI.GetProcAddress(hLibrary, FunctionName);

    if (pointerToFunction != IntPtr.Zero)
    {
        MyFunctionDelegate function = (MyFunctionDelegate)Marshal.GetDelegateForFunctionPointer(
            pointerToFunction, typeof(MyFunctionDelegate));
        function(123);
    }

    NativeWinAPI.FreeLibrary(hLibrary);
}

Where MyFunctionDelegate is a delegate. E.g.:

MyFunctionDelegate是委托。例如:

delegate void MyFunctionDelegate(int i);

#2


3  

You can use LoadAssembly method , and CreateInstance method in order to invoke method

您可以使用LoadAssembly方法和CreateInstance方法来调用方法。

        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance. 
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);