Compiling Delphi DLL

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.