首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

新手求教,DLL怎么传递和返回STRING

2013-03-12 
新手求教,DLL如何传递和返回STRING.百度了有关这个问题的内容,很多前辈都说,DLL不适合传递和返回STRING类

新手求教,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;

[解决办法]
Dll部份
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.

调用程序
*.dpr
program Project1;

uses
  sharemem,{引用shareMem}Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

.Pas

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.


[解决办法]
传PChar类型把变量S改成Pchar类型,
Dll代码    
s:=IntToStr(iNum);改成 s:=Pchar(IntToStr(iNum));
[解决办法]
Dll传参类型Pchar

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.

热点排行