1.怎么样调用dll函数
procedure TBMFather.DOReport(sReportType:TBMReport; sOID:string = '');
type
TPuFun = function (TempLateURL: pchar;CusCallBack: pointer): integer; stdcall;
//声明回调函数
var
f:TfrmWebReport;
sURL:string;
a: array of integer;
ctype: string;//'h','hf','f'
Th: THandle;
Tf: TPuFun;
begin
//调用dll函数--------------------------------------------------
Th := LoadLibrary(PChar('BM_HASPX.dll'));
if Th > 0 then
try
try
@tf := GetProcAddress(Th, PChar('ASPX_DOReport')); //暴露函数
if Assigned(@tf) then
begin
if ctype='h' then
//下面的@Cus_SplitHTML为回调函数地址,将此地址传到dll中,由dll中的函数调用回调函数
Tf(pchar(surl), @Cus_SplitHTML) //@Cus_SplitHTML:@+函数名为函数地址
else if ctype='f' then
Tf(pchar(surl), @Cus_SplitHTMLF)
else if ctype='hf' then
Tf(pchar(surl), @Cus_SplitHTMLHF)
else //Tf(param1, param2)
Tf(pchar(surl), nil);
end;
finally
FreeLibrary(Th);
end
except
end;
end
2.dll暴露函数
function ASPX_DOReport(TempLateURL: pchar;CusCallBack: pointer): integer; stdcall;
begin
end;
3.dll中的函数:根据调用程序的函数指针(即@Cus_SplitHTML),调用函数
procedure TfrmWebReport.btn1Click(Sender:TObject);
//调用回调函数,首先需要先声明
type
TPuFun_Cus = procedure(const SourcHTML: pchar; var NewHTML1: pchar;var AttachList:array of
pchar);stdcall;
var
TmpTPuFUN : TPuFun_Cus;
begin
try
if wordback<>nil then //回调指针 @Cus_SplitHTML
begin
TmpTPuFUN := TPuFun_Cus(wordback); //根据函数指针就可以了
TmpTPuFUN(pchar(webstr), newhtml2, attractlist); //参数为本地参数
end
end;
4.提供函数指针的函数,申明如下
procedure Cus_SplitHTMLHF(const SourcHTML: pchar;var NewHTML: pchar;
var AttachList: array of pchar);stdcall;
//这个函数即是我们需要回调的函数体
以上的过程相当于一个循环:主程序调用DLL函数,DLL函数再次调用主程序的回调函数
主程序中定义的回调函数要注意位置,由于dll需要取得回调函数的地址,如果回调函数是声明在类
中的话,取地址会比较麻烦,所以这里的回调函数体的实现是在类以外的。