您当前的位置: 首页 >  c#

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4642博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#调用dll代码范例

柳鲲鹏 发布时间:2019-12-26 16:04:26 ,浏览量:0

示例代码:


public class ExecuteDLL : Form
{

   private IntPtr hModule = IntPtr.Zero;

   //申明外部API

   [DllImport("kernel32.dll")]
   static extern IntPtr LoadLibrary(string lpFileName);

   [DllImport("kernel32.dll")]
   static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

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

   //申明委托
   private delegate IntPtr TestFunc(int window);

   private void test()
   {
        string strDLLPath =  ".\\tests.dll";
        hModule = LoadLibrary(strDLLPath);
        if (hModule.Equals(IntPtr.Zero))
        {
            MessageBox.Show("导入DLL失败");
            return;
        }

       TestFuncfarProc = (TestFunc)this.GetFunctionAddress(hModule, "TestFunc", typeof(TestFunc));
       if (farProc == null)
       {
           FreeLibrary(hModule);
           hModule = IntPtr.Zero;
           return;
       }

       //利用委托执行DLL文件中的接口方法
       farProc(hModule, IntPtr.Zero, IntPtr.Zero, null, null, strNo);
       FreeLibrary(hModule);
       hModule = IntPtr.Zero;
   }

}

 

关注
打赏
1665724893
查看更多评论
立即登录/注册

微信扫码登录

0.0502s