c - VB6中如何从指针获取字符串

标签 c string dll vb6 bstr

我必须用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 代码无法运行。

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

请帮助我,我依靠你。

最佳答案

评论实际上是错误的。你是对的。 VB6 会将String(无论如何以这种方式发送)转换为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$())。

关于c - VB6中如何从指针获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18562873/

相关文章:

c# - 文件路径作为命令行参数

java - 将 Java 值发送到网页

c++ - 用于测试的 IAT Hook

winapi - 如何保护自己免受 shell DLL 加载到进程中的影响?

c - 使用 vmalloc 为内核模块分配大量内存

c++ - 为什么 Microsoft 的 C 编译器要函数开头的变量?

c - 在c中导入矩阵

在打开的连接上更改密码

string - 如何在 Bash 中将字符串转换为小写

eclipse - 程序无法启动,因为 Eclipse CDT 中缺少 cygwin1.dll...