我做了一个最简单的动态库。
DLL: 用一个名称为hi的function返回string 'hello'
调试程序:用一个按钮来调用动态库的函数hi结果.......我得到了hello,可是随后却报了一个invalid pointer operation!!!我没有用指针啊,Delphi老大,俺冤枉啊~~~~~~~~~~ 555555我的源程序如下,请大虾指点啊:
//***********************************************
//调试程序的 Project1
//************************************************
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.//***********************************************
//调试程序的 Unit1
//************************************************unit Unit1;interfaceuses
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;implementation
function hi():string; external 'testdll.dll';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Format('%x', [Longint(@hi)]));
ShowMessage(hi());
end;end.//*********************************************
// DLL端的 library testdll
//*********************************************
library testdll;{ 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,
Classes,
hello in 'hello.pas';{$R *.res}
exports hi;
begin
end.//*********************************************
// DLL端的 unit hello
//*********************************************
unit hello;interface
uses Dialogs;
function hi():string;
implementation
function hi():string;
begin
hi := 'hello';
end;end.
DLL: 用一个名称为hi的function返回string 'hello'
调试程序:用一个按钮来调用动态库的函数hi结果.......我得到了hello,可是随后却报了一个invalid pointer operation!!!我没有用指针啊,Delphi老大,俺冤枉啊~~~~~~~~~~ 555555我的源程序如下,请大虾指点啊:
//***********************************************
//调试程序的 Project1
//************************************************
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.//***********************************************
//调试程序的 Unit1
//************************************************unit Unit1;interfaceuses
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;implementation
function hi():string; external 'testdll.dll';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Format('%x', [Longint(@hi)]));
ShowMessage(hi());
end;end.//*********************************************
// DLL端的 library testdll
//*********************************************
library testdll;{ 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,
Classes,
hello in 'hello.pas';{$R *.res}
exports hi;
begin
end.//*********************************************
// DLL端的 unit hello
//*********************************************
unit hello;interface
uses Dialogs;
function hi():string;
implementation
function hi():string;
begin
hi := 'hello';
end;end.
解决。
在DLL中无论输出函数的返回值或者参数如果用String,必须把ShareMM放在DLL和调用DLL的程序Uses之后,并且分发程序时,brlndMem.DLL必须一起发放。
还有,你可试试这样看如何?
procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
ShowMessage(Format('%x', [Longint(@hi)]));
s := hi;
ShowMessage(s);
dispose(s);
end;