Create and call a dynamic link library in Delphi and VC

Source: Internet
Author: User

In software development, we often encounter this situation. In different development environments, we need to implement the same function process. Because the development uses different design languages, therefore, you have to write a lot of similar code, and sometimes rewrite the code in different projects in the same development environment, which leads to a great waste of resources. The dynamic link library can better solve this problem, and reuse code will greatly improve the development efficiency. Generally, you can create and use a DLL in an application based on the following requirements:

1. Programs shared between different executable files;

2. When designing an application, it is split into independent functional components, which will provide a convenient way to upgrade these functional components in the future.

The dynamic link library makes the shared program or functional parts into functions in the library to form DLL files. Other applications call these functions by Using DLL. Here we will introduce how to create and use dynamic link libraries in Delphi and VC. Dynamic Link Libraries defined by Delphi and VC can be used in both development environments.

1. Use Delphi to create a DLL

The creation of DLL in Delphi is not complex. Next we will introduce the method for creating DLL in Delphi.

(1) Create a New DLL Project (newproject) project1. the file header is:

Library project1;

(2) Add the exports statement after the uses statement to specify the name of the function that calls the DLL, in the form:

Exports
Checkpwd name 'checkpwd ';

(3) Add the DLL function or process declaration after type... end in the DLL's pas file, in the form:

Functionname (argment): Boolean; export;

  
This function can also exist in another pas file (such as unit1.pas). In this case, you only need to add the following to the project file:

Uses
Unit1 in 'unit1. pa' {form1 };

It is worth noting that this function or process should be added to the create and free (generate and release) Methods of the form.

(4) compile the project to form a DLL file, which can be called by other projects.

Ii. Call of DLL in Delphi

You can call a DLL either when the application is loaded or when the application is running.

(1) Call DLL during loading

In the PAS file that calls the DLL, make an external Declaration of the DLL function. The declaration should be placed after implementation, in the following form:

Implementation
Function functionname (argment): Boolean; far; External 'dllname ';

  
The quotation marks behind the external keyword are the DLL file name. This file should generally be placed under the system directory or the same directory as the project that calls it. After the declaration, You can reference the DLL function anywhere in the PAS file.

The advantage of calling DLL during loading is that it is faster and code can be shared between programs.

(2) Call DLL during runtime

Another way to call DLL is to call it at runtime. This method calls the Windows API functions loadlibrary, getprocaddress, and freelibrary. This method is mainly used to call other languages, especially C ++ compiled DLL.

Assume that the DLL you want to call contains a function:

Function checkpwd (PWD: string): Boolean; export;

  
Add the following statement to the type declaration of the program to call the DLL:

Type
Tcheckpwd = function (PWD: string): Boolean;

  
This sentence serves as a function pointer declared in C ++.

Then define the following variables:

VaR
Aptr: tfarproc;
Lhnd: thandle;
Flag: Boolean;

The aptr and lhnd variables must be declared, and the flag is the DLL function return value, depending on the situation. Add the following statement to call the DLL for DLL loading:

Lhnd: = loadlibrary ('path: DLL filename '); {for example, lhnd: = loadlibrary ('C:/project1.dll ');
Aptr: = getprocaddress (lhnd, 'checkpwd ');

  
You can call the DLL directly below:

Flag: = tcheckpwd (aptr) ('pwd'); {fill in the corresponding variable parameters according to the function}

  
After the call, use freelibrary to release the memory occupied by the DLL:

Freelibrary (lhnd );

Ii. Call of DLL in Delphi

You can call a DLL either when the application is loaded or when the application is running.

(1) Call DLL during loading

In the PAS file that calls the DLL, make an external Declaration of the DLL function. The declaration should be placed after implementation, in the following form:

Implementation
Function functionname (argment): Boolean; far; External 'dllname ';

  
The quotation marks behind the external keyword are the DLL file name. This file should generally be placed under the system directory or the same directory as the project that calls it. After the declaration, You can reference the DLL function anywhere in the PAS file.

The advantage of calling DLL during loading is that it is faster and code can be shared between programs.

(2) Call DLL during runtime

Another way to call DLL is to call it at runtime. This method calls the Windows API functions loadlibrary, getprocaddress, and freelibrary. This method is mainly used to call other languages, especially C ++ compiled DLL.

Assume that the DLL you want to call contains a function:

Function checkpwd (PWD: string): Boolean; export;

  
Add the following statement to the type declaration of the program to call the DLL:

Type
Tcheckpwd = function (PWD: string): Boolean;

  
This sentence serves as a function pointer declared in C ++.

Then define the following variables:

VaR
Aptr: tfarproc;
Lhnd: thandle;
Flag: Boolean;

The aptr and lhnd variables must be declared, and the flag is the DLL function return value, depending on the situation. Add the following statement to call the DLL for DLL loading:

Lhnd: = loadlibrary ('path: DLL filename '); {for example, lhnd: = loadlibrary ('C:/project1.dll ');
Aptr: = getprocaddress (lhnd, 'checkpwd ');

  
You can call the DLL directly below:

Flag: = tcheckpwd (aptr) ('pwd'); {fill in the corresponding variable parameters according to the function}

  
After the call, use freelibrary to release the memory occupied by the DLL:

Freelibrary (lhnd );

3. Use VC to create a DLL

Create a DLL in VC, which is similar to the creation process in Delphi. The steps are as follows:

(1) create a DLL project. MFC provides two types of Dynamic Link Libraries: conventional DLL and extended DLL. Conventional DLL is suitable for applications developed in programming languages that support DLL calls in non-MFC development environments. Extended dll can only be used by MFC programs.

(2) to create a process or function in the DLL, you only need to add the following statements to the program:

Extern "C" _ declspec (dllexport)
Int yanzheng (cstring input, char * zcm)
{
Afx_manage_state (afxgetstaticmodulestate ());
File: // specific implementation process
}

This function is used to verify whether the registration code is valid. In this function, yanzheng is the function name, Int Is the function return value type, and () Is the function parameter. After compilation, you can use it.

Iv. dll call in VC

When calling the dynamic link library in VC, it is similar to calling the DLL method when running in Delphi. You must also call the Windows API functions such as loadlibrary and getprocaddress. This method is applicable to calling DLL compiled by other languages and VC.

Assume that the dll contains a function:

Int yanzheng (cstring input, char * zcm)

  
First, declare the function pointer before the CPP file to call the DLL function and define the function type:

Typedef int (* _ yanzheng) (cstring, char * zcm );

Then define the following variables:

Hinstance hlibrary;
_ Yanzheng;

Add the following statement to call the DLL for DLL loading:

Hlibrary = loadlibrary ("zcdll. dll ");
Yanzheng = (_ yanzheng) getprocaddress (hlibrary, "yanzheng ");
Shuchu = (* yanzheng) (shuru, zcm );
Freelibrary (hlibrary );

5. Tips

(1) If other forms are used to create a DLL using Delphi, the DLL output functions should include create (create) and free (release) of form ), in addition, you can only use the showmodal process when displaying the window, but not the show process. You can see it in detail in the following example.

(2) If the function or process in the DLL created by Delphi uses the string type as the parameter input or returns the string type, errors will often occur when the VC or other programs are called, and vice versa. To solve this problem, it is recommended that the pchar type be used instead of the string type when the string type is used between different programs. In addition, memory management should be added to the program and the sharemem unit provided by the system should be used. The following is an example of how to convert a week Id from a number to an English one:

Uses
Sharemem;
Exports
Makeitaday;
VaR
Mydate: pchar;
Function makeitaday (S: integer): pchar; stdcall; export;
Implementation
Function makeitaday (S: integer): pchar; stdcall; export;
VaR
Thedate: array [0 .. 20] of char;
Begin
Getmem (mydate, 200 );
If S = 1 then thedate: = 'sunday ';
File ://..........
Strpcopy (mydate, thedate );
Makeitaday: = mydate;
End;

Vi. Instances

The process of using Delphi to create and call a DLL is similar to that of using VC to create and call a DLL, but the code of the former is slightly more complex than that in the VC environment, therefore, a complete example of using Delphi to create and Call DLL is provided at the end of the article. This dll is mainly used to check whether the entered password is correct. The form contains an edit box, a button, and a password in the edit box. Compare the value of the edit box and the input parameter to return the true value.

// The file name is checkpassword. DPR; compile this file to generate checkpassword. dll
Library checkpassword;
Uses
Sysutils, classes, unit1 in 'unit1. pa' {form1 };
Exports
Checkpwd name 'checkpwd'; // declare the DLL Function
{$ R *. Res}
Begin
End.

File: // the file name is unit1.pas.
Unit unit1;
Interface
Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs, stdctrls;
Type
Tform1 = Class (tform)
Password: tedit;
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Flag: Boolean; // returns whether the entered password is correct
Rightpwd: string; // record the input parameters (correct password)
Function checkpwd (PWD: string): Boolean; export; // declare the DLL Function
Implementation
{$ R *. DFM}
Procedure tform1.button1click (Sender: tobject );
Begin
If password. Text = rightpwd then
Flag: = true;
Form1.close;
End;

Function checkpwd (PWD: string): Boolean; export;
Begin
Flag: = false;
Rightpwd: = PWD; // read the correct password value
Form1: = tform1.create (application); // create a password verification window
Form1.showmodal; // display window
Checkpwd: = flag;
Form1.free; // release resources
End;
End.

File: // The Main Program Main. Pas, which calls the DLL
Unit Main;
Interface
Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs, stdctrls;
Type
Tform1 = Class (tform)
Jieguo: tedit;
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
Function checkpwd (PWD: string): Boolean; External 'project1. dll '; // Function Description
{$ R *. DFM}
Procedure tform1.button1click (Sender: tobject );
Begin
If checkpwd ('congrong ') Then // call the Function
Jieguo. Text: = 'true'
Else
Jieguo. Text: = 'false ';
End;
End.

The above program is compiled in Windows98/delphi5.

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.