C#调用delphi写的DLL

爱LOVE大葱 2013-11-24 07:55:36
我用C#调用delphi写的DLL,但是一个方法不知道怎么定义,求大家帮帮忙。

一. delphi中的函数名称
function GetData( pt: Data): LongInt;//读取数据

delphi数据结构如下:
Data = record
id: LongInt; //编码
Type: LongInt; //类型
Time: LongInt; //时间
Value: LongInt; //值
end;


请问大家,在C#总怎么定义,我是这样定义的,但是好像有问题:
[DllImport("DELPHI.dll")]
public static extern void GetData(int[] Datas);
...全文
155 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sololie 2013-11-24
  • 打赏
  • 举报
回复
我的话就它参数声明为IntPtr,类似这样 public static extern int GetData(IntPtr Datas); Data d; d.id = 666666; d.type = 55555; d.time = 1222; d.value = 22222; IntPtr pdata = Marshal.AllocHGlobal(Marshal.SizeOf(d)); Marshal.StructureToPtr(d, pdata, false); int iRes = GetData(pdata); Marshal.FreeHGlobal(pdata); .....
sololie 2013-11-24
  • 打赏
  • 举报
回复
结果正确就行了
sololie 2013-11-24
  • 打赏
  • 举报
回复
intPtr需要给它分配非托管内存 Data d; d.id = xxxx; ...... IntPtr pdata = Marshal.AllocHGlobal(Marshal.SizeOf(d));
爱LOVE大葱 2013-11-24
  • 打赏
  • 举报
回复
引用 1 楼 sololie 的回复:
function GetData( pt: Data): LongInt;// LongInt 是返回值 Data = record // 相当于c#的结构,你用类应该也没问题,不过最好用结构吧,结构是值类型的 id: LongInt; //编码 Type: LongInt; //类型 Time: LongInt; //时间 Value: LongInt; //值 end struct Data { int id,type,time,value; } Data d; d.id = xxx; d.type = xxx; timer = xxx; value = xxx; public static extern int GetData(Data d);
引用 1 楼 sololie 的回复:
function GetData( pt: Data): LongInt;// LongInt 是返回值 Data = record // 相当于c#的结构,你用类应该也没问题,不过最好用结构吧,结构是值类型的 id: LongInt; //编码 Type: LongInt; //类型 Time: LongInt; //时间 Value: LongInt; //值 end struct Data { int id,type,time,value; } Data d; d.id = xxx; d.type = xxx; timer = xxx; value = xxx; public static extern int GetData(Data d);
大哥,你写的太详细了,非常感谢您。还有我的struct是这样new的(Data d = new Data),然后调用public static extern int GetData(Data d); 跟你那样不一样,我这样有什么问题吗?
爱LOVE大葱 2013-11-24
  • 打赏
  • 举报
回复
求救。。。
爱LOVE大葱 2013-11-24
  • 打赏
  • 举报
回复
引用 2 楼 sololie 的回复:
忘了加上public struct Data { public int id,type,time,value; }
大哥,我也这样定义了试了一下,好像会报错:尝试读取或写入受保护的内存。
sololie 2013-11-24
  • 打赏
  • 举报
回复
忘了加上public struct Data { public int id,type,time,value; }
sololie 2013-11-24
  • 打赏
  • 举报
回复
function GetData( pt: Data): LongInt;// LongInt 是返回值 Data = record // 相当于c#的结构,你用类应该也没问题,不过最好用结构吧,结构是值类型的 id: LongInt; //编码 Type: LongInt; //类型 Time: LongInt; //时间 Value: LongInt; //值 end struct Data { int id,type,time,value; } Data d; d.id = xxx; d.type = xxx; timer = xxx; value = xxx; public static extern int GetData(Data d);

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧