设为首页 加入收藏

TOP

C#调用dll导出函数
2013-10-07 00:59:51 】 浏览:503
Tags:调用 dll 导出 函数

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

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

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

  1. publicdelegateintMsgBox(inthwnd,stringmsg,stringcpp,intok);  
  2. [DllImport("Kernel32")]  
  3. publicstaticexternintGetProcAddress(inthandle,Stringfuncname);  
  4. [DllImport("Kernel32")]  
  5. publicstaticexternintLoadLibrary(Stringfuncname);  
  6. [DllImport("Kernel32")]  
  7. publicstaticexternintFreeLibrary(inthandle);  
  8. privatestaticDelegateGetAddress(intdllModule,stringfunctionname,Typet)  
  9. {  
  10. intaddr=GetProcAddress(dllModule,functionname);  
  11. if(addr==0)  
  12. returnnull;  
  13. else  
  14. returnMarshal.GetDelegateForFunctionPointer(newIntPtr(addr),t);  
  15. }  
  16. privatevoidbutton1_Click(objectsender,EventArgse)  
  17. {  
  18. inthuser32=0;  
  19. huser32=LoadLibrary("user32.dll");  
  20. MsgBoxmymsg=(MsgBox)GetAddress(huser32,"MessageBoxA",typeof(MsgBox));  
  21. mymsg(this.Handle.ToInt32(),txtmsg.Text,txttitle.Text,64);  
  22. FreeLibrary(huser32);  

C#调用dll导出函数

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

【编辑推荐】

  1. C#字符串进行分割
  2. 全面测试C#字符串
  3. C# out和ref传递数组
  4. 浅析C#定义整型数组
  5. C#数据库连接字符串
【责任编辑:志京 TEL:(010)68476606】

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C#异常机制的相关解释 下一篇A.2.3 参考文献

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目