DLL代码:
function joinstr(str1:pchar;str2:pchar):pchar;stdcall;
var
len1,len2,i:integer;
begin
len1:=strlen(str1);
len2:=strlen(str2);
for i:=0 to len1-1 do
joinstr[i]:=str1[i];
for i:=len1 to len1+len2-1 do
joinstr[i]:=str2[i];
end;
exports
joinstr;
end.调用程序代码:
type
Tjoinstr=function(var str1:pchar;str2:pchar):pchar;
......procedure TForm1.Button1Click(Sender: TObject);
var
txt1:pchar;
txt2:pchar;
moudle:thandle;
joinstr:tjoinstr;
begin
moudle:=loadlibrary('D:\chess\dll调用\delphidll.dll');
if moudle<>0 then
begin
edit2.Text:='';
@joinstr:=getprocaddress(moudle,'joinstr');
begin
txt1:=strpcopy(txt1,edit1.Text);
txt2:=strpcopy(txt2,edit2.Text);
label2.Caption:=strpas(joinstr(txt1,txt2));
end;
end;
freelibrary(moudle);
end;
end.
这本是很简单的问题,但是我现在不懂,为何就不能将edit1和edit2的字符合并到label2中来,程序运行后也没有任何的提示有错,就是label2中没有数据过来,请那位高人能指点一二!!谢过
function joinstr(str1:pchar;str2:pchar):pchar;stdcall;
var
len1,len2,i:integer;
begin
len1:=strlen(str1);
len2:=strlen(str2);
for i:=0 to len1-1 do
joinstr[i]:=str1[i];
for i:=len1 to len1+len2-1 do
joinstr[i]:=str2[i];
end;
exports
joinstr;
end.调用程序代码:
type
Tjoinstr=function(var str1:pchar;str2:pchar):pchar;
......procedure TForm1.Button1Click(Sender: TObject);
var
txt1:pchar;
txt2:pchar;
moudle:thandle;
joinstr:tjoinstr;
begin
moudle:=loadlibrary('D:\chess\dll调用\delphidll.dll');
if moudle<>0 then
begin
edit2.Text:='';
@joinstr:=getprocaddress(moudle,'joinstr');
begin
txt1:=strpcopy(txt1,edit1.Text);
txt2:=strpcopy(txt2,edit2.Text);
label2.Caption:=strpas(joinstr(txt1,txt2));
end;
end;
freelibrary(moudle);
end;
end.
这本是很简单的问题,但是我现在不懂,为何就不能将edit1和edit2的字符合并到label2中来,程序运行后也没有任何的提示有错,就是label2中没有数据过来,请那位高人能指点一二!!谢过
delphi在函数内部有个隐藏变量result,不要直接引用函数本身,递归才这样引用
你用的全是指针,指针不应该是这样str1[i],这不是串,串是数组可以这样访问,访问某位置的字符用inc或dec改变指针的指向。把函数结束的end;顶头写,我看了半天才看明白哪个end;是怎么回事。所有的pchar都没分配内存。
另外我从来没这么调用过dll的函数,不知道你这样调用dll是不是有问题
function joinstr(str1:pchar;str2:pchar):pchar;stdcall;
var
len1,len2,i:integer;
s: string;
begin
s := Str1 + Str2; GetMem(Result, Length(S) + 1); StrPCopy(Result, S);
end;exports
joinstr;
end.调用程序代码:
type
Tjoinstr=function(var str1:pchar;str2:pchar):pchar;
......procedure TForm1.Button1Click(Sender: TObject);
var
moudle:thandle;
joinstr:tjoinstr;
P: PChar;
begin
moudle:=loadlibrary('D:\chess\dll调用\delphidll.dll');
if moudle<>0 then
begin
edit2.Text:='';
joinstr:=getprocaddress(moudle,'joinstr'); P := JoinStr(PChar(Edit1.Text), PChar(Edit2.Text));
label2.Caption := P;
FreeMem(P);
end; freelibrary(moudle);
end;end.大致给你改了一下