从外部DLL调用函数 [英] Call Function off an extern DLL

查看:122
本文介绍了从外部DLL调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道如何将dll导入到我的项目中,但是通过手动引用im接收到诸如找不到入口点"之类的错误

我创建了一个名为myDll的dll文件,该类名为myFunctions和一个名为showResult()As String的函数,例如:

例如,位于C:\上的myDll.dll

I already know how to import a dll into my project but by manually reference im receiving errors like "entry point was not found"

I created the dll file called myDll the class is called myFunctions and a function called showResult() As String just for example:

myDll.dll which is located on C:\ for example

Public Class myFunctions
  Public Function showResult() as string
  Return "HELLO WORLD"
  End Function
End Class




myApplication




myApplication

Public Class callExternDll
  <dllimport("c:\mydll.dll")>
  Public Shared Function showResult() As String
  End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox(showResult)

End Sub




我想调用该函数,以便接收"hello world".




I want to call the function so to receive "hello world"

推荐答案

请参阅我对问题的评论.如果myFunctions.showResult也在用VB.NET编写的CLI代码中,则不需要P/Invoke即可使用它. P/Invoke仅用于在.NET(或其他CLI)程序集中使用非托管代码.如果两个部件都是.NET程序集,则只需简单地相互引用并直接使用它:
http://en.wikipedia.org/wiki/.NET_assembly [ http://msdn.microsoft.com/en-us/library/ms973231.aspx [ ^ ].

如果在.NET程序集中使用非托管代码,则确实需要使用P/Invoke.您应该检查的一件事是使用正确的入口点名称,调用约定(在Basic的不同版本之间应该是相同的)等等.当您拥有DLL时,您可以使用某些二进制转储工具(例如"dumpbin")查看其导出. EXE文件".请参阅:
http://msdn.microsoft.com/en-us/library/aa446532.aspx [ ^ ].

如果您需要学习P/Invoke,请参阅:
http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [^ ].

此CodeProject文章也可能有用:基本P/调用 [
Please see my comment to the question. If myFunctions.showResult is also in the CLI code, written in VB.NET, you don''t need P/Invoke to use it. P/Invoke is only used to use unmanaged code in .NET (or other CLI) assembly. If both pieces are .NET assemblies, you simply reference one by another and use it directly:
http://en.wikipedia.org/wiki/.NET_assembly[^],
http://msdn.microsoft.com/en-us/library/ms973231.aspx[^].

If you use unmanaged code in a .NET assembly, you really need to use P/Invoke. One thing you should check up is using right entry point name, calling conventions (which should be the same between different versions of Basic), etc. When you have a DLL, you can see its exports using some binary dump tool like "dumpbin.exe". Please see:
http://msdn.microsoft.com/en-us/library/aa446532.aspx[^].

If you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful: Essential P/Invoke[^].

—SA


这篇关于从外部DLL调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆