从 VB.net 项目调用 VB6 本机 dll 时出现问题
我有一个 vb6 项目,在其中使用 dll 库来执行一些任务。为此,我添加了一个模块,该模块声明了该库的所有函数,例如:
Declare Function myFunction Lib "abcdapi.dll" (ByVal hVar1 As Long, ByVal hVar2 As Long) As Long
当我调用此函数(以及许多其他类似函数)时,我能够完成工作并返回正确的 Long 指针。但是,当我尝试通过 VB.net 执行相同的操作(通过使用 dllimport 导入相同的库并调用相同的函数)时,我没有得到任何结果。虽然它确实返回一些指针,但不会产生正确的结果。
Private Const ABCD As String = "abcdapi.dll"
<DllImport(ABCD)>
Public Shared Function myFunction(ByVal hVar1 As IntPtr, ByVal hVar2 As IntPtr) As IntPtr
End Function
当我尝试调用上面的函数时,它不起作用。我什至尝试使用该函数在我的 vb6 项目中创建一个 dll,并尝试使用导入关键字来调用该新 dll,但这也不起作用。可能是什么问题以及我该如何让它发挥作用。
I have a vb6 project in which I use a dll library to do some tasks. For that, I add a module which declares all functions of that library such as:
Declare Function myFunction Lib "abcdapi.dll" (ByVal hVar1 As Long, ByVal hVar2 As Long) As Long
When I call this function (and many other similar) I'm able to do the work and correct Long pointer is returned. But when I try to do the same thing by VB.net (by importing the same library using dllimport and calling the same function), I get no result. although it does return some pointer but that doesn't produce correct result.
Private Const ABCD As String = "abcdapi.dll"
<DllImport(ABCD)>
Public Shared Function myFunction(ByVal hVar1 As IntPtr, ByVal hVar2 As IntPtr) As IntPtr
End Function
When I try to call the above function, it doesn't work. I even tried creating a dll in my vb6 project using the function and try to use imports keyword to call that new dll but that doesn't work either. What could be the issue and how do I make it work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您引用的文档显示:
TM1U 和 TM1V 是否有可能在该 API 中定义为 32 位数据类型,并且您在 64 位计算机上运行 .NET 代码,从而使您的 IntPtr 成为 64 位数据类型? (如果 API 附带 C 头文件,您可以看到这些数据类型是如何定义的)。尝试将 .NET 代码重新编译为“x86”,然后重试。
我刚刚从您上面的评论中复制了这段代码:
这可能是一个拼写错误,但是 VB6 中的返回类型与 VB.NET 中的返回类型不同。 VB6 整数是 16 位,VB.NET 中的整数是 32 位。
The docos you referenced show:
Is it possible that TM1U and TM1V are defined as 32 bit data types in that API and you are running your .NET code on a 64 bit machine, making your IntPtr a 64 bit data type? (If the API came with C header files you can see how those data types are defined). Try recompiling your .NET code to "x86" and try it again.
I just copied this code from your comment above:
It is probably a typo, but that return type in your VB6 is not the same as the one in VB.NET. A VB6 Integer is 16 bits and an Integer in VB.NET is 32 bits.