抱歉,如果我没有很好地解释。dll不是用VB写的,是用机器语言写的,实际上可以从vb6 delphi 7和c++中访问dll。连接方法“PBusConnectEx”在 C# 代码中完美运行。我真正的问题是我在 vb6 中有一个“类型”,我必须在 C# 中将其设为“结构”。
第一个问题是如果你认为C#中的结构“struct”等价于vb6中的结构“type”。
第二个问题是,如果您认为外部函数声明“PBusTransferPLUCluster”与 vb6 中的外部函数声明“PBusTransferPLUCluster”相比做得很好。
第三个问题是:¿你觉得PBusTransferPLUCluster函数和参数类型应该怎么调用,和vb6中的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;
}
}
}
如果我不能在上一篇文章中解释我真的很抱歉。