dll code
--------
library SerReger;uses
SysUtils,
Classes,
UCom in 'UCom.pas';var
Info:THardwareInfo;{$R *.res}function Test1(a,b:integer):integer;
begin
Result:=a+b;
end;function Test2(tx:string):string;
begin
Result:=tx+'b';
end;function ResPWDStr(Key:string):string;
begin
Result := key+'xxx';//
end;exports
Test1,Test2;begin
end.--call code --function TfrmSysControl.testr(a,b:integer):integer;
var
ReStr:function(a,b:integer):integer;
DllHandle:hmodule;
DllName:pchar;
begin
DllName :=pchar(strPath+'SerReger.dll');
DllHandle:=LoadLibrary(dllName);
if DllHandle >0 then
try
//@ReStr:=GetProcAddress(DllHandle,'Test1');
@ReStr:=GetProcAddress(DllHandle,'Test1');
if @ReStr<>nil then
Result := ReStr(a,b);
finally
FreeLibrary(DllHandle);
end
else
ShowMessage('dll没有找到');
end;
function TfrmSysControl.test(Key:string):string;
var
ReStrx:function(Key:string):string;
DllHandle:hmodule;
DllName:pchar;
begin
DllName :=pchar(strPath+'SerReger.dll');
DllHandle:=LoadLibrary(dllName);
if DllHandle >0 then
try
@ReStrx:=GetProcAddress(DllHandle,'Test2');
if @ReStrx<>nil then
Result := ReStrx(Key);
finally
FreeLibrary(DllHandle);
end
else
ShowMessage('dll没有找到');
end;
--问题--
为何在用test1 是不会报内存地址错误,而在调test2是却报内存地址错误。
Showmessage(test('Text'));
showmessage(inttostr(testr(1,3)));
--------
library SerReger;uses
SysUtils,
Classes,
UCom in 'UCom.pas';var
Info:THardwareInfo;{$R *.res}function Test1(a,b:integer):integer;
begin
Result:=a+b;
end;function Test2(tx:string):string;
begin
Result:=tx+'b';
end;function ResPWDStr(Key:string):string;
begin
Result := key+'xxx';//
end;exports
Test1,Test2;begin
end.--call code --function TfrmSysControl.testr(a,b:integer):integer;
var
ReStr:function(a,b:integer):integer;
DllHandle:hmodule;
DllName:pchar;
begin
DllName :=pchar(strPath+'SerReger.dll');
DllHandle:=LoadLibrary(dllName);
if DllHandle >0 then
try
//@ReStr:=GetProcAddress(DllHandle,'Test1');
@ReStr:=GetProcAddress(DllHandle,'Test1');
if @ReStr<>nil then
Result := ReStr(a,b);
finally
FreeLibrary(DllHandle);
end
else
ShowMessage('dll没有找到');
end;
function TfrmSysControl.test(Key:string):string;
var
ReStrx:function(Key:string):string;
DllHandle:hmodule;
DllName:pchar;
begin
DllName :=pchar(strPath+'SerReger.dll');
DllHandle:=LoadLibrary(dllName);
if DllHandle >0 then
try
@ReStrx:=GetProcAddress(DllHandle,'Test2');
if @ReStrx<>nil then
Result := ReStrx(Key);
finally
FreeLibrary(DllHandle);
end
else
ShowMessage('dll没有找到');
end;
--问题--
为何在用test1 是不会报内存地址错误,而在调test2是却报内存地址错误。
Showmessage(test('Text'));
showmessage(inttostr(testr(1,3)));
{ 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. }library SerReger; uses
ShareMem, //这个uses在你使用了这个.dll的所有项目的所有文件(包括.dpr)的uses第一句出现, 以及.dll工程所有文件中uses第一句出现
SysUtils,
Classes,
UCom in 'UCom.pas';function Test2(tx:string):string;stdcall;export;
begin
Result:=tx+'b';
end;{$R *.res}exports
Test2;begin
end.//下面是调用, 注意在这个调用.dll函数的文件的uses第一句使用ShareMe, 及这个工程的.dpr文件开头uses第一句(保证正确退出)function TfrmSysControl.test(Key:string):string;
var
ReStrx:function(Key:string):string;stdcall; //stdcall非常重要
DllHandle:hmodule;
DllName:pchar;
begin
DllName :=pchar(strPath+'SerReger.dll');
DllHandle:=LoadLibrary(dllName);
if DllHandle >0 then
try
@ReStrx:=GetProcAddress(DllHandle,'Test2');
if @ReStrx <>nil then
Result := ReStrx(Key);
finally
FreeLibrary(DllHandle);
end
else
ShowMessage('dll没有找到');
end;