如何将结构从vb传递到dll?
我正在尝试将一个结构从vb传递到dll im facinf //数据链路层.cpp如何将结构从vb传递到dll?,dll,vb6,Dll,Vb6,我正在尝试将一个结构从vb传递到dll im facinf //数据链路层.cpp #include"DataLinkLayer.h" #include<stdio.h> #include <windows.h> int __stdcall DataLink_TellTale_Encode (struct telltalelib *st_telltale, LPSTR * rtnFramePassedPtr) { sprintf(*rtnFramePa
#include"DataLinkLayer.h"
#include<stdio.h>
#include <windows.h>
int __stdcall DataLink_TellTale_Encode (struct telltalelib *st_telltale, LPSTR * rtnFramePassedPtr)
{
sprintf(*rtnFramePassedPtr,"<%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d>",NORMAL,STREAM,VEHICLEOVERALL,NA,PERCENTAGE,TELLTALEID,COMPIDNA,TELLTALELENGTH,st_telltale.Telltaleid,st_telltale.color,st_telltale.glowLevel,st_telltale.onOff);
return 0;
}
VB代码:
Private Type struct_telltalelib
Telltaleid As Integer
color As Integer
glowLevel As Integer
onOff As Integer
End Type
Private Declare Function DataLink_TellTale_Encode Lib "C:\MinGW\bin\DataLinkLayer.dll" (ByRef st_telltale As struct_telltalelib, ByRef rtnFramePassedPtr As String) As Integer
Dim vb_telltale As struct_telltalelib
Dim str_data As String * 20
Dim s2 As String
Dim stringToBeTrasmitted As String
Private Sub Check1_Click()
vb_telltale.Telltaleid = 8
vb_telltale.color = -1
vb_telltale.glowLevel = -2
vb_telltale.onOff = 1
dummy = DataLink_TellTale_Encode(str(vb_telltale), str_data)
stringToBeTrasmitted = Trim(Mid(Replace(str_data, Chr(0), " "), 1, InStr(str_data, Chr(0))))
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
UART1.PortOpen = True
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = False
UART1.Output = stringToBeTrasmitted
End Sub
我收到的错误消息类似于只有在公共对象模块中定义的用户才能在各种函数之间进行更正或传递到后期绑定函数我发现了以下问题:
- 结构应作为
传递,而不是vb\u信号装置
李>str(vb\u信号装置)
- 字符串应声明为
李>ByVal
- 字符串缓冲区可能太小,将溢出
公共
。此外,在C中,int
相当于VB6Long
,而不是整数,因此可能存在大小和对齐不匹配的问题。可能还有其他问题,但这些问题很突出。将str(vb_-telltable)更改为vb_-telltable,str_-dat大小更改为100后,获取运行时错误“53”。并指向行**dummy=DataLink_-telltable_-Encode(vb_-telltable,str_-data)**@DavidHeffernan字符串的返回类型是指针,所以只有它通过Ref给出了ad。不,正如我所说的,必须通过val来完成。阅读文档。@Sri-只要VB代码中有为整数
,将其更改为为Long
。将私有类型
更改为公共类型
。将Dim vb\u信号装置
更改为Public vb\u信号装置
,并将两者放置在.bas
模块中,而不是窗体或类模块中。将stru data
更改为可变长度字符串,并将其初始化为包含(比如)1000个空格。传递它ByVal
,而不是ByRef
(BSTR已经是指针)。试试看,让我们知道会发生什么。
Private Type struct_telltalelib
Telltaleid As Integer
color As Integer
glowLevel As Integer
onOff As Integer
End Type
Private Declare Function DataLink_TellTale_Encode Lib "C:\MinGW\bin\DataLinkLayer.dll" (ByRef st_telltale As struct_telltalelib, ByRef rtnFramePassedPtr As String) As Integer
Dim vb_telltale As struct_telltalelib
Dim str_data As String * 20
Dim s2 As String
Dim stringToBeTrasmitted As String
Private Sub Check1_Click()
vb_telltale.Telltaleid = 8
vb_telltale.color = -1
vb_telltale.glowLevel = -2
vb_telltale.onOff = 1
dummy = DataLink_TellTale_Encode(str(vb_telltale), str_data)
stringToBeTrasmitted = Trim(Mid(Replace(str_data, Chr(0), " "), 1, InStr(str_data, Chr(0))))
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
UART1.PortOpen = True
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = False
UART1.Output = stringToBeTrasmitted
End Sub