/// <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文件 }