Unload a .NET DLL from an unmanaged process

后端 未结 5 735
时光取名叫无心
时光取名叫无心 2020-12-09 16:33

I\'m extending my Inno-Setup script with code that I can best implement in C# in a managed DLL. I already know how to export methods from a managed DLL as functions for use

5条回答
  •  半阙折子戏
    2020-12-09 17:03

    The easy way to do what you want is through an AppDomain. You can unload an AppDomain, just not the initial one. So the solution is to create a new AppDomain, load your managed DLL in that and then unload the AppDomain.

            AppDomain ad = AppDomain.CreateDomain("Isolate DLL");
            Assembly a = ad.Load(new AssemblyName("MyManagedDll"));
            object d = a.CreateInstance("MyManagedDll.MyManagedClass");
            Type t = d.GetType();
            double result = (double)t.InvokeMember("Calculate", BindingFlags.InvokeMethod, null, d, new object[] { 1.0, 2.0 });
            AppDomain.Unload(ad);
    

    Here is what the DLL code looks like...

    namespace MyManagedDll
    {
       public class MyManagedClass
       {
          public double Calculate(double a, double b)
          {
            return a + b;
          }
       }
    }
    

提交回复
热议问题