簡體   English   中英

如何使用元數據在c#中像vb6中的類型一樣在c#中構造結構數組

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

對不起,如果我沒有解釋清楚。 dll不是用VB編寫的,而是用機器語言編寫的,您實際上可以從vb6 delphi 7和c ++訪問dll。 連接方法“ PBusConnectEx”可從C#代碼完美地工作。 我的真正問題是在vb6中有一個“類型”,在C#中必須使其成為“結構”。

第一個問題是您是否認為C#中的結構“ struct”與vb6中的結構“ type”等效。

第二個問題是,與vb6中的外部函數聲明“ PBusTransferPLUCluster”相比,您是否認為外部函數聲明“ PBusTransferPLUCluster”做得好。

第三個問題是:與vb6中的PBusTransferPLUCluster函數調用相比,您認為應該如何調用PBusTransferPLUCluster函數和參數類型? 抱歉,如果我知道我在上面解釋了,我是該論壇的新手,英語寫作不太好。

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

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

這是我的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;


        }
    }   
}

非常抱歉,如果我無法在上一篇文章中向我解釋。

Vb6中的Int在C#中為Short,VB6中的Long在C#中為Int

VB6沒有像C#中那樣的64位長

您可以在VB6中為Long(C#)創建UDT,

Type VB6Long64
   LoValue As Long
   HiValue As Long
End Type

請參閱VB6與VS2005之間的數據類型比較,以及如何在vba中執行64位(即VB6)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM