/// <summary> /// 加载dll文件,并返回数据 /// </summary> /// <param name="funcname">调用dll的函数名</param> /// <param name="k">自定义数组长度</param> /// <param name="userCode">传入的数据</param> /// <returns></returns> private byte[] loadDllFile(string funcname, int k, string userCode) { //int hModule = DllInvoke.LoadLibrary(@"D:\git\wpfPlayerUI\Wpf实例\bin\Release\USBJOY_DLL.dll"); //IntPtr intPtr = DllInvoke.GetProcAddress(hModule, "GET_2DOF"); //1. 动态加载C++ Dll int hModule = DllInvoke.LoadLibrary(@"md5.dll"); //if (hModule == 0) return null; //2. 读取函数指针SQ_GET_2DOF IntPtr intPtr = DllInvoke.GetProcAddress(hModule, funcname); //3. 将函数指针封装成委托 PSQ_GET_DATA addFunction = (PSQ_GET_DATA)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(PSQ_GET_DATA)); //4. 测试 byte[] aa = new byte[k]; // this.Dispatcher.Invoke(new Action(() => {}));使用同步的方法来使用函数委托 this.Dispatcher.Invoke(new Action(() => { addFunction(userCode, ref aa[0]); })); //DllInvoke.FreeLibrary(hModule);//释放Dll文件 return(aa); }
/// <summary> /// 动态调用Dll以及DLL里面的方法 /// </summary> /// <param name="funcname"></param> /// <param name="k"></param> private byte[] USBJOY_DLL(string funcname, int k) { //int hModule = DllInvoke.LoadLibrary(@"D:\dll\usbjoy-alldof\vs2010\x64\Release\USBJOY_DLL.dll"); //IntPtr intPtr = DllInvoke.GetProcAddress(hModule, "GET_2DOF"); //1. 动态加载C++ Dll int hModule = DllInvoke.LoadLibrary(@"../../Release/USBJOY_DLL.dll"); if (hModule == 0) { return(null); } //2. 读取函数指针SQ_GET_2DOF IntPtr intPtr = DllInvoke.GetProcAddress(hModule, funcname); //3. 将函数指针封装成委托 PSQ_GET_2DOF addFunction = (PSQ_GET_2DOF)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(PSQ_GET_2DOF)); //4. 测试 byte[] a = new byte[k]; // this.Dispatcher.Invoke(new Action(() => {}));使用同步的方法来使用函数委托 this.Dispatcher.Invoke(new Action(() => { addFunction(ref a[0]); })); //DllInvoke.FreeLibrary(hModule);//释放Dll文件 return(a); }
private void USBJOY_DLL(string funcname, int k) { //1. 动态加载C++ Dll int hModule = DllInvoke.LoadLibrary(@"../../Release/USBJOY_DLL.dll"); if (hModule == 0) { return; } //2. 读取函数指针SQ_GET_2DOF IntPtr intPtr = DllInvoke.GetProcAddress(hModule, funcname); //3. 将函数指针封装成委托 PSQ_GET_2DOF addFunction = (PSQ_GET_2DOF)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(PSQ_GET_2DOF)); //4. 测试 byte[] a = new byte[k]; addFunction(ref a[0]); for (int i = 0; i < a.Length; i++) { Console.WriteLine(i + ":" + a[i]); } //DllInvoke.FreeLibrary(hModule); //释放Dll文件 }