新手求教,DLL如何传递和返回STRING.
百度了有关这个问题的内容,很多前辈都说,DLL不适合传递和返回STRING类型。要改成PCHAR之类的,具体是怎么做的,请有空的老师帮忙举个传递PCHAR的例子 谢谢。
[解决办法]
生成DLL的时候就有系统提示,注意看看
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
//********取得当前可执行文件名
procedure GetApplicationName(var Lp:String);StdCall;
begin
StrCopy(PChar(Lp),PChar(ChangeFileExt(ExtractFileName(Application.ExeName),'')));
end;library OutStringDll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
ShareMem,{引用ShareMem}SysUtils, Dialogs,
Classes;
{$R *.res}
{不建议用string作Dll参数传递或返回值}
procedure GetNumString(iNum:Integer;var s:string);stdcall;
begin
s:=IntToStr(iNum);
end;
exports
GetNumString;
begin
end.
program Project1;
uses
sharemem,{引用shareMem}Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure GetNumString(iNum: Integer; var s: string); stdcall;
implementation
{$R *.dfm}
procedure GetNumString(iNum: Integer;var s: string); external 'OutStringDll.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
i: integer;
begin
i := 8899;
GetNumString(i, s);
ShowMessage(s);
end;
end.
library OutStringDll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils, Dialogs,
Classes;
{$R *.res}
procedure GetNumString(iNum:Integer;var s:PChar);stdcall;
begin
s:=PChar(IntToStr(iNum));
end;
exports
GetNumString;
begin
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure GetNumString(iNum: Integer; var s: PChar); stdcall;
implementation
{$R *.dfm}
procedure GetNumString(iNum: Integer;var s: pchar);external 'OutStringDll.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
s:PChar;
begin
GetNumString(8899, s);
ShowMessage(s);
end;
end.