简体   繁体   English

如何使用元数据在c#中像vb6中的类型一样在c#中构造结构数组

[英]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 + +. dll不是用VB编写的,而是用机器语言编写的,您实际上可以从vb6 delphi 7和c ++访问dll。 The connection method "PBusConnectEx" works perfectly from C # code. 连接方法“ PBusConnectEx”可从C#代码完美地工作。 My real problem is that I have a "type" in vb6 I had to make it a "struct" in C #. 我的真正问题是在vb6中有一个“类型”,在C#中必须使其成为“结构”。

The first question is if you consider that the structure "struct" in C # is equivalent to the structure "type" in vb6. 第一个问题是您是否认为C#中的结构“ struct”与vb6中的结构“ type”等效。

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. 第二个问题是,与vb6中的外部函数声明“ PBusTransferPLUCluster”相比,您是否认为外部函数声明“ PBusTransferPLUCluster”做得好。

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.? 第三个问题是:与vb6中的PBusTransferPLUCluster函数调用相比,您认为应该如何调用PBusTransferPLUCluster函数和参数类型? Sorry if I knew I explain above, I am new to the forum and do not write English very well. 抱歉,如果我知道我在上面解释了,我是该论坛的新手,英语写作不太好。

VB6 Code Unit1.bas VB6代码单元1.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 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: 这是我的C#代码:

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中的Int在C#中为Short,VB6中的Long在C#中为Int

VB6 has no 64bit Long like in C# VB6没有像C#中那样的64位长

You could create a UDT in VB6 for Long (C#), 您可以在VB6中为Long(C#)创建UDT,

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) 请参阅VB6与VS2005之间的数据类型比较,以及如何在vba中执行64位(即VB6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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