在用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()行。 “之前的命令”调用TCP_Set_RelayIO(“192.168.1.2”,“0000”,“0000”,“00120”)“工作正常,因为它会重置此设备中的继电器。但是当我尝试使用Get_Receivedata
函数读取回复时,我得到一个未处理的类型'System.AccessViolationException'的异常。
我知道问题是尝试从char *
函数读取Get_Receivedata
返回,但我不确定如何正确执行此操作。
感谢您的帮助!
答案 0 :(得分:0)
这看起来不正确(Variant
):
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)
答案 1 :(得分:0)
尝试将Get_Receivedata()
的回复率从String
更改为Object
或Variant
(以及结果变量)。您也可以尝试Byte
数组。如果它停止了崩溃,你可能只需要弄清楚返回的字节细分,并可能做出Type
或做一些CopyMemory()
欺骗。