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)));
解决方案 »
- 中英文切换
- idHttp session 的问题
- DBGrid1DrawColumnCell事件处理问题。(DELHPI7)
- 想用TCOMM的windows api函数,要uses什么?
- DBGrid中数据输入后的类型检测问题
- 如何把系统的输入法定于自己选择的??高分求急。QQ:2059478 在线。。。
- 求 DevExpress.ExpressQuantumGrid.Suite 6.45及以上版本
- 请问同是为了重用,Component Template和Frame有什么本质上的相同点,不同点?
- 如何改变窗体Tab键的顺序
- 朋友遇到大难题了(关于考试特长加分),谁能给他个好的建议。。。
- 可用分小于20分的人接分之五,Delphi区五帖,五一后结贴
- 问下关于Delphi 编译的问题
{ 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;