Use of dll in Delphi

Source: Internet
Author: User
Tags getcolor

Windows execution files can be divided into two types of programs and dynamic connection libraries (DLLs ). A. EXE file is used to run a program, but an application can sometimes call a function stored in a DLL.
When we call the API functions in Windows, we actually call the functions stored in the DLL.
It is reasonable to call the DLL in the following situations:
1) different programs use the same DLL, so you only need to load the DLL once in the memory, saving the memory overhead.
2) When some content needs to be upgraded, you only need to change the DLL to use it, without changing the entire program.
3) because the DLL is independent from the language, it is often used by people of different languages.
When developing a large project together, you can use DLL to facilitate the communication between program systems. Of course, the DLL developed by Delphi can also be used in systems such as Visual BASIC and C ++.
The following examples describe how to develop a dynamic connection library using Delphi.
Section 1 how to build and call a dynamic Connection Library
1. Build File --- New --- Other --- DLL Wizard through dynamic Connection Library
This creates a basic module for the dynamic Connection Library.
Library Project2; uses
SysUtils,
Classes;
{$ R *. res}
Begin
End.
Change the project name to Mydll and write the necessary functions.
Library mydll;
Uses
SysUtils, Classes, Dialogs, windows;
Function Triple (N: Integer): integer; stdcall;
Begin
Result: = N + 3;
End;
Function Double (N: Integer): integer; stdcall;
Begin
Result: = N + 2;
End;
Function Triple1 (N: Integer): integer; stdcall;
Begin
Showmessage ('calculate N + 3 ');
Result: = N + 3;
End;
Function Double1 (N: Integer): integer; stdcall;
Begin
Messagebox (0, 'calculate N + 2', 'calculate N + 2', mb_ OK );
Result: = N + 2;
End;
Exports
Triple name 'tr ',
Double name 'do ',
Triple1 name 'trm ',
Double1 name 'dom ';
Triple, Double, Triple1, Double1;
{$ R *. RES}
Begin
End.
Function: Triple: adds the input value to three
Double: adds two values to the input value.
Triple1: add the input value to three and display the prompt.
Double1: Add two input values and display the prompt.
In this example, we can see several DLL program rules:
1) In the DLL program, the output function must be declared as stdcall, replacing the optimized Register with the standard Win32 parameter passing technology.
(Note: in Delphi, the Register method is the default call Convention. This Convention uses registers as much as possible to transmit parameters. The transfer order is left to right and can be used to a maximum of three CPU registers, if there are more than three parameters, the rest will be transferred through the stack. using register transfer ensures that the parameter transmission speed is the fastest.
The stdcall method transmits parameters through standard calls in Windows, and the transmission order is from left to right. This method is suitable for calling Windows APIs. In DLL, of course, this method should be used ).
2) All output functions must be listed under the exports clause, so that the subroutine can be seen outside the DLL.

Exports
Triple name 'tr ',
Double name 'do ',
Triple1 name 'trm ',
Double1 name 'dom ';
Lists the names of interfaces that users use this function. Although the alias is not necessary, it is best to give it an alias so that the user program can easily find this function. It also points out that Delphi 6.0 has canceled the index allowed in Delphi 5.0, if you also use Index to specify the interface name, an error will be prompted in Delphi 6.0.
The instance provides two tips:
Showmessage ('') is a function provided by VCL. Because VCL is compiled multiple times, the program will be large.
Messagebox (0, '','', mb_ OK) is an API function provided by Windows, and the program is relatively small.
This means that when compiling a DLL program, try to avoid compiling VCL multiple times. As an instance, both methods are listed here.
Save
Compile: projrdbms --- Build Mydll
This completes the compilation of a simple dynamic Connection Library.
Ii. Dynamic Connection Library call
First, make a call declaration under implementation.
Const
Gdi32 = 'mydll. dll ';
Function triple (n: integer): integer; stdcall; external gdi32 name 'tr ';
Function Double (N: Integer): integer; stdcall; external gdi32 name 'do ';
Function triple1 (n: integer): integer; stdcall; external gdi32 name 'trm ';
Function Double1 (N: Integer): integer; stdcall; external gdi32 name 'dom ';
In the future, the program can be used as a common function, for example:
Procedure TForm1.Button1Click (Sender: TObject );
Var N: integer;
Begin
N: = updown1.position;
Edit1.text: = inttostr (triple (N ));
End;
Section 2. Delphi Form in DLL
1. How to place a window in DLL
In DLL, in addition to placing standard functions and procedures, you can also place the prepared delphi Form, or you can use the prepared form for other programs:
1) First, create a form using the normal method. However, in the interface Area, declare the interface function as follows:
Function Createform (capt: string): string; stdcall;
2) Add interface functions under implementation
Function Createform (capt: string): string; stdcall;
Var Form1: TForm1;
Begin
Form1: = Tform1.Create (application );
Form1.show;
Form1.caption: = capt;
End;
3) create a DLL dynamic connection library, but declare:
Uses
Unit1 in 'unit1. pa ';
Exports
{Writing interface identifier}
Createform name 'myform ';
4) The program that calls the form is created in the normal method, but the DLL function to be called is first declared in implementation.
Const
Gdi32 = 'myformdll. dll ';
Function Createform (capt: string): string; stdcall; external gdi32 name 'myform ';

Procedure TForm3.Button1Click (Sender: TObject );
Var n, m: string;
Begin
M: = 'my form ';
Createform (m); var n, m: string;
End;
Ii. Data transfer when calling a form in DLL

When calling a form, you can use common function methods to transmit data. The following is an example.
1) create a form
To change the color form and place it in DLL, you can use the normal method, but you must make the following statement:
Function mycolor (col: longint): longint; stdcall;
Function Getcolor: longint; stdcall;
Mycolor is the construction form, and Getcolor is the color data.
Declare a global variable in the form in the implementation area.
Var color1: longint;
Write the corresponding program below
Function mycolor (col: longint): longint; stdcall;
Var Form1: TForm1;
Begin
Form1: = Tform1.Create (application );
Form1.show;
Form1.panel1. Color: = col;
Form1.edit1. Text: = inttostr (form1.panel1. Color );
Result: = color1;
End;
Function Getcolor: longint; stdcall;
Begin
Result: = color1;
End;
Procedure TForm1.ScrollBar1Change (Sender: TObject );
Begin
Panel2.Color: = RGB (ScrollBar1.Position, ScrollBar2.Position, ScrollBar3.Position );
Edit2.Text: = inttostr (panel2.Color );
Color1: = panel2.Color;
End;
Procedure TForm1.Button2Click (Sender: TObject );
Begin
Free; // destructor Form1
End;
2) create a dynamic Connection Library
After running successfully, create a dynamic Connection Library:
Library FormDLL;
{Transfer from file}
Uses
Unit1 in 'unit1. pa ';
Exports
{Writing interface identifier}
Mycolor name 'my ',
Getcolor name 'get ';
Begin
End.
3) create a called Program
First declare the DLL function to be called
Const
Gdi32 = 'formdll. dll ';
Function Mycolor (col: longint): longint; stdcall; external gdi32 name 'my ';
Function Getcolor: longint; stdcall; external gdi32 name 'get ';
Then write the corresponding program
Procedure TForm1.Button1Click (Sender: TObject );
Begin
Mycolor (color );
End;
Procedure TForm1.Button2Click (Sender: TObject );
Begin
Color: = getcolor;
End;
We can see that the color of the current form changes after the color changes in the form.

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.