关于dll参数的一个问题。各位大虾帮帮忙了。

sunkyling 2003-06-10 06:01:08
我用delphi写了一个dll
其中有个函数是这样的
function aaa(var pst:pchar):integer;stdcall
var str:string
begin
str='ILOVEYOU'
pst:=Pchar(str);//强制转换
showmessage(pst)//显示真确。为ILOVEYOU
result:=1;
end;
我在vb里调用的时候
declare Function aaa Lib "aaa.dll" (ByVal b As String) As Integer

dim b as string;
dim z as integer;
b=space(255);
z=aaa(b)
msgbox b
结果显示的乱ma

z = aaa(b)
结果返回的值
...全文
41 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dnazj 2003-06-22
  • 打赏
  • 举报
回复
你这么写当然错了.

pst只是指向了str的指针(实际上是@str[1]), 离开函数的时候str就不存在了,也就是实际的数据并没有copy到你传入的字符串缓冲区,而且你连传入的pst这个指针都改变了.

const
MAX_SIZE = 传出的最大缓冲区大小
function aaa(const pst:pchar):integer;stdcall
var str:string
begin
str='ILOVEYOU';
StrPLCopy(pst, str, MAX_SIZE-1);
showmessage(pst)//显示真确。为ILOVEYOU
result:=1;
end;
sunkyling 2003-06-11
  • 打赏
  • 举报
回复
我用delphi写了一个dll
其中有个函数是这样的
function aaa(var pst:pchar):integer;stdcall
var str:string
begin
str='ILOVEYOU'
pst:=Pchar(str);//强制转换
showmessage(pst)//显示真确。为ILOVEYOU
result:=1;
end;

请问如何调用。
我这样写的 但是返回的值是错误的。

var a:array[0..8] of char;
z:integer;
begin
z:=aaa(a);
showmessage(a);
end;
结果显示的是乱码 而与dll中本身SHOW出来得值不一致。
henry2003 2003-06-10
  • 打赏
  • 举报
回复
Up
sunkyling 2003-06-10
  • 打赏
  • 举报
回复
我知道在vb里 用
declare Function aaa Lib "aaa.dll" (Byref b As String) As Integer 能够真确显示,但byref b as string 和byval b as string 完全是一样的。
eaglezhao 2003-06-10
  • 打赏
  • 举报
回复
ByVal b As String 参数生命不正确 应该为byref 或者不写

pst:=Pchar(str);最好用strPCopy函数
Linux2001 2003-06-10
  • 打赏
  • 举报
回复
VB的东西忘光了,你Delphi的代码没有问题

5,392

社区成员

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

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