//dll部分
library dllmem;
uses
SysUtils,
Classes;
var
G_Buffer: PChar;
{$R *.res}
function AlloMem: PChar;
var
I: Integer;
begin
GetMem(G_Buffer, 100000);
for I := 0 to 99999 do
G_Buffer := 'A';
Result := G_Buffer;
end;
function DealMem: Integer;
begin
Result := 0;
try
FreeMem(G_Buffer, 100000);
except
Exit;
end;
Result := 1;
end;
exports
AlloMem,
DealMem;
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;
implementation
uses Math;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
type
TAlloMem = function: PChar;
TDealMem = function: Integer;
var
AlloMem: TAlloMem;
DealMem: TDealMem;
dllHandle: Cardinal;
Ph: PChar;
begin
dllHandle := LoadLibrary(PChar('F:/guiganghospMS/testdll/新建文件夹/dllmem.dll'));
If dllHandle <= 0 then Exit;
AlloMem := GetProcAddress(dllHandle, Pchar('AlloMem'));
DealMem := GetProcAddress(dllHandle, Pchar('DealMem'));
if (@AlloMem = nil) or (@DealMem = nil) then
begin
FreeLibrary(dllHandle);
Exit;
end;
try
Ph := AlloMem; {dll分配内存}
ShowMessage(IntToStr(length(StrPas(Ph)))); //返回了100000
finally
DealMem; {dll释放内存}
end;
end;
end.