delphi2010 char为何无法于VC的 CAHR大小一致。

jowers 2010-12-10 05:40:43

    我用delphi2010定义了一个结构体。

type
sthinfo = record
name: array[0..$20] of pansichar;
id : dword;
type: dword;
end;
Psthinfo = ^sthinfo;


var DllHandle:THandle;
Pro:Procedure (mon:Psthinfo); cdecl;
i:integer;
sth:array[0..3] of sthinfo;
begin
DllHandle := LoadLibrary('DelphiDll.Dll');

if DllHandle<>0 then
begin
@Pro := GetProcAddress(DllHandle,pchar('Getsthinfo'));
Pro(@sth);

for I := 0 to 2 do
begin
showmessage(mon[i].name);
Showmessage(inttohex(mon[i].id,4));
Showmessage(inttohex(mon[i].m_type,4));
end;
end;
end;



vc我也定义了同样的结构体

typedef struct _sthinfo
{
DWORD id;
DWORD type;
char name[0x20];
}sthinfo;


void GetMoninfo(sthinfo *sth)
{

sth[0].id = 1;

strncpy(sth[0].name,"1号",0x20);

sth[0].type = 0x1f;

sth[1].id = 2;

strncpy(sth[1].name,"2号",0x20);

sth[1].type = 0x2f;

sth[2].id = 3;

strncpy(sth[2].name,"3号",0x20);

sth].type = 0x3f;

return ;
};



如果我把结构体的名字不要。DELPHI得出来的结果正常。但是加上名字。第一次出来的名字是对的。后面的就不一样了。
求解。





...全文
169 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjxboy 2011-03-10
  • 打赏
  • 举报
回复
呵呵,很容易的问题,不过确实容易犯这样错。。。以后注意了!接分int
ABLE__323 2011-02-19
  • 打赏
  • 举报
回复
2010的CHAR默认是WIDECHAR
mihi1978 2010-12-27
  • 打赏
  • 举报
回复
楼上都是正解,我来接分。
wzwcn 2010-12-11
  • 打赏
  • 举报
回复
不对,Delphi的结构应该是这样才对:
type
sthinfo = record
name: array[0..$20-1] of ansichar;//注意这里
id : dword;
type: dword;
end;
Mit1208 2010-12-10
  • 打赏
  • 举报
回复
接分........
weixiaohua 2010-12-10
  • 打赏
  • 举报
回复
好吧,接分.
zhaodog 2010-12-10
  • 打赏
  • 举报
回复
呵呵,接分了
bdmh 2010-12-10
  • 打赏
  • 举报
回复
array[0..$20],这是0..32,是33个字节,而C中char name[0x20],是char name[32],是32个字节
jowers 2010-12-10
  • 打赏
  • 举报
回复
丢人的问题。我长度定义得不一样。。尴尬啊。大家过来接分吧。低级错误。

name: array[0..$20-1] of pansichar;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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