C#调用dll导出函数

本文介绍C#调用dll导出函数,在C++中我们能够通过 LoadLibrary,GetProcAddress 来动态C#调用dll导出函数。在C#中也能够用这样的方式吗?
首页 新闻资讯 行业资讯 C#调用dll导出函数

在 C++ 中我们能够通过 LoadLibrary, GetProcAddress 来动态C#调用dll导出函数。在 C# 中也能够用这样的方式吗?

在 DotNet 2.0 里面这样是可以的, 这完全得益于 2.0新增的一个函数Marshal.GetDelegateForFunctionPointer 方法此方法在 .NET Framework 2.0 版中是新增的。将非托管函数指针转换为委托。

C#调用dll导出函数实例代码如下:

复制

publicdelegateintMsgBox(inthwnd,stringmsg,stringcpp,intok);  [DllImport("Kernel32")]  publicstaticexternintGetProcAddress(inthandle,Stringfuncname);  [DllImport("Kernel32")]  publicstaticexternintLoadLibrary(Stringfuncname);  [DllImport("Kernel32")]  publicstaticexternintFreeLibrary(inthandle);  privatestaticDelegateGetAddress(intdllModule,stringfunctionname,Typet)  {  intaddr=GetProcAddress(dllModule,functionname);  if(addr==0)  returnnull;  else  returnMarshal.GetDelegateForFunctionPointer(newIntPtr(addr),t);  }  privatevoidbutton1_Click(objectsender,EventArgse)  {  inthuser32=0;  huser32=LoadLibrary("user32.dll");  MsgBoxmymsg=(MsgBox)GetAddress(huser32,"MessageBoxA",typeof(MsgBox));  mymsg(this.Handle.ToInt32(),txtmsg.Text,txttitle.Text,64);  FreeLibrary(huser32);  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

C#调用dll导出函数

以上介绍C#调用dll导出函数

【编辑推荐】

  1. C#字符串进行分割

  2. 全面测试C#字符串

  3. C# out和ref传递数组

  4. 浅析C#定义整型数组

  5. C#数据库连接字符串

11    2009-08-07 17:22:36    C#调用dll导出函数