Dll中传入PChar类型的参数,在dll中为何得到的是乱码?

sky_234 2005-03-01 02:57:30
我在dll中的函数类型如下:
function GetReturnStr(Source: PChar; Dest: PChar): Integer; stdcall;
在调用程序中,把值赋给Source,但在dll中得到Source是乱码?是怎么回事啊?
...全文
177 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
SKYGUARDER 2005-03-01
  • 打赏
  • 举报
回复
调用代码如下:

type
TGetReturnStr = function(Source: PChar; Dest: PChar): Integer;stdcall;
......
如果还是不行,可以试试把stdcall换为cdecl;
suncheng 2005-03-01
  • 打赏
  • 举报
回复
记着给分!
sky_234 2005-03-01
  • 打赏
  • 举报
回复
解决了,把调用端的Dest :=PChar(S);改成 StrPCopy(Dest,S);就解决了,谢谢大家,结帖!!!
suncheng 2005-03-01
  • 打赏
  • 举报
回复
请说清楚点 哪个参数返回?
sky_234 2005-03-01
  • 打赏
  • 举报
回复
谢谢楼上,参数传入已经解决,但参数返回的还是不行,请帮忙!
suncheng 2005-03-01
  • 打赏
  • 举报
回复
type
TGetReturnStr = function(Source: PChar; Dest: PChar): Integer;
改为
type
TGetReturnStr = function(Source: PChar; Dest: PChar): Integer;stdcall;
sky_234 2005-03-01
  • 打赏
  • 举报
回复
i :=aptr(Source1,Dest1); 编译通过不了啊?
sky_234 2005-03-01
  • 打赏
  • 举报
回复
在Dll中已经给Dest赋值了,Dest :=PChar(S);但在调用端得到的Dest也是乱码,这是怎么回事啊?
suncheng 2005-03-01
  • 打赏
  • 举报
回复
我单步跟踪调试一下!
zhaojinghui 2005-03-01
  • 打赏
  • 举报
回复
这样试试
library DllOne;

uses
SysUtils,
// ShareMem,
Classes,
Dialogs;

{$R *.res}

function GetReturnStr(Source: PChar; Dest: PChar): Integer; //
var S: string;
begin
S :=StrPas(Source);
S :='DllOne---'+S;
Dest :=PChar(S);
Result :=1;
end;

exports
GetReturnStr;

begin
end.


调用代码如下:

type
TGetReturnStr = function(Source: PChar; Dest: PChar): Integer;

procedure TForm1.bDLLOneClick(Sender: TObject);
var aptr: TFarproc;
lhnd: THandle;
i: integer;
Source1,Dest1: PChar;
S: string;
begin
lhnd :=LoadLibrary('DllOne.dll');
if lhnd>32 then
begin
try
GetMem(Source1,Length(trim(mSource.Text))+1);
StrPCopy(Source1,trim(mSource.Text));
Dest1 := '';
aptr :=GetprocAddress(lhnd,'GetReturnStr');
if (aptr<>nil) then
begin
i :=aptr(Source1,Dest1);
mDest.Text :=intToStr(i)+'---'+StrPas(Dest1);
end;
finally
FreeLibrary(lhnd);
end;
end;
end;
楚人无衣 2005-03-01
  • 打赏
  • 举报
回复
没用var方式用过,其实你可以让function返回一个PAnsiChar类型的值呀然后再用StrPas处理
sky_234 2005-03-01
  • 打赏
  • 举报
回复
我已经把mSource.text赋给Source1了啊?
sky_234 2005-03-01
  • 打赏
  • 举报
回复
StrPCopy(Source1,trim(mSource.Text));
这条语句给它赋值了啊?
suncheng 2005-03-01
  • 打赏
  • 举报
回复
i :=TGetReturnStr(aptr)(Source1,Dest1)
此句之前没有付Source1值!他不知指到哪儿去了所以是乱码!
sky_234 2005-03-01
  • 打赏
  • 举报
回复
在上面dll的代码中,返回的Dest也是乱码,到底是怎么回事啊?
sky_234 2005-03-01
  • 打赏
  • 举报
回复
在上面dll的代码中,S :=StrPas(Source);其S得到的是乱码!
sky_234 2005-03-01
  • 打赏
  • 举报
回复
代码如下:
library DllOne;

uses
SysUtils,
// ShareMem,
Classes,
Dialogs;

{$R *.res}

function GetReturnStr(Source: PChar; Dest: PChar): Integer; stdcall; //
var S: string;
begin
S :=StrPas(Source);
S :='DllOne---'+S;
Dest :=PChar(S);
Result :=1;
end;

exports
GetReturnStr;

begin
end.


调用代码如下:

type
TGetReturnStr = function(Source: PChar; Dest: PChar): Integer;

procedure TForm1.bDLLOneClick(Sender: TObject);
var aptr: TFarproc;
lhnd: THandle;
i: integer;
Source1,Dest1: PChar;
S: string;
begin
lhnd :=LoadLibrary('DllOne.dll');
if lhnd>32 then
begin
try
GetMem(Source1,Length(trim(mSource.Text))+1);
StrPCopy(Source1,trim(mSource.Text));
Dest1 := '';
aptr :=GetprocAddress(lhnd,'GetReturnStr');
if (aptr<>nil) then
begin
i :=TGetReturnStr(aptr)(Source1,Dest1);
mDest.Text :=intToStr(i)+'---'+StrPas(Dest1);
end;
finally
FreeLibrary(lhnd);
end;
end;
end;

5,392

社区成员

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

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