动态库中的函数为 read_card ,其形参 outStr 为STRING型变量,返回一个字型串给调用者具体代码如下:一、动态链接库
library card_yj;{ 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,SysUtils,
Classes;{$R *.res} function read_card(var outStr:string):integer;export;stdcall;
begin
outStr:='i love you';
result:=1;
end; exports
read_card;begin
end.二、引用单元unit Unit1;interfaceuses
ShareMem,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;
function read_card(var outStr:string):integer;stdcall;external 'card.dll';
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
a:string;
b:integer;
begin
b:=read_card(a);
showmessage(a);
end;end.在程序中调用DLL中的read_card函数没有异常,showmessage(a)时,提示'i love you',也就是说函数调用正常。 但当程序退出时,总是出现错误提示:
project1.exe faulted with message:'access biolation at 0x0038617d:write of address 0x00030e00'. Process Stopped.Use Step or Run to continue.经测试,不调用DLL中read_card函数,程序运行退出时也出现上述提示。
library card_yj;{ 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,SysUtils,
Classes;{$R *.res} function read_card(var outStr:string):integer;export;stdcall;
begin
outStr:='i love you';
result:=1;
end; exports
read_card;begin
end.二、引用单元unit Unit1;interfaceuses
ShareMem,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;
function read_card(var outStr:string):integer;stdcall;external 'card.dll';
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
a:string;
b:integer;
begin
b:=read_card(a);
showmessage(a);
end;end.在程序中调用DLL中的read_card函数没有异常,showmessage(a)时,提示'i love you',也就是说函数调用正常。 但当程序退出时,总是出现错误提示:
project1.exe faulted with message:'access biolation at 0x0038617d:write of address 0x00030e00'. Process Stopped.Use Step or Run to continue.经测试,不调用DLL中read_card函数,程序运行退出时也出现上述提示。
sharemem, // <---
Forms,
Unit1 in 'Unit1.pas' {Form1};
小姐,我是叫你把SHAREMEM写到程序的DRP里面...
program Project1;uses
sharemem, // <-- 在这里加
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.