【问题标题】:How to get String from Pointer in VB6VB6如何从指针中获取字符串
【发布时间】:2013-09-01 20:59:18
【问题描述】:

我必须用 VB6 调用 C DLL。

C 代码

short int decode(BOOL Mode, char* tete, char* adresse, char* status, char* nombre, char* datadecode);

我的 VB 代码:

Private Declare Function decode Lib "VBdecode.dll" ( _ 
ByVal Mode As Long, _ 
ByVal tete As String, _ 
ByVal adresse As String, _ 
ByVal status As String, _ 
ByVal nombre As String, _ 
ByVal datadecode As String) As Long
Dim retour_lire As Long
Dim buffer(4) As Byte   
Dim  vbcData as string
Dim i As Integer
Dim chdecode As string

retour_lire = Byte_read(True, "4", "00", buffer, "16", vbcData)

For i = 1 To 10 
chdecode = vbcData(i) 
Next

MsgBox chdecode

但我的 VB6 代码不起作用。

请提出任何想法、建议或更正。

请帮帮我,我指望你。

【问题讨论】:

  • VB 将 unicode 字符串作为 BSTR 传递;不是char*
  • VB需要字符串的大小,它没有终止\0的概念,BSTR里面有大小。
  • 所以在函数声明中我必须通过 As BSTR 编辑 AS String ??

标签: c string dll vb6 bstr


【解决方案1】:

cmets 实际上是错误的。你说的对。 VB6 会将Strings(无论如何都以这种方式发送)转换为char*。如果您使用的是VarPtr()As Any,他们是对的。

您的问题是返回值。将 C++ 端的 short int 更改为 int 或将 VB6 端的 As Long 更改为 As Integer

所以要么:

int decode(BOOL Mode, char* tete, char* adresse, char* status, char* nombre, char* datadecode);

或:

Private Declare Function decode Lib "VBdecode.dll" ( _ 
ByVal Mode As Long, _ 
ByVal tete As String, _ 
ByVal adresse As String, _ 
ByVal status As String, _ 
ByVal nombre As String, _ 
ByVal datadecode As String) As Integer

不是两者都有:)!

值得注意的是,您的示例代码实际上并没有调用decode()。所以另一个问题,这只是一个猜测,是你试图在 C++ 端更改字符串的内容。您可以更改字符串,但不能重新分配它。所以你需要有它的大小(使用Space$())。

【讨论】:

  • 我所有的建议都表明了VB6端的解决方案。
  • +1 @mahader 答案是:如果您不更改 C,则“将 VB6 端的 As Long 更改为 As Integer。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2019-10-15
  • 2017-10-29
  • 1970-01-01
相关资源
最近更新 更多