你的位置:首页 > 软件开发 > ASP.net > C#调用C++ Dll

C#调用C++ Dll

发布时间:2015-08-16 23:00:08
现在项目基本都是旁边C++的哥们做好dll扔给我,然后我调用。好久之前晚上down了一份c#调用c++dll的方法,出处早已经遗忘。闲来无事,放上来好了。原作者看到后可以留言,我会把您链接放上的,帮了我很多!!! 1 using System; 2 using System.Co ...

现在项目基本都是旁边C++的哥们做好dll扔给我,然后我调用。好久之前晚上down了一份c#调用c++dll的方法,出处早已经遗忘。闲来无事,放上来好了。原作者看到后可以留言,我会把您链接放上的,帮了我很多!!!

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Reflection; 5 using System.Reflection.Emit; 6 using System.Runtime.InteropServices; 7 using System.Text; 8  9 namespace TEDS_App 10 { 11   public enum ModePass 12   { 13     ByValue = 0x0001, 14     ByRef = 0x0002 15   } 16   public class FaultFunc 17   { 18     [DllImport("kernel32.dll")] 19     static extern IntPtr LoadLibrary(get='_blank'>string lpFileName); 20     [DllImport("kernel32.dll")] 21     static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); 22     [DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)] 23     static extern bool FreeLibrary(IntPtr hModule); 24     private IntPtr hModule = IntPtr.Zero; 25     private IntPtr farProc = IntPtr.Zero; 26     public void LoadDll(string lpFileName) 27     { 28       hModule = LoadLibrary(lpFileName); 29       if (hModule == IntPtr.Zero) 30       { 31         throw (new Exception("没有找到:" + lpFileName + ".")); 32       } 33     } 34     public void LoadDll(IntPtr HMODULE) 35     { 36       if (HMODULE == IntPtr.Zero) 37       { 38         throw (new Exception("所传入的函数库模块的句柄为空")); 39       } 40       hModule = HMODULE; 41     } 42     public void LoadFun(string lpProcName) 43     { 44       if (hModule == IntPtr.Zero) 45       { 46         throw (new Exception("函数库模块的句柄为空,确保已进行加载dll操作")); 47       } 48       farProc = GetProcAddress(hModule, lpProcName); 49       if (farProc == IntPtr.Zero) 50       { 51         throw (new Exception("没有找到:" + lpProcName + "这个函数的入口点")); 52       } 53     } 54     public void LoadFun(string lpFileName, string lpProcName) 55     { 56       hModule = LoadLibrary(lpFileName); 57       if (hModule == IntPtr.Zero) 58       { 59         throw (new Exception("没有找到:" + lpFileName + ".")); 60       } 61       farProc = GetProcAddress(hModule, lpFileName); 62       if (farProc == IntPtr.Zero) 63       { 64         throw (new Exception("没有找到:" + lpProcName + "这个函数的入口点")); 65       } 66     } 67     public void UnLoadDll() 68     { 69       FreeLibrary(hModule); 70       hModule = IntPtr.Zero; 71       farProc = IntPtr.Zero; 72     } 73     public object Invoke(object[] ObjArray_Parameter, Type[] TypeArray_parameterType, ModePass[] ModePassArray_Parameter, Type Type_Return) 74     { 75       if (hModule == IntPtr.Zero) 76         throw (new Exception("函数库模块的句柄为空,请确保进行了LoadLll操作")); 77       if (farProc == IntPtr.Zero) 78         throw (new Exception("函数指针为空,请确保已进行LoadFun操作")); 79       if (ObjArray_Parameter.Length != ModePassArray_Parameter.Length) 80         throw (new Exception("参数个数及其传递方式的个数不匹配")); 81       AssemblyName MyAssemblyName = new AssemblyName(); 82       MyAssemblyName.Name = "InvokeFun"; 83       AssemblyBuilder MyAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(MyAssemblyName, AssemblyBuilderAccess.Run); 84       ModuleBuilder MyModuleBuilder = MyAssemblyBuilder.DefineDynamicModule("InvokeDll"); 85       MethodBuilder MyMethodBuilder = MyModuleBuilder.DefineGlobalMethod("FaultFun", MethodAttributes.Public | MethodAttributes.Static, Type_Return, TypeArray_parameterType); 86       ILGenerator IL = MyMethodBuilder.GetILGenerator(); 87       int i; 88       for (i = 0; i < ObjArray_Parameter.Length; i++) 89       { 90         switch (ModePassArray_Parameter[i]) 91         { 92           case ModePass.ByValue: 93             IL.Emit(OpCodes.Ldarg, i); 94             break; 95           case ModePass.ByRef: 96             IL.Emit(OpCodes.Ldarga, i); 97             break; 98           default: 99             throw (new Exception("第" + (i + 1).ToString() + "个参数没有给定正确的传递方式"));100         }101       }102       if (IntPtr.Size == 4)103       {104         IL.Emit(OpCodes.Ldc_I4, farProc.ToInt32());105       }106       else if (IntPtr.Size == 8)107       {108         IL.Emit(OpCodes.Ldc_I8, farProc.ToInt64());109       }110       else111       {112         throw new PlatformNotSupportedException();113       }114       IL.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, Type_Return, TypeArray_parameterType);115       IL.Emit(OpCodes.Ret);116       MyModuleBuilder.CreateGlobalFunctions();117       MethodInfo MyMethodInfo = MyModuleBuilder.GetMethod("FaultFun");118       return MyMethodInfo.Invoke(null, ObjArray_Parameter);119     }120     public object Invoke(IntPtr IntPtr_Function, object[] ObjArray_Parameter, Type[] TypeArray_ParameterType, ModePass[] ModePassArray_Parameter, Type Type_Return)121     {122       if (hModule == IntPtr.Zero)123         throw (new Exception("函数库模块的句柄为空,请确保已进行LoadDll操作"));124       if (IntPtr_Function == IntPtr.Zero)125         throw (new Exception("函数指针IntPtr_Function为空"));126       farProc = IntPtr_Function;127       return Invoke(ObjArray_Parameter, TypeArray_ParameterType, ModePassArray_Parameter, Type_Return);128     }129   }130 131 }

原标题:C#调用C++ Dll

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录