请教一个用C#翻译Delphi出现的问题。求解。。。

[复制链接]
查看11 | 回复2 | 2021-1-27 05:33:03 | 显示全部楼层 |阅读模式
有一段代码是用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:经测试,发现红色部分代码没生效。)
请大侠帮忙分析分析原因呢?谢谢各位先!

分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:33:03 | 显示全部楼层
熬了个通宵了。。。没找出原因。。。。才疏学浅。。。还请高手支招啊。。。。
回复

使用道具 举报

千问 | 2021-1-27 05:33:03 | 显示全部楼层
你用C++调用能成功吗?我这里(C#)的是
[DllImport("SMScaleAPI.dll",
CallingConvention=CallingConvention.StdCall,
EntryPoint="SendLPlu")]
publicstaticexternInt32SendLabelPLU(Int32sock,refPluFIlePLUData);
注意参数是PluFile,不是LabelPLU.更多消息请留下你的email.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行