c#中动态装载dll

记得很久前有个人让我解决这么一个事情,他的一个c动态连接库里面有个静态变量,每次调用这个方法的时候,就自动增加,他想在特定的时候,为了恢复这个静态变量的初值,动态卸了这个动态库,然后重新加载。(该动态库不能改动)

c#里面要用到动态库,需要使用DllImport,但是这个是全局的东西,不能像动态load/unload assembly所使用的AppDomain的方法。

这样就想到了API: LoadLibrary, GetProcAddress, 和FreeLibrary方法。
  [DllImport("kernel32",EntryPoint="LoadLibrary",SetLastError=true)]
  static extern IntPtr LoadLibrary(string lpLibName);

  [DllImport("kernel32",EntryPoint="GetProcAddress",SetLastError=true)]
  static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

  [DllImport("kernel32",EntryPoint="FreeLibrary",SetLastError=true)]
  static extern bool FreeLibrary(IntPtr hModule);

然后调用
   IntPtr hModule = IntPtr.Zero;
   IntPtr pfn = IntPtr.Zero;

   // Load the library and get a pointer to the function 
   hModule = LoadLibrary("ADll.dll"); 
   pfn = GetProcAddress(hModule, "GetValue"); 
然后就麻烦了,知道这个函数的入口地址,我怎么调用这个函数呢,我一直不知道这个用c#怎么解决,最后就写了一段IL代码。
.assembly extern mscorlib {}
.assembly Wrapper {}

.class public Wrapper
{
     .method public static int32 SomeMethod(native int pfn)
    {
        .maxstack 2
        .locals (int32 V_0)
        ldarg.0 // Push pfn onto the execution stack
        calli unmanaged stdcall int32()
        stloc.0
        ldloc.0
        ret
    }
}
把这个.il文件编译成dll
然后代码接着写
   // Make call to function pointer
   int i = Wrapper.SomeMethod(pfn);

   MessageBox.Show(this, i.ToString());
 
   FreeLibrary(hModule);
问题就解决了。

 
原文地址:https://www.cnblogs.com/rainbowzc/p/2422148.html