DllImport使用的一点整理

用C#调用DLL的方法进行介绍。首先,您需要了解什么是托管,什么是非托管。一般可以认为:非托管代码主要是基于win 32平台开发的DLL,activeX的组件,托管代码是基于.net平台开发的。

静态调用: 
DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息。
DllImport属性应用于方法,要求最少要提供包含入口点的dll的名称。
DllImport 必须要知道dll文件路径绝对路径才能加载调用成功。并且文件路径字符串是常量型的,不能使用Mappath函数修改。
DllImport的定义如下:

(一)调用DLL中的非托管函数一般方法
首先,应该在C#语言源程序中声明外部方法,其基本形式是:
[DLLImport(“DLL文件”)]
修饰符 extern 返回变量类型 方法名称 (参数列表)
其中:
DLL文件:包含定义外部方法的库文件。
修饰符: 访问修饰符,除了abstract以外在声明方法时可以使用的修饰符。
返回变量类型:在DLL文件中你需调用方法的返回变量类型。
方法名称:在DLL文件中你需调用方法的名称。
参数列表:在DLL文件中你需调用方法的列表。
注意:需要在程序声明中使用System.Runtime.InteropServices命名空间。
DllImport只能放置在方法声明上。
DLL文件必须位于程序当前目录或系统定义的查询路径中(即:系统环境变量中Path所设置的路径)。
返回变量类型、方法名称、参数列表一定要与DLL文件中的定义相一致。

若要使用其它函数名,可以使用EntryPoint属性设置,如:

[DllImport("user32.dll", EntryPoint="MessageBoxA")]
static extern int MsgBox(int hWnd, string msg, string caption, int type);

DllImport会按照顺序自动去寻找的地方:
1、exe所在目录 ;
2、System32目录 ;
3、环境变量目录;

所以只需要你把引用的DLL 拷贝到这三个目录下 就可以不用写路径了 或者可以这样server.MapPath(.\bin\*.dll)web中的,同时也是应用程序中的 后来发现用[DllImport(@"C:\OJ\Bin\Judge.dll")]这样指定DLL的绝对路径就可以正常装载。 


(二)动态装载、调用DLL中的非托管函数
        ///<summary>
        /// API LoadLibrary
        ///</summary>
        [DllImport("Kernel32")]
        private static extern int LoadLibrary(String funcname);

        ///<summary>
        /// API GetProcAddress
        ///</summary>
        [DllImport("Kernel32")]
        private static extern IntPtr GetProcAddress(int handle, String funcname);

        ///<summary>
        /// API FreeLibrary
        ///</summary>
        [DllImport("Kernel32")]
        private static extern int FreeLibrary(int handle);

         private delegate int DelegateCallPeople(Byte[] in_str, out IntPtr pValue);
         //int DelegateCallPeople(Byte[] in_str, out IntPtr pValue)   C里的函数


            int hModule = LoadLibrary(dllFilePlain);
            if (hModule == 0)
            {
                Log.Error("路径下dll是否存在?" + dllFilePlain);
            }

            IntPtr intPtr = GetProcAddress(hModule, "CallPeople");   //"CallPeople"  C里的函数名
            if (intPtr == IntPtr.Zero)
            {
                    Log.ThrowException("未加载");
            }
            DelegateCallTax dCallPeople = (DelegateCallTax)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(DelegateCallTax));
            if (dCallPeople  == null)
            {
                Log.ThrowException("调用函数委托不能是空");
            }
            int ud = dCallPeople(in_str, out pValue);         //调用函数
         

            hModule = FreeLibrary(hModule);

原文地址:https://www.cnblogs.com/panjun/p/2429919.html