動態(tài)加載DLL需要使用Windows API函數(shù):LoadLibrary,、GetProcAddress以及FreeLibrary,。我們可以使用DllImport在C#中使用這三個函數(shù)。
[DllImport("Kernel32")] public static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")] public static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")] public static extern int FreeLibrary(int handle);
當(dāng)我們在C++中動態(tài)調(diào)用Dll中的函數(shù)時,,我們一般的方法是: 假設(shè)DLL中有一個導(dǎo)出函數(shù),,函數(shù)原型如下: BOOL __stdcall foo(Object &object, LPVOID lpReserved);
1,、首先定義相應(yīng)的函數(shù)指針: typedef BOOL (__stdcall *PFOO)(Object &object, LPVOID lpReserved);
2,、調(diào)用LoadLibrary加載dll: HINSTANCE hInst = ::LoadLibraryW(dllFileName);
3,、調(diào)用GetProcAddress函數(shù)獲取要調(diào)用函數(shù)的地址: PFOO foo = (PFOO)GetProcAddress(hInst,"foo"); if(foo == NULL) { FreeLibrary(hInst); return false; }
4、調(diào)用foo函數(shù): BOOL bRet = foo(object,(LPVOID)NULL);
5,、使用完后應(yīng)釋放DLL: FreeLibrary(hInst);
那么在C#中應(yīng)該怎么做呢,?方法基本上一樣,我們使用委托來代替C++的函數(shù)指針,通過.NET Framework 2.0新增的函數(shù)GetDelegateForFunctionPointer來得到一個委托的實(shí)例:
下面封裝了一個類,,通過該類我們就可以在C#中動態(tài)調(diào)用Dll中的函數(shù)了:
public class DLLWrapper { ///<summary> /// API LoadLibrary ///</summary> [DllImport("Kernel32")] public static extern int LoadLibrary(String funcname);
///<summary> /// API GetProcAddress ///</summary> [DllImport("Kernel32")] public static extern int GetProcAddress(int handle, String funcname);
///<summary> /// API FreeLibrary ///</summary> [DllImport("Kernel32")] public static extern int FreeLibrary(int handle);
///<summary> ///通過非托管函數(shù)名轉(zhuǎn)換為對應(yīng)的委托, by jingzhongrong ///</summary> ///<param name="dllModule">通過LoadLibrary獲得的DLL句柄</param> ///<param name="functionName">非托管函數(shù)名</param> ///<param name="t">對應(yīng)的委托類型</param> ///<returns>委托實(shí)例,,可強(qiáng)制轉(zhuǎn)換為適當(dāng)?shù)奈蓄愋?lt;/returns> public static Delegate GetFunctionAddress(int dllModule, string functionName, Type t) { int address = GetProcAddress(dllModule, functionName); if (address == 0) return null; else return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t); }
///<summary> ///將表示函數(shù)地址的IntPtr實(shí)例轉(zhuǎn)換成對應(yīng)的委托, by jingzhongrong ///</summary> public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t) { if (address == IntPtr.Zero) return null; else return Marshal.GetDelegateForFunctionPointer(address, t); }
///<summary> ///將表示函數(shù)地址的int轉(zhuǎn)換成對應(yīng)的委托,,by jingzhongrong ///</summary> public static Delegate GetDelegateFromIntPtr(int address, Type t) { if (address == 0) return null; else return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t); } }
通過這個類,,我們這樣調(diào)用DLL:
1、聲明相應(yīng)的委托(正確聲明很重要,,否則不能調(diào)用成功,,后面有詳細(xì)介紹)。
2,、加載DLL: int hModule = DLLWrapper.LoadLibrary(dllFilePath); if (hModule == 0) return false;
3,、獲取相應(yīng)的委托實(shí)例: FOO foo = (FOO)DLLWrapper.GetFunctionAddress(hModule, "foo", typeof(FOO)); if (foo == null) { DLLWrapper.FreeLibrary(hModule); return false; }
4、調(diào)用函數(shù): foo(...);
5,、.NET并不能自動釋放動態(tài)加載的DLL,,因此我們在使用完DLL后應(yīng)該自己釋放DLL: DLLWrapper.FreeLibrary(hModule);
下面我們將就委托應(yīng)如何聲明進(jìn)行相應(yīng)的討論,在實(shí)際操作過程中,,我發(fā)現(xiàn)使用DllImport方法和動態(tài)調(diào)用方法兩者在C#中對DLL中函數(shù)原型的聲明是有些區(qū)別的,,下面我介紹動態(tài)調(diào)用中委托的聲明:
1、首先應(yīng)該注意的是,,C++中的類型和C#中類型的對應(yīng)關(guān)系,,比如C++中的long應(yīng)該對應(yīng)C#中的Int32而不是long,否則將導(dǎo)致調(diào)用結(jié)果出錯,。
2,、結(jié)構(gòu)的聲明使用StructLayout對結(jié)構(gòu)的相應(yīng)布局進(jìn)行設(shè)置,具體的請查看MSDN:
使用LayoutKind指定結(jié)構(gòu)中成員的布局順序,,一般可以使用Sequential: [StructLayout(LayoutKind.Sequential)] struct StructVersionInfo { public int MajorVersion; public int MinorVersion; } 另外,,如果單獨(dú)使用內(nèi)部類型沒有另外使用到字符串、結(jié)構(gòu),、類,,可以將結(jié)構(gòu)在C#中聲明為class: [StructLayout(LayoutKind.Sequential)] class StructVersionInfo { public int MajorVersion; public int MinorVersion; }
對應(yīng)C++中的聲明: typedef struct _VERSION_INFO { int MajorVersion; int MinorVersion; } VERSION_INFO, *PVERSION_INFO;
如果結(jié)構(gòu)中使用到了字符串,最好應(yīng)指定相應(yīng)的字符集: [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
部分常用的聲明對應(yīng)關(guān)系(在結(jié)構(gòu)中): C++:字符串?dāng)?shù)組 wchar_t Comments[120]; C#: [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 120)] public string Comments;
C++:結(jié)構(gòu)成員 VERSION_INFO ver; C# publicStructVersionInfo ver;
C++:函數(shù)指針聲明 PFOO pFoo; //具體聲明見文章前面部分 C#: publicIntPtr pFoo; //也可以為 public int pFoo; //不同的聲明方法可以使用上面DLLWrapper類的相應(yīng)函數(shù)獲取對應(yīng)的委托實(shí)例
如果在結(jié)構(gòu)中使用到了union,,那么可以使用FieldOffset指定具體位置,。
3、委托的聲明:
當(dāng)C++編寫的DLL函數(shù)需要通過指針傳出將一個結(jié)構(gòu):如以下聲明: void getVersionInfo(VERSION_INFO *ver); 對于在C#中聲明為class的結(jié)構(gòu)(當(dāng)VERSION_INFO聲明為class) delegate voidgetVersionInfo(VERSION_INFO ver); 如果結(jié)構(gòu)聲明為struct,,那么應(yīng)該使用如下聲明: delegate voidgetVersionInfo(refVERSION_INFO ver); 注意:應(yīng)該使用ref關(guān)鍵字,。
如果DLL函數(shù)需要傳入一個字符串,比如這樣: BOOL __stdcall jingzhongrong1(const wchar_t* lpFileName, int* FileNum); 那么使用委托來調(diào)用函數(shù)的時候應(yīng)該在C#中如下聲明委托: delegate bool jingzhongrong1( [MarshalAs(UnmanagedType.LPWStr)]String FileName, ref int FileNum); 注意:應(yīng)該使用[MarshalAs(UnmanagedType.LPWStr)]和String進(jìn)行聲明,。
如果要在DLL函數(shù)中傳出一個字符串,,比如這樣: void __stdcall jingzhongrong2( wchar_t* lpFileName, //要傳出的字符串 int* Length); 那么我們?nèi)缦侣暶魑校?nbsp; //使用委托從非托管函數(shù)的參數(shù)中傳出的字符串, //應(yīng)該這樣聲明,并在調(diào)用前為StringBuilder預(yù)備足夠的空間 delegate void jingzhongrong2( [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpFileName, ref int Length, ); 在使用函數(shù)前,,應(yīng)先為StringBuilder聲明足夠的空間用于存放字符串: StringBuilder fileName = new StringBuilder(FileNameLength); 參考: http://www./kf/201007/52562.html 參考: http://www./Articles/12121/Essential-P-Invoke 參考: http://www./Articles/339290/PInvoke-pointer-safety-Replacing-IntPtr-with-unsaf 參考: http://www./merlin/archive/2012/07/17/73424.aspx ============================== (轉(zhuǎn)載:http://blog.csdn.net/xqf222/article/details/5877795) //C++中的DLL函數(shù)原型為 //extern "C" __declspec(dllexport) bool 方法名一(const char* 變量名1, unsigned char* 變量名2) //extern "C" __declspec(dllexport) bool 方法名二(const unsigned char* 變量名1, char* 變量名2) //C#調(diào)用C++的DLL搜集整理的所有數(shù)據(jù)類型轉(zhuǎn)換方式,可能會有重復(fù)或者多種方案,自己多測試 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byte(unsigned char) ---- c#:System.Byte //c++:SHORT(short) ---- c#:System.Int16 //c++:WORD(unsigned short) ---- c#:System.UInt16 //c++:INT(int) ---- c#:System.Int16 //c++:INT(int) ---- c#:System.Int32 //c++:UINT(unsigned int) ---- c#:System.UInt16 //c++:UINT(unsigned int) ---- c#:System.UInt32 //c++:LONG(long) ---- c#:System.Int32 //c++:ULONG(unsigned long) ---- c#:System.UInt32 //c++:DWORD(unsigned long) ---- c#:System.UInt32 //c++:DECIMAL ---- c#:System.Decimal //c++:BOOL(long) ---- c#:System.Boolean //c++:CHAR(char) ---- c#:System.Char //c++:LPSTR(char *) ---- c#:System.String //c++:LPWSTR(wchar_t *) ---- c#:System.String //c++:LPCSTR(const char *) ---- c#:System.String //c++:LPCWSTR(const wchar_t *) ---- c#:System.String //c++:PCAHR(char *) ---- c#:System.String //c++:BSTR ---- c#:System.String //c++:FLOAT(float) ---- c#:System.Single //c++:DOUBLE(double) ---- c#:System.Double //c++:VARIANT ---- c#:System.Object //c++:PBYTE(byte *) ---- c#:System.Byte[] //c++:BSTR ---- c#:StringBuilder //c++:LPCTSTR ---- c#:StringBuilder //c++:LPCTSTR ---- c#:string //c++:LPTSTR ---- c#:[MarshalAs(UnmanagedType.LPTStr)] string //c++:LPTSTR 輸出變量名 ---- c#:StringBuilder 輸出變量名 //c++:LPCWSTR ---- c#:IntPtr //c++:BOOL ---- c#:bool //c++:HMODULE ---- c#:IntPtr //c++:HINSTANCE ---- c#:IntPtr //c++:結(jié)構(gòu)體 ---- c#:public struct 結(jié)構(gòu)體{}; //c++:結(jié)構(gòu)體 **變量名 ---- c#:out 變量名 //C#中提前申明一個結(jié)構(gòu)體實(shí)例化后的變量名 //c++:結(jié)構(gòu)體 &變量名 ---- c#:ref 結(jié)構(gòu)體 變量名 //c++:WORD ---- c#:ushort //c++:DWORD ---- c#:uint //c++:DWORD ---- c#:int //c++:UCHAR ---- c#:int //c++:UCHAR ---- c#:byte //c++:UCHAR* ---- c#:string //c++:UCHAR* ---- c#:IntPtr //c++:GUID ---- c#:Guid //c++:Handle ---- c#:IntPtr //c++:HWND ---- c#:IntPtr //c++:DWORD ---- c#:int //c++:COLORREF ---- c#:uint //c++:unsigned char ---- c#:byte //c++:unsigned char * ---- c#:ref byte //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] byte[] //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] Intptr
//c++:unsigned char & ---- c#:ref byte //c++:unsigned char 變量名 ---- c#:byte 變量名 //c++:unsigned short 變量名 ---- c#:ushort 變量名 //c++:unsigned int 變量名 ---- c#:uint 變量名 //c++:unsigned long 變量名 ---- c#:ulong 變量名 //c++:char 變量名 ---- c#:byte 變量名 //C++中一個字符用一個字節(jié)表示,C#中一個字符用兩個字節(jié)表示 //c++:char 數(shù)組名[數(shù)組大小] ---- c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 數(shù)組大小)] public string 數(shù)組名; ushort //c++:char * ---- c#:string //傳入?yún)?shù) //c++:char * ---- c#:StringBuilder//傳出參數(shù) //c++:char *變量名 ---- c#:ref string 變量名 //c++:char *輸入變量名 ---- c#:string 輸入變量名 //c++:char *輸出變量名 ---- c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 輸出變量名 //c++:char ** ---- c#:string //c++:char **變量名 ---- c#:ref string 變量名 //c++:const char * ---- c#:string //c++:char[] ---- c#:string //c++:char 變量名[數(shù)組大小] ---- c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=數(shù)組大小)] public string 變量名; //c++:struct 結(jié)構(gòu)體名 *變量名 ---- c#:ref 結(jié)構(gòu)體名 變量名 //c++:委托 變量名 ---- c#:委托 變量名 //c++:int ---- c#:int //c++:int ---- c#:ref int //c++:int & ---- c#:ref int //c++:int * ---- c#:ref int //C#中調(diào)用前需定義int 變量名 = 0; //c++:*int ---- c#:IntPtr //c++:int32 PIPTR * ---- c#:int32[] //c++:float PIPTR * ---- c#:float[] //c++:double** 數(shù)組名 ---- c#:ref double 數(shù)組名 //c++:double*[] 數(shù)組名 ---- c#:ref double 數(shù)組名 //c++:long ---- c#:int //c++:ulong ---- c#:int //c++:UINT8 * ---- c#:ref byte //C#中調(diào)用前需定義byte 變量名 = new byte(); //c++:handle ---- c#:IntPtr //c++:hwnd ---- c#:IntPtr //c++:void * ---- c#:IntPtr //c++:void * user_obj_param ---- c#:IntPtr user_obj_param //c++:void * 對象名稱 ---- c#:([MarshalAs(UnmanagedType.AsAny)]Object 對象名稱
//c++:char, INT8, SBYTE, CHAR ---- c#:System.SByte //c++:short, short int, INT16, SHORT ---- c#:System.Int16 //c++:int, long, long int, INT32, LONG32, BOOL , INT ---- c#:System.Int32 //c++:__int64, INT64, LONGLONG ---- c#:System.Int64 //c++:unsigned char, UINT8, UCHAR , BYTE ---- c#:System.Byte //c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t ---- c#:System.UInt16 //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT ---- c#:System.UInt32 //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG ---- c#:System.UInt64 //c++:float, FLOAT ---- c#:System.Single //c++:double, long double, DOUBLE ---- c#:System.Double
//Win32 Types ---- CLR Type //Struct需要在C#里重新定義一個Struct //CallBack回調(diào)函數(shù)需要封裝在一個委托里,,delegate static extern int FunCallBack(string str); //unsigned char** ppImage替換成IntPtr ppImage //int& nWidth替換成ref int nWidth //int*, int&, 則都可用 ref int 對應(yīng) //雙針指類型參數(shù),可以用 ref IntPtr //函數(shù)指針使用c++: typedef double (*fun_type1)(double); 對應(yīng) c#:public delegate double fun_type1(double); //char* 的操作c++: char*; 對應(yīng) c#:StringBuilder; //c#中使用指針:在需要使用指針的地方 加 unsafe //unsigned char對應(yīng)public byte /* * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg); * typedef void (*CALLBACKFUN1A)(char*, void* pArg); * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg); * 調(diào)用方式為 * [UnmanagedFunctionPointer(CallingConvention.Cdecl)] * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg); * * */
f you want to pass a byte array to native DLL as parameter, you can use the Intptr to do this, please check the demo below. //C++ API code: DEMODLL_API void TestArrayPara(BYTE * pArray, int nSize) { for (int i=0; i<nSize; i++) printf("%d\n", pArray[i]); } //C# code: class Class2 { [DllImport(@"DemoDll.dll")] public static extern void TestArrayPara(IntPtr pArray, int nSize); public static void Test() { Console.WriteLine("This is C# program"); byte[] array = new byte[16]; for (int i = 0; i < 16; i++) { array[i] = (byte)(i + 97); } //============================ int size = Marshal.SizeOf(array[0]) * array.Length; IntPtr pnt = Marshal.AllocHGlobal(size); try { // Copy the array to unmanaged memory. Marshal.Copy(array, 0, pnt, array.Length); } finally { // Free the unmanaged memory. // Marshal.FreeHGlobal(pnt); } //============================ TestArrayPara(pnt, array.Length); Marshal.FreeHGlobal(pnt); Console.WriteLine("End of app"); } } In addition to Mashal.Copy method, please refer to the following article. http://msdn.microsoft.com/en-us/library/ms146625.aspx
|