C#调用delphi编写的DLL参数是结构体

「已注销」 2009-07-16 09:40:47

//delphi编写的DLL函数
function ReportServer(const ServerTypeParams: TServerTypeParams; var RetValue: PChar;var RetValueLen: Integer): Boolean;stdcall;
//TServerTypeParams结构体的定义
TServerTypeParams= record
ServerType: PChar;
Template: PChar;
DefaultTemplate: PChar;
end;

在ASP.net(C#)中调用DLL提出ReportServer函数

//对应结构体定义
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi,Pack=1)]
public struct TServerTypeParams
{
[MarshalAs(UnmanagedType.LPStr)]
public string ServerType;
[MarshalAs(UnmanagedType.LPStr)]
public string Template;
}


这样传送过去后delphi里跟踪根本就没有,忘高手指点指点。
...全文
266 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
minizhuxianchun 2009-07-17
  • 打赏
  • 举报
回复
不加PACKED的作用是边界对齐,加快访问速度.如果结构体内成员变量类型大小总和是5,那这个结构体的大小就是6(可能是8,我忘了,反正是增大了).
加PACKED后,结构体内成员变量类型大小总和大小是多少那结构体的大小就是就是多少.
所以在asp.net(C#)里的结构体定义应该还是原来的定义.
「已注销」 2009-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 minizhuxianchun 的回复:]
//delphi编写的DLL函数
function ReportServer(const ServerTypeParams: TServerTypeParams; var RetValue: PChar;var RetValueLen: Integer): Boolean;stdcall;
//TServerTypeParams结构体的定义
TServerTypeParams= record
ServerType: PChar;
Template: PChar;
DefaultTemplate: PChar;
end;
换成这样看看:
TServerTypeParams= Packed record
ServerType: PChar;
Template: …
[/Quote]
这个我是想到了,关键是在asp.net(C#)里的结构体怎么定义。
「已注销」 2009-07-16
  • 打赏
  • 举报
回复

//对应结构体定义,上面发帖的时候少黏贴了一个DefaultTemplate
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi,Pack=1)]
public struct TServerTypeParams
{
[MarshalAs(UnmanagedType.LPStr)]
public string ServerType;
[MarshalAs(UnmanagedType.LPStr)]
public string Template;
[MarshalAs(UnmanagedType.LPStr)]
public string DefaultTemplate;
}




minizhuxianchun 2009-07-16
  • 打赏
  • 举报
回复
//delphi编写的DLL函数
function ReportServer(const ServerTypeParams: TServerTypeParams; var RetValue: PChar;var RetValueLen: Integer): Boolean;stdcall;
//TServerTypeParams结构体的定义
TServerTypeParams= record
ServerType: PChar;
Template: PChar;
DefaultTemplate: PChar;
end;
换成这样看看:
TServerTypeParams= Packed record
ServerType: PChar;
Template: PChar;
DefaultTemplate: PChar;
end;

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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