如何将struct从vb传递给dll?

时间:2017-03-23 04:57:41

标签: dll vb6

我正试图将一个结构从vb传递给dll im facinf isues。

// Datalink layer.cpp

 #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;
}

// Datalink Layer.h

  #define MAXFRAMELEN 20
  #define MAXPOSSOPTFIELDS 50
  #define RESERVED 0
  #define COMPIDNA 0
  #define NA 1
  #define SPEEDLENGTH 1
  #define RPMLENGTH 1
  #define FUELLENGTH 1
  #define TELLTALELENGTH 3

 struct telltalelib
{
 int Telltaleid; 
 int color;
 int glowLevel;
 int onOff;
};



 int __stdcall DataLink_TellTale_Encode (struct telltalelib *st_telltale, char* rtnFramePassedPtr);

  enum priority{ LOW=1  , NORMAL,   HIGH,   CRITICAL,FATAL };
  enum frameType{ REQUEST=1,    RESPONSE,   ERROR,  STREAM, NOTIFICATION,    STATEINFORMATIONCHANGE,    PERIODIC };
  enum domain{MODULEPOWER=1,MODULESTATEMACHINE,VEHICLEOVERALL,ENGINE,TRANSMISSION,BRAKE,SAFETYSYSTEMS,LOCK,DRIVER,AC,DOOR,TPMS,LIGHTS,WINDOW,   ANTENNA,SEATS,VEHICLEATTITUDE,WIPER,ORVM,DISPLAY,VEHICLECONSTANTS,TELEPHONY,INFOTAINMENT,FOTA };
   enum Parameter{ SPEED=2  ,RPM    ,FUEL,  ODO ,TEMPERATURE,   PRESSURE,   HUMIDITY,BATTERYLEVEL,  ONOFF , GLOWLEVEL,  COLOR,  OPENPOSITION,   RAMPSTYLE,  CMDSHUTDOWN,    CMDREBOOT,  NOTIFICATIONTEXT,   CURRENT,    DRIVEMODE,  GEARLEVEL,  MASS,   VOLUME, DATAPACKET  ,SLIDELEVEL ,RECLINELEVEL,  DRIVERID,   DRIVINGSCORE,   MINLEVELSETTING,    MAXLEVELSETTING,    TIME,   ROLL,   PITCH,  YAW,MODE};
   enum Unit{ RAW=2,    SECONDS,    CM  ,M  ,KM,    MILE,   KMPERHOUR,  MILESPERHOUR,   X100RPM,    X1000RPM,   PERCENTAGE, RGB,    PSI,    BAR,    KPA,    DEGCELCIUS, DEGFARANHEIT    ,FULLYOPENORFULYLCLOSED,    ENUMERATEDCODES ,ENUMERATEDFIELDID, V   ,MA,    A,  DEGREE ,    KG  ,CM3,   CC, PLAINTEXT   ,DB ,EPOCHTIME  ,OKNOTOK };
    enum Component{TYREID=2 ,ACVENTID,DOORID,   WINDOWID,   LIGHTID,    ANTENNAID,  TELLTALEID, ACTUATORID,GENERICDATAFIELDID   ,DATAFORMATID,  WIPERID }   ;

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

我收到错误消息,例如只有公共对象模块中定义的用户可以更改为varient或来自varient或传递给后期绑定函数

2 个答案:

答案 0 :(得分:0)

我看到以下问题:

  • 结构应该以{{1​​}}而不是vb_telltale传递。
  • 该字符串应声明为str(vb_telltale)
  • 字符串缓冲区可能太小而且会溢出。

答案 1 :(得分:0)

错误告诉您必须在BAS模块中将VB Type变量声明为Has A。此外,在C中,Is A相当于VB6 Public,而不是int,因此您可能会出现大小和对齐不匹配的问题。可能还有其他问题,但这些问题很突出。