Function MyMax ( X , Y : integer ) : integer ; stdcall ;
begin
if X > Y then
Result := X
else
Result := Y ;
end ;
//切记:Library 的名字大小写没关系,可是DLL-Func的大小写就有关系了。
// 在 DLL-Func-Name写成MyMax与myMAX是不同的。如果写错了,立即
// 的结果是你叫用到此DLL的AP根本开不起来。
//参数的大小写就没关系了。甚至不必同名。如原型中是 (X,Y:integer)但引
// 用时写成(A,B:integer),那是没关系的。
//切记:要再加个stdcall。书上讲,如果你是用Delphi写DLL,且希望不仅给
// Delphi-AP也希望BCB/VC-AP等使用的话,那你最好加个Stdcall ; 的指示
//参数型态:Delphi有很多种它自己的变量型态,这些当然不是DLL所喜欢的
// ,Windows/DLL的母语应该是C。所以如果要传进传出DLL的参数,我们
// 尽可能照规矩来用。这两者写起来,后者会麻烦不少。如果你对C不熟
// 的话,那也没关系。我们以后再讲。
procedure TForm1.Button1Click(Sender: TObject);
type
TIntFunc=function(i:integer):integer;stdcall;
var
Th:Thandle;
Tf:TIntFunc;
Tp:TFarProc;
begin
Th:=LoadLibrary(’Cpp.dll’); {装载DLL}
if Th>0 then
try
Tp:=GetProcAddress(Th,PChar(’TestC’));
if Tp<>nil
then begin
Tf:=TIntFunc(Tp);
Edit1.Text:=IntToStr(Tf(1)); {调用TestC函数}
end
else
ShowMessage(’TestC函数没有找到’);
finally
FreeLibrary(Th); {释放DLL}
end
else
ShowMessage(’Cpp.dll没有找到’);
end;