dll中的函数为: function transstr(str1:PChar;str2:PChar):PChar;stdcall; var returnchar:PChar; rstr1,rstr2:string; returnstr:string; begin rstr1:= str1; rstr2:=str2; returnstr:=rstr1+rstr2; returnchar:=StrAlloc(80); returnchar:=strpcopy(returnchar,rstr1); result:=returnchar; end; 调用的情况如下: procedure TForm1.Button1Click(Sender: TObject); var chang:PChar; strin,strin1:string; pc1,pc2:PChar; begin strin1:='hello'; strin:='how can i do that'; pc1:=StrAlloc(80); pc1:=strpcopy(pc1,strin); pc2:=StrAlloc(80); pc2:=strpcopy(pc2,strin1); chang:=transstr(pc1,pc2); edit1.Text:=chang; end; 返回的是乱码字符,各位大大帮帮忙呀
呵呵,你的功能太大! 写一个传递字符的例子吧dll函数 procedure PassChar(pDemo1,pDemo2,pRet:Pchar);stdcall; var Str1,Str2,StrRet:String; begin Str1:=StrPas(pDemo1); Str2:=StrPas(pDemo2); StrRet:=Format('%s %s',[Str1,Str2]); StrPCopy(pRet,StrRet); end; 调用procedure TForm1.Button2Click(Sender: TObject); var pDemo1,pDemo2,pRet:Pchar; begin pDemo1:=StrAlloc(80); pDemo2:=StrAlloc(80); pRet:=StrAlloc(80); StrCopy(pDemo1,'hello!'); StrCopy(pDemo2,'how can i do that'); PassChar(pDemo1,pDemo2,pRet); edit1.Text:=StrPas(pRet); StrDispose(pDemo1); StrDispose(pDemo2); StrDispose(pRet); end;
我用delphi6,运行时,报错是:‘access violation at 0x77f96b9f:write of address 0x00030ffc ’写地址错误,不知道怎么搞的
function transstr(str1:PChar;str2:PChar):PChar;stdcall;
var
returnchar:PChar;
rstr1,rstr2:string;
returnstr:string;
begin
rstr1:= str1;
rstr2:=str2;
returnstr:=rstr1+rstr2;
returnchar:=StrAlloc(80);
returnchar:=strpcopy(returnchar,rstr1);
result:=returnchar;
end;
调用的情况如下:
procedure TForm1.Button1Click(Sender: TObject);
var
chang:PChar;
strin,strin1:string;
pc1,pc2:PChar;
begin
strin1:='hello';
strin:='how can i do that';
pc1:=StrAlloc(80);
pc1:=strpcopy(pc1,strin);
pc2:=StrAlloc(80);
pc2:=strpcopy(pc2,strin1);
chang:=transstr(pc1,pc2);
edit1.Text:=chang;
end;
返回的是乱码字符,各位大大帮帮忙呀
写一个传递字符的例子吧dll函数
procedure PassChar(pDemo1,pDemo2,pRet:Pchar);stdcall;
var
Str1,Str2,StrRet:String;
begin
Str1:=StrPas(pDemo1);
Str2:=StrPas(pDemo2);
StrRet:=Format('%s %s',[Str1,Str2]);
StrPCopy(pRet,StrRet);
end;
调用procedure TForm1.Button2Click(Sender: TObject);
var
pDemo1,pDemo2,pRet:Pchar;
begin
pDemo1:=StrAlloc(80);
pDemo2:=StrAlloc(80);
pRet:=StrAlloc(80); StrCopy(pDemo1,'hello!');
StrCopy(pDemo2,'how can i do that');
PassChar(pDemo1,pDemo2,pRet); edit1.Text:=StrPas(pRet); StrDispose(pDemo1);
StrDispose(pDemo2);
StrDispose(pRet);
end;