gpt4 book ai didi

无法使用 VB 从 C 函数读取 char *

转载 作者:行者123 更新时间:2023-11-30 17:49:11 26 4
gpt4 key购买 nike

从用 C 编写的 dll 函数读取 char * 时出现问题。

DLL 称为 rapdll.dll。它的头文件如下所示:

extern "C"{void _stdcall New_Rapdll(void);}  
extern "C"{void _stdcall TCP_Set_RelayIO(char *Ipaddress,char *SerialNumber,char *Password,char *Data);}
extern "C"{Variant _stdcall Get_Receivedata(void);}

我没有 DLL 的源代码。我遇到问题的命令 (Get_Receivedata()) 的帮助文件是:

声明

Char * Get_Receivedata(void)                                

示例:

Get_Receivedata ()  

返回值:

See Response from RAP: Success  
Data empty: Fail
Description:
This function is getting response from RAP.

到目前为止我编写的代码是:

Public Class Form1  
Declare Sub TCP_Set_RelayIO Lib "rapdll.dll" (ByVal Ipaddress As String, ByVal Serial As String, ByVal Pass As String, ByVal Relay As String)
Declare Sub TCP_Get_SerialNumber Lib "rapdll.dll" (ByVal Ipaddress As String, ByVal Pass As String)
Declare Sub New_Rapdll Lib "rapdll.dll" ()
Declare Function Get_Receivedata Lib "rapdll.dll" () As String
Dim Result As String


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub

Function rap() As String
Result = Get_Receivedata()
Return Result
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim st As String
Call New_Rapdll()
Call TCP_Set_RelayIO("192.168.1.2", "0000", "0000", "00120")
st = rap()
MsgBox(st)

End
End Sub
End Class

直到 st = rap() 行,一切正常。 “Call TCP_Set_RelayIO("192.168.1.2", "0000", "0000", "00120")"之前的命令可以正常工作,因为它确实重置了该设备中的中继。但是,当我尝试使用 Get_Receivedata 函数读取回复时,我收到“System.AccessViolationException”类型的未处理异常。

我知道问题在于尝试读取 Get_Receivedata 函数返回的 char *,但我不确定如何正确执行此操作。

感谢您的帮助!

最佳答案

这看起来不对(变体):

extern "C"{Variant _stdcall Get_Receivedata(void);}  

无论如何,我真的建议您改用参数。像这样的事情...

C++:

extern "C"{void _stdcall Get_Receivedata(char** data);}

VB:

Declare Function Get_Receivedata Lib "rapdll.dll" (ByRef Data As String)

关于无法使用 VB 从 C 函数读取 char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com