从 VB.net 项目调用 VB6 本机 dll 时出现问题

发布于 2024-12-26 20:34:26 字数 638 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2025-01-02 20:34:26

您引用的文档显示:

TM1IMPORT TM1_BOOL TM1API TM1ValBoolGet(TM1U hUser, TM1V vBool );

TM1U 和 TM1V 是否有可能在该 API 中定义为 32 位数据类型,并且您在 64 位计算机上运行 .NET 代码,从而使您的 IntPtr 成为 64 位数据类型? (如果 API 附带 C 头文件,您可以看到这些数据类型是如何定义的)。尝试将 .NET 代码重新编译为“x86”,然后重试。

我刚刚从您上面的评论中复制了这段代码:

函数调用如下:

ibOK = TM1ValBoolGet(hUser, voTemp) 

在 VB.net 中:<<<我假设你指的是 VB6

Declare Function TM1ValBoolGet Lib "tm1api.dll" (ByVal hUser As Long, ByVal vBool As Long) As Integer 

在 vb.net 中:

<DllImport(TM1APIDLL)> Public Shared Function TM1ValBoolGet(ByVal hUser As IntPtr, ByVal vBool As IntPtr) As Integer 
End Function 

这可能是一个拼写错误,但是 VB6 中的返回类型与 VB.NET 中的返回类型不同。 VB6 整数是 16 位,VB.NET 中的整数是 32 位。

The docos you referenced show:

TM1IMPORT TM1_BOOL TM1API TM1ValBoolGet(TM1U hUser, TM1V vBool );

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:

the function call is below:

ibOK = TM1ValBoolGet(hUser, voTemp) 

In VB.net: <<< I assume here you meant VB6

Declare Function TM1ValBoolGet Lib "tm1api.dll" (ByVal hUser As Long, ByVal vBool As Long) As Integer 

In vb.net:

<DllImport(TM1APIDLL)> Public Shared Function TM1ValBoolGet(ByVal hUser As IntPtr, ByVal vBool As IntPtr) As Integer 
End Function 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文