1 动态库的相互调用 1.1 C#调用C++ dll步骤(只能导出方法): 1. c++建立空项目->源文件文件夹中添加cpp文件和函数 2. c++属性设置中,配置类型设置为动态库dll,公共语言运行时支持改为/clr 3. c#引用c++的dll 4. c#声明c++的方法,并添加 DllImport特性 5. c#工程属性设置为:目标平台x86 6. 注意方法的类型匹配 7. 引发PInvokeStackImbalance异常:注意:C++的"_declspec"和C#的“CallingConvention=CallingConvention.Cdecl” 另外,可以通过VS的异常窗口,取消掉对 PInvokeStackImbalance异常的检测: 点击VS的“调试 - 异常”,打开异常窗口,展开选择“Managed Debugging Assistants\PInvokeStackImbalance”,去掉对应的“引发”可选框。 1.2 c++调用 c# dll的步骤(可直接使用C#类): 1. 创建c++控制台应用程序 2. 拷贝c# dll到c++工程根目录 3. 工程属性->配置->常规->公共语言运行时支持->clr 工程属性->配置->c/c++常规->调试信息格式->zi 工程属性->配置->c/c++常规->公共语言运行时支持->clr ? 1 2 #using "CSharpDllProject.dll" using namespace CSharpDllProject; 1.3 c#调用c++类步骤(c++/cli,可直接使用C++类) c++/cli简介:C++/CLI标准是基于Microsoft提交的标准C++与通用语言基础结构(Common Language Infrastructure)结合的技术 1.使用c++/cli语法对标准c++类进行包装(可采用聚合模式,引用标准c++类,实现所有标准c++的方法) 2.c#引用c++ dll后,可直接new出一个 c++/cli创建的托管类对象 2 DEMOS 2.1 BASIC 新建一个classlibrary,包含两个类class1和class2,这两个类中分别有一个方法,都是返回一个字符串,代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace mydll { public class Class1 {public Class1() { }public string sayhello() { return "hello,word!"; } } public class Class2 {public Class2() { }public string saybeautiful() { return "beautiful,very good!"; } } } 在编译完成后会生成一个mydll.dll动态链接库,然后新建一个winform项目: private void button1_Click(object sender, EventArgs e) { string path = @"D:\123\mydll\mydll\bin\Debug\mydll.dll"; //Byte[] byte1 = System.IO.File.ReadAllBytes(path);//也是可以的 //Assembly assem = Assembly.Load(byte1); Assembly assem = Assembly.LoadFile(path); //string t_class = "mydll.Class1";//理论上已经加载了dll文件,可以通过命名空间加上类名获取类的类型,这里应该修改为如下: //string t_class = "mydll.Class1,mydll";//如果你想要得到的是被本工程内部的类,可以“命名空间.父类……类名”;如果是外部的,需要在后面加上“,链接库名”; //再次感谢thy38的帮助。 //Type ty = Type.GetType(t_class);//这儿在调试的时候ty=null,一直不理解,望有高人可以解惑 Type[] tys = assem.GetTypes();//只好得到所有的类型名,然后遍历,通过类型名字来区别了 foreach (Type ty in tys)//huoquleiming {if (ty.Name == "Class1") { ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes);//获取不带参数的构造函数 object magicClassObject = magicConstructor.Invoke(new object[] { });//这里是获取一个类似于类的实例的东东 //object magicClassObject = Activator.CreateInstance(t);//获取无参数的构造实例还可以通过这样 MethodInfo mi = ty.GetMethod("sayhello"); object aa=mi.Invoke(magicClassObject, null); MessageBox.Show(aa.ToString());//这儿是执行类class1的sayhello方法}if (ty.Name == "Class2") { ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes);//获取不带参数的构造函数,如果有构造函数且没有不带参数的构造函数时,这儿就不能这样子啦 object magicClassObject = magicConstructor.Invoke(new object[] { }); MethodInfo mi = ty.GetMethod("saybeautiful"); object aa = mi.Invoke(magicClassObject, null);//方法有参数时,需要把null替换为参数的集合 MessageBox.Show(aa.ToString());} } //AppDomain pluginDomain = (pluginInstanceContainer[key] as PluginEntity).PluginDomain; //if (pluginDomain != null) //{ // AppDomain.Unload(pluginDomain); // } } 动态加载、卸载 c#中通过反射可以方便的动态加载dll程序集,但是如果你需要对dll进行更新,却发现.net类库没有提供卸载dll程序集的方法。在.net 中,加入了应用程序域的概念,应用程序域是可以卸载的。也就是说,如果需要对动态加载的dll程序集进行更新,可以通过以下方法解决: 新建一个应用程序域,在该应用程序域中动态加载DLL,然后可以卸载掉该应用程序域。该应用程序域被卸载的时候,相关资源也会被回收。 要想这样实现,就要让你程序的currentDomain和新建的newDomain之间进行通信,穿过应用程序域的边界。从网上找到了某大牛的解决方法,抄下来留给自己看吧: using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Reflection;namespace UnloadDll {class Program {static void Main(string[] args) {string callingDomainName = AppDomain.CurrentDomain.FriendlyName;//Thread.GetDomain().FriendlyName;Console.WriteLine(callingDomainName); AppDomain ad = AppDomain.CreateDomain("DLL Unload test"); ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap(@"UnloadDll.exe", "UnloadDll.ProxyObject"); obj.LoadAssembly(); obj.Invoke("TestDll.Class1", "Test", "It's a test"); AppDomain.Unload(ad); obj = null; Console.ReadLine(); } }class ProxyObject : MarshalByRefObject { Assembly assembly = null;public void LoadAssembly() { assembly = Assembly.LoadFile(@"TestDLL.dll"); }public bool Invoke(string fullClassName, string methodName, params Object[] args) {if(assembly == null)return false; Type tp = assembly.GetType(fullClassName);if (tp == null)return false; MethodInfo method = tp.GetMethod(methodName);if (method == null)return false; Object obj = Activator.CreateInstance(tp); method.Invoke(obj, args);return true; } } } 注意: 1. 要想让一个对象能够穿过AppDomain边界,必须要继承MarshalByRefObject类,否则无法被其他AppDomain使用。 2. 每个线程都有一个默认的AppDomain,可以通过Thread.GetDomain()来得到 参考文章: C++与C#互调dll的实现步骤 c# 动态加载dll文件,并实现调用其中的方法(推荐) c#动态加载卸载DLL的方法 |
|