zoukankan      html  css  js  c++  java
  • 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);
    问题就解决了。

     
  • 相关阅读:
    Vsftpd 3.0.2 正式版发布
    Putdb WebBuilder 6.5 正式版本发布
    SoaBox 1.1.6 GA 发布,SOA 模拟环境
    pynag 0.4.6 发布,Nagios配置和插件管理
    Percona Playback 0.4,MySQL 负荷回放工具
    xombrero 1.3.1 发布,微型 Web 浏览器
    Hypertable 0.9.6.4 发布,分布式数据库
    libmemcached 1.0.11 发布
    CryptoHeaven 3.7 发布,安全邮件解决方案
    Android Activity生命周期
  • 原文地址:https://www.cnblogs.com/rainbowzc/p/2422148.html
Copyright © 2011-2022 走看看