有一段代码是用Delphi写的,实现调用一个C++的dll的函数。
我现在需要按着Delphi的写法改秤C#来调用。
现在改完了以后有些问题。(程序执行,dll调用都正常,对int的赋值也正确,只是对结构体的赋值就不正确,不报错,但值不正确。)
请看代码:
/**************************************Delphi代码**************************************/
varRecords:LabelPLU;
begin
Records.PLUNo:=1;
Records.EAN_Data.ItemCode:='1234500000';
Records.EAN_Data.BarCodeType:=0;
Records.EAN_Data.BarRightData:=0;
Records.Commodity[0].TwsFontSize:=21;
Records.Commodity[0].TxtValue:='商品名称test';
CommResult:=SendLPlu(s,@Records);
end;
/*****************
SendLPlud代码
*****************/
functionSendLPlu(s:TSocket;PLUData:PLabelPLU):Integer;stdcall;external'×××.dll';
/**************
LabelPLU代码:
**************/
LabelPLU=record
PLUNo:Integer;
EAN_Data:SM_EANData;
Commodity:array[0..3]ofSM_ASCII_TXT;
end;
PLabelPLU=^LabelPLU;
/*************
SM_EANData代码
*************/
SM_EANData=record
ItemCode:array[0..9]ofchar;
BarCodeType:Integer;
BarRightData:Integer;
end;
/*************
SM_ASCII_TXT代码
*************/
SM_ASCII_TXT=record
TwsFontSize:Integer;//字体
TxtValue:array[0..99]ofchar;//文本内容
end;
/****************************************C#翻译的代码部分****************************/
[StructLayout(LayoutKind.Sequential)]
publicstructSM_EANData
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=10)]
publicstringItemCode;
[MarshalAs(UnmanagedType.U4)]
publicintBarCodeType;
[MarshalAs(UnmanagedType.U4)]
publicintBarRightData;
}
---------------------------------------------
[StructLayout(LayoutKind.Sequential)]
publicstructSM_ASCII_TXT
{
[MarshalAs(UnmanagedType.U4)]
publicintTwsFontSize;
//字体
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]
publicstringTxtValue;
}
--------------------------------------------
[StructLayout(LayoutKind.Sequential)]
publicstructLabelPLU
{
[MarshalAs(UnmanagedType.U4)]
publicintPLUNo;
publicSM_EANDataEAN_Data;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=4)]
publicSM_ASCII_TXT[]Commodity;
}
-----------------------------------------
LabelPLURecords=newLabelPLU();
Records.PLUNo=1;
SM_EANDataean=newSM_EANData();
ean.ItemCode="1234500000";
ean.BarCodeType=0;
ean.BarRightData=0;
Records.EAN_Data=ean;
SM_ASCII_TXT[]txt=newSM_ASCII_TXT[4];
txt[0].TwsFontSize=21;
txt[0].TxtValue="商品名称test";
Records.Commodity=txt;
result=SendLPlu(i,refRecords);
---------------------------------------------------
[DllImport("SMScaleAPI.dll",EntryPoint="SendLPlu")]
publicstaticexternintSendLPlu(intsocketHandle,refLabelPLUPLUData);
------------------------------------------------------------------
(ps:经测试,发现红色部分代码没生效。)
请大侠帮忙分析分析原因呢?谢谢各位先!
分 -->
|