N
newyj
Unregistered / Unconfirmed
GUEST, unregistred user!
如何实现在dll 中分配内存 和释放
///////////dll
library PrjDll;
uses
windows, SysUtils, Classes;
{$R *.RES}
function file2char(var Pchar):integer
stdcall;export;
var
fm:Tfilestream;
begin
//MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
fm:=Tfilestream.Create('1.txt',fmopenread);
result := fm.size+1;
getMem(p,fm.size+1);
//MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
fm.read(p,fm.size);
fm.Free;
end;
procedure delmem(pchar);
begin
freemem(p);
end;
exports
file2char,
delmem;
begin
end.
//测试程序
function file2char(var Pchar):integer
stdcall;external 'PrjDll.DLL';
procedure delmem(pchar)
stdcall;external 'PrjDll.DLL';
var
Form1: TForm1;
implementation
var
pchar;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
isize:integer;
begin
//getmem(p,8)
//如果分配的内存比实际需要的大没问题,小了就不行;
//我想知道dll中如何去改变内存大小;好像ReallocMem不行
//p[0]:=#0;
// MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
isize:=file2char(p);
// MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
if isize>0 then
begin
richedit1.Clear;
richedit1.Lines.add(p);
end
else richedit1.Lines.add('dfadfdas');
// freemem(p,isize);
delmem(p);
p:=nil;
end;
end.
///////////dll
library PrjDll;
uses
windows, SysUtils, Classes;
{$R *.RES}
function file2char(var Pchar):integer
stdcall;export;
var
fm:Tfilestream;
begin
//MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
fm:=Tfilestream.Create('1.txt',fmopenread);
result := fm.size+1;
getMem(p,fm.size+1);
//MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
fm.read(p,fm.size);
fm.Free;
end;
procedure delmem(pchar);
begin
freemem(p);
end;
exports
file2char,
delmem;
begin
end.
//测试程序
function file2char(var Pchar):integer
stdcall;external 'PrjDll.DLL';
procedure delmem(pchar)
stdcall;external 'PrjDll.DLL';
var
Form1: TForm1;
implementation
var
pchar;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
isize:integer;
begin
//getmem(p,8)
//如果分配的内存比实际需要的大没问题,小了就不行;
//我想知道dll中如何去改变内存大小;好像ReallocMem不行
//p[0]:=#0;
// MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
isize:=file2char(p);
// MessageBox(0, PChar(IntToHex(Longint(p), 8)), '', MB_OK);
if isize>0 then
begin
richedit1.Clear;
richedit1.Lines.add(p);
end
else richedit1.Lines.add('dfadfdas');
// freemem(p,isize);
delmem(p);
p:=nil;
end;
end.