简体   繁体   中英

how to do array of struct in c# like an type in vb6 using marshal

Sorry if I did not explain well. The dll is not written in VB, is written in machine language, you can actually access the dll from vb6 delphi 7 and c + +. The connection method "PBusConnectEx" works perfectly from C # code. My real problem is that I have a "type" in vb6 I had to make it a "struct" in C #.

The first question is if you consider that the structure "struct" in C # is equivalent to the structure "type" in vb6.

The second question is if you consider that the external function declaration "PBusTransferPLUCluster" is well done compared to the external function declaration "PBusTransferPLUCluster" in vb6.

The third question is: ¿how do you think should be called PBusTransferPLUCluster function and parameter type must be sent compared with PBusTransferPLUCluster function call in vb6.? Sorry if I knew I explain above, I am new to the forum and do not write English very well.

VB6 Code Unit1.bas

Attribute VB_Name = "Module1"
Type TPlu
   PLUName As String
   LFCode As Long
   Code As String
   BarCode As Long
   UnitPrice As Long
   WeightUnit As Long
   Deptment As Long
   Tare As Double
   ShlefTime As Long
   PackageType As Long
   PackageWeight As Double
   Tolerance As Long
   Message1 As Byte
   Reserved As Byte
   Reserved1 As Integer
   Message2 As Byte
   Reserved2 As Byte
   MultiLabel As Byte
   Rebate As Byte
   Account As Long
End Type

Type TPLUCluster
   PLU(0 To 3) As TPlu
End Type

Type THotkeyTable
   Hotkey(0 To 83) As Long
End Type

Declare Function PBusConnect Lib "PBusDrv.dll" (ByVal RefLFZKFileName As String, ByVal RefCFGFileName As String, ByVal SerialNO As Integer, ByVal CommName As String, ByVal BaudRate As Integer) As Long
Declare Function PBusDisConnect Lib "PBusDrv.dll" (ByVal SerialNO As Integer) As Long
Declare Function PBusTransferPLUCluster Lib "PBusDrv.dll" (ByRef PLUCluster As TPLUCluster) As Long
Declare Function PBusTransferHotkey Lib "PBusDrv.dll" (ByRef HotkeyTable As THotkeyTable, ByVal TableIndex As Long) As Long
Declare Function PBusPLUToStr Lib "PBusDrv.dll" (ByRef PLU As TPlu, ByVal LPStr As String) As Long
Declare Function PBusStrToPLU Lib "PBusDrv.dll" (ByVal LPStr As String, ByRef PLU As TPlu) As Long
Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

Unit1.frm

VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Sample for Windows NT 4.0"
   ClientHeight    =   1500
   ClientLeft      =   1770
   ClientTop       =   1710
   ClientWidth     =   2190
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   1500
   ScaleWidth      =   2190
   Begin VB.CommandButton TestBtn 
      Caption         =   "Test"
      Height          =   375
      Left            =   360
      TabIndex        =   1
      Top             =   480
      Width           =   1335
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Height          =   195
      Left            =   1560
      TabIndex        =   0
      Top             =   4560
      Width           =   45
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub TestBtn_Click()
Dim Result As Long
Dim PLUCluster As TPLUCluster
Dim PLU As TPlu
Dim HotkeyTable As THotkeyTable
Dim Str As String

PLU.PLUName = "chorizoxx"
PLU.LFCode = 1
PLU.Code = 1
PLU.Deptment = 1
PLU.UnitPrice = 21414
PLU.BarCode = 22
PLUCluster.PLU(0) = PLU

Result = PBusConnectEx(".\lfzk.dat", ".\system.cfg", "192.168.1.87")
Result = PBusTransferPLUCluster(PLUCluster)
Result = PBusDisConnect(1234)
Result = MessageBox(0, "Test OK", "Demo", 0)

End Sub

This is my C# code:

using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]

namespace BALANZAC_{

   public partial class MainForm : Form{

      struct TPlu{
      public string PLUName;
      public long LFCode;
      public string Code;
      public long BarCode;
      public long UnitPrice;
      public long WeightUnit;
      public long Deptment;
      public double Tare;
      public long ShlefTime;
      public long PackageType;
      public long PackageWeight;
      public long Tolerance;
      public byte Message1;
      public byte Reserved;
      public int Reserved1;
      public byte Message2;
      public byte Reserved2;
      public byte MultiLabel;
      public byte Rebate;
      public long Account;
   }

   private TPlu PLU;
   private TPlu[] PPC=new TPlu[3];

   [DllImport("PBusDrv.dll")]
   public static extern int PBusConnectEx(string RefLFZKFileName, string RefCFGFileName , string IPAddr);
   [DllImport("PBusDrv.dll")]

   private static extern int PBusTransferPLUCluster([MarshalAs(UnmanagedType.LPArray, SizeConst=3)]TPlu[] PPC);

      public MainForm()
         InitializeComponent();

         PBusConnectEx("lfzk.dat", "system.cfg", "192.168.1.87");
         PLU.PLUName="PRUEBA PESADO X1";
         PLU.Code="222";
         PLU.LFCode=222;
         PLU.Deptment=1;
         PLU.UnitPrice=12993;
         PLU.BarCode=22;
         PPC[0]=PLU;
         PLUCluster.PLU[0]=PLU;


        }
    }   
}

Really sorry if I can`t explain me in the last post.

Int in Vb6 is Short in C#, Long in VB6 is Int in C#

VB6 has no 64bit Long like in C#

You could create a UDT in VB6 for Long (C#),

Type VB6Long64
   LoValue As Long
   HiValue As Long
End Type

See Data Type comparison between VB6 and VS2005 and how to do 64 bit in vba (ie VB6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM