The compilation of Delphi DLL by shaoyun 2010-03-18 recently intends to continue learning Delphi, which is rarely used. Now it has not moved for a long time! By the way, write down the learning content and forget it! Sample Code:
Code
1 library sylib;
2 uses
3 sysutils, classes;
4 {$ R *. Res}
5 function max (X: integer; Y: integer): integer; stdcall;
6 begin
7 if x> Y then
8 result: = x
9 else
10 result: = y;
11 end;
12
13 exports
14 Max;
15
16 begin
17 end.
Dephi is the fastcall call method, and C/C ++ is the stdcall call. To allow the DLL to call these programs, it is best to declare it as the stdcall type. The exports export function cannot be called without exporting!
Two call methods are static and dynamic: static call, and the most common method is to declare function max (X: integer; Y: integer): integer; stdcall after var; external 'sylib. the DLL is called as follows: Procedure tform1.btn1click (Sender: tobject );
Begin
Showmessage (inttostr (max (2, 3 )));
End;
Dynamic call, more trouble:
1 procedure tform2.btn1click (Sender: tobject );
2 type
3 tfuntype = function (X: integer; Y: integer): integer; stdcall;
4 VaR
5 dllhandle: thandle;
6 pfun: tfarproc;
7 _ max: tfuntype;
8 begin
9 dllhandle: = loadlibrary ('sylib. dll ');
10 if dllhandle> 0 then
11 try
12 pfun: = getprocaddress (dllhandle, pansichar ('max '));
13 if pfun <> nil then
14 begin
15 _ max: = tfuntype (pfun );
16 showmessage (inttostr (_ max (2, 3 )));
17 end
18 else
19 showmessage ('Max function not found! ');
20 finally
21 freelibrary (dllhandle );
22 end
23 else
24 showmessage ('sylib. dll not found! ');
25 end;
That's all for now! Delphi2010/Windows SP3 test!