请教:VB调用VC dll 报错

testpwd 2010-04-23 04:36:29
VC中的导出的函数是这样的:

extern "C" __declspec(dllexport)
HRESULT Connect(LPCSTR IpAddr, CHAR* pcUserName = NULL, CHAR* pcPassword = NULL)//返回0表示连接成功,非0则失败
{
...
}

我在VB中声明如下:

Declare Function Connect Lib "C:\example.dll" (ByVal IpAddr As String, ByVal UserName As String, ByVal Passwd As String) As Integer

同时,我定义

Const NULL = 0

然后调用:

Dim result As Integer

result = Connect("138.139.0.230", NULL, NULL)

If result <> 0 Then

MsgBox("error_connect")

Return

End If

结果总是报错,请问哪里不对呢?

...全文
125 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
testpwd 2010-04-23
  • 打赏
  • 举报
回复
太感谢zdbb大侠了!OK了,多谢!

也感谢各位兄弟的大力帮助!
只在此山中 2010-04-23
  • 打赏
  • 举报
回复
可能需加上
Imports System.Runtime.InteropServices
只在此山中 2010-04-23
  • 打赏
  • 举报
回复
再试下:
<DllImport("C:\example.dll", CallingConvention:=CallingConvention.Cdecl)> _
public Function Connect (ByVal IpAddr As String, ByVal UserName As String, ByVal Passwd As String) As Integer
End Function

result = Connect("138.139.0.230", nothing, nothing)
testpwd 2010-04-23
  • 打赏
  • 举报
回复
我用VC写的程序调用这个Connect函数:

HRESULT result = pDriver->Connect("138.139.0.230");

返回值为0,就是对的。
testpwd 2010-04-23
  • 打赏
  • 举报
回复
返回的就是一个负值:-1071579135
zsuswy 2010-04-23
  • 打赏
  • 举报
回复
result = Connect("138.139.0.230", NULL, NULL)
-----------
你看一下,它返回的不是0,返回的具体是个什么东西?调试一下或者直接吧结果MsgBox出来
testpwd 2010-04-23
  • 打赏
  • 举报
回复
把后两个参数的ByVal改成ByRef也不行。。。
testpwd 2010-04-23
  • 打赏
  • 举报
回复
String.Empty也试过了,返回值也非0。
testpwd 2010-04-23
  • 打赏
  • 举报
回复
用nothing试过了,还是不行,报错就是说 result 不等于0

进入到 MsgBox("error_connect")
yuanhuiqiao 2010-04-23
  • 打赏
  • 举报
回复
result = Connect("138.139.0.230", String.Empty, String.Empty)?
只在此山中 2010-04-23
  • 打赏
  • 举报
回复
看下:
result = Connect("138.139.0.230", nothing, nothing)
xingyuebuyu 2010-04-23
  • 打赏
  • 举报
回复
Const NULL = Nothing

报错信息是什么?

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧