在C#中调用Delphi的动态库系统直接退出了,由于对Delphi完全不了所以现在完全不知道怎么办
如题动态库中的方法为:function_zdw_write(wd:zdw_down;varcon_buf:ARR):Integer;con_buf:为ARR:array[...
如题
动态库中的方法为:
function _zdw_write(wd :zdw_down; var con_buf:ARR):Integer;
con_buf:为 ARR:array[0..128] of byte;
dw_down 在Delphi中定义为:
zdw_down = record
cardType :Integer;
buy :Integer;
cardID :Integer;
subareaID :Integer;
alarm1 :Integer;
XL :Integer;
DBCS :Integer;
alarm2 :Integer;
YXSBJ :Integer;
BuyCount :Integer;
JM1 :Integer;
JM2 :Integer;
CSBH :Integer;
end;
我将dw_down在C#为定义为这样子:
struct dw_down
{
public int cardType;
public int buy;
public int cardID;
public int subareaID;
public int alarm1;
public int XL;
public int DBCS;
public int alarm2;
public int YXSBJ;
public int BuyCount;
public int JM1;
public int JM2;
public int CSBH;
}
引用代码如下:
[DllImport("ZDW_DLL.dll", EntryPoint = "_zdw_write", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern Int16 _zdw_write(dw_down wd,ref byte[] r);
调用的代码:
……
byte[] data1 = new byte[128];
dw_down dw = new dw_down();
dw.cardType = 1;
dw.buy = 100;
dw.cardID = 10;
dw.subareaID = 1;
dw.alarm1 = 10;
dw.XL = 1;
dw.DBCS = 110;
dw.alarm2 = 10;
dw.YXSBJ = 1;
dw.BuyCount = 1;
dw.JM1 = 1;
dw.JM2 = 1;
dw.CSBH = 100;
ZDW._zdw_write(dw,ref data1);
……
程序运行到 ZDW._zdw_write(dw,ref data1);这里时直接就退出了。 展开
动态库中的方法为:
function _zdw_write(wd :zdw_down; var con_buf:ARR):Integer;
con_buf:为 ARR:array[0..128] of byte;
dw_down 在Delphi中定义为:
zdw_down = record
cardType :Integer;
buy :Integer;
cardID :Integer;
subareaID :Integer;
alarm1 :Integer;
XL :Integer;
DBCS :Integer;
alarm2 :Integer;
YXSBJ :Integer;
BuyCount :Integer;
JM1 :Integer;
JM2 :Integer;
CSBH :Integer;
end;
我将dw_down在C#为定义为这样子:
struct dw_down
{
public int cardType;
public int buy;
public int cardID;
public int subareaID;
public int alarm1;
public int XL;
public int DBCS;
public int alarm2;
public int YXSBJ;
public int BuyCount;
public int JM1;
public int JM2;
public int CSBH;
}
引用代码如下:
[DllImport("ZDW_DLL.dll", EntryPoint = "_zdw_write", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern Int16 _zdw_write(dw_down wd,ref byte[] r);
调用的代码:
……
byte[] data1 = new byte[128];
dw_down dw = new dw_down();
dw.cardType = 1;
dw.buy = 100;
dw.cardID = 10;
dw.subareaID = 1;
dw.alarm1 = 10;
dw.XL = 1;
dw.DBCS = 110;
dw.alarm2 = 10;
dw.YXSBJ = 1;
dw.BuyCount = 1;
dw.JM1 = 1;
dw.JM2 = 1;
dw.CSBH = 100;
ZDW._zdw_write(dw,ref data1);
……
程序运行到 ZDW._zdw_write(dw,ref data1);这里时直接就退出了。 展开
展开全部
C#你告诉编译器用stdcall约定
但是D里你用的是默认的register,不是stdcall的
D里改成 function _zdw_write(wd :zdw_down; var con_buf:ARR):Integer; stdcall
但是D里你用的是默认的register,不是stdcall的
D里改成 function _zdw_write(wd :zdw_down; var con_buf:ARR):Integer; stdcall
追问
Delphi的动态库是别人给的 编译好的,改不了。只能改C#的代码,请问一下要怎么改才能正常调用
追答
你把C#的声明改成CallingConvention.ThisCall试试,我不确定啊
如果不行,可能你得写个stdcall或cdecl的封装给C#用。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询