调用动态链接库时返回数据丢失了,赏老多分了(100分)

  • 主题发起人 主题发起人 hyxic
  • 开始时间 开始时间
H

hyxic

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位大侠:我在调用动态链接库时返回数据丢失了,具体如下:
1。动态链接库:
function resd():pchar:Export;
begin
Result:=pchar('00-12-5B-23-AD-5F');
end;
2.调用
procedure TForm1.Button1Click(Sender: TObject);
var
TFc:Function ():Pchar;stdcall; Handle:THandle;
begin
Handle:=LoadLibrary('GetSysInfo.dll');
if Handle>=32 then
TFc:= GetProcAddress(Handle,PChar('GetMacAddress')); //get Proaddres
if Assigned(TFc) then
Edit1.Text:=TFc();
FreeLibrary(Handle);
end;
结果返回的数据只有'00-12-5B-23-AD-5',最后的'F'不见了,赏老多分了
 
把返回值的类型pchar改为string不行,出错:Access voilation.......
 
function resd():pchar:Export;
begin
Result := StrAlloc(100);
StrPCopy(Result , '00-12-5B-23-AD-5F');
end;
///////////



Edit1.Text:=StrPas(TFc());
 
//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.
--修改一下就可以拉!
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部