Basic concepts of VC ++ dynamic link library Programming

Source: Internet
Author: User

  1. Overview 

First, let's explain the concept of DLL (Dynamic linkable library). You can simply look at dll as a repository, which provides you with some variables, functions, or classes that can be used directly. In the history of warehouse development, we have experienced the era of "no database-static Link Library-dynamic link library. Static and dynamic libraries share code, commands in lib are directly included in the final generated EXE file. However, if a DLL is used, the DLL does not need to be included in the final EXE file. During execution of the EXE file, the DLL file can be dynamically referenced and detached. Another difference between a static link library and a dynamic link library is that the static Link Library cannot contain any other dynamic or static Link Library, other dynamic or static link libraries can be included in the dynamic link library.

For dynamic link libraries, we also need to establish the following concepts:

(1) DLL compilation has nothing to do with the specific programming language and Compiler

As long as the agreed DLL interface specifications and calling methods are followed, the DLL files written in various languages can call each other. For example, the system DLL provided by Windows (including windows APIs) can be called in any development environment, regardless of whether it is Visual Basic, Visual C ++ or Delphi.

(2) Dynamic Link Libraries are everywhere

In the System32 folder under the Windows directory, we can see kernel32.dll, user32.dll, and gdi32.dll. Most windows APIs are included in these DLL. Functions in kernel32.dll mainly process memory management and process scheduling. Functions in user32.dll mainly control the user interface. Functions in gdi32.dll are responsible for graphic operations.

Generally, programmers have used functions similar to MessageBox. In fact, they are included in the dynamic link library user32.dll. It can be seen that DLL is not a stranger to us.

(3) classification of VC Dynamic Link Library

Visual c ++ supports three types of DLL, including non-mfc dll (non-MFC dynamic library), MFC regular DLL (MFC rule DLL), and MFC extension DLL (MFC extension DLL ).

The non-MFC dynamic library does not adopt the structure of the MFC class library. Its export function is a standard C interface and can be called by applications written by non-MFC or MFC; the MFC rule dll contains a class inherited from cwinapp, but it does not have a message loop. The MFC extension DLL is created using the dynamic link version of MFC, it can only be called by applications written in the MFC class library.

Because this article is long and has many content, it is necessary to explain the relevant items in this article first. The following is a question and answer section.

Q: What is the main content of this article?

A: This article describes in detail all aspects of DLL programming. After learning this article, we should be able to have a comprehensive understanding of DLL and be able to write most DLL programs.

Q: How do I read this article?

A: The Source Code routine is included in each topic of this article, which can be downloaded with the document (each project is compressed by WinRAR ). All these routines are compiled by the author and debugged in VC ++ 6.0.

Of course, understanding this article is not the ultimate goal of the reader. The reader should practice it in person to truly grasp the secrets of DLL.

Q: What basic knowledge is required to learn this article?

A: If you have mastered C, C ++, and MFC, you can easily understand this article.

 2. Static Link Library

The explanation of the static link library is not the focus of this article, but before explaining the DLL, the example of a static link library can help us quickly build the concept of "library.



Figure 1 create a static Link Library


1. In VC ++ 6.0, create a new static library project named libtest (Click here to download this project) and create a new Lib. H and Lib. CPP files: Lib. H and Lib. the source code of CPP is as follows:


// File: Lib. h

# Ifndef lib_h
# Define lib_h
Extern "C" int add (int x, int y); // declared as an external function in C compilation and Connection Mode
# Endif

// File: Lib. cpp

# Include "Lib. H"
Int add (int x, int y)
{
Return X + Y;
}


Compile the project to obtain a. Lib file, which is a function library and provides the Add function. After the header file and the. Lib file are submitted to the user, the user can directly use the Add function.

The C library functions (scanf, printf, memcpy, strcpy) in the standard Turbo c2.0 come from this static library.

Next let's take a look at how to use this library and create a new libcall project in the workspace where the libtest project is located. The libcall project contains only one main. cpp file, which demonstrates the call method of the static Link Library. Its source code is as follows:


# Include <stdio. h>
# Include ".. \ Lib. H"
# Pragma comment (Lib, ".. \ debug \ libtest. lib") // specify the link to the static library

Int main (INT argc, char * argv [])
{
Printf ("2 + 3 = % d", add (2, 3 ));
}


The call to the static Link Library is so simple that we may be using it every day, but we do not understand this concept. In the code, # pragma comment (Lib, ".. \ debug \ libtest. lib") means that the. OBJ file generated in this file should be connected with libtest. Lib. If you do not need to specify # pragma comment, you can directly set it in VC ++. 2. Select the tools, options, directories, library files menu, or options in sequence, and enter the path of the file to be imported. The red circle in Figure 2 shows the path of the libtest. Lib file we added.



Figure 2 set the library file path in VC


The example of this static link library should at least let us understand what the library functions are and where they come from. We now have the following fuzzy understandings:

(1) The Library is not a monster. The programming of the library is not much different from the general programming, but the library cannot be executed separately;

(2) The Library provides some things that can be called by other programs. To call other programs, it must be indicated in some way that it is to be called.

The above ignorance of the database obtained from the analysis of the static link library can be directly extended to the dynamic link library, the differences between the dynamic and static link libraries in writing and calling are reflected in slightly different definitions of external interfaces and calling methods of the library.

  3. debug and view the database

Before entering the detailed description of various DLL types, it is necessary to introduce the debugging and viewing methods of library files, because we will face a large number of example projects from the next section.

Because the library files cannot be executed separately, when you press F5 (start debug mode execution) or Ctrl + F5 (run) for execution, the dialog box shown in 3 is displayed, the user is required to enter the path of the executable file to start the execution of the library function. At this time, we can debug the library by entering the path of the EXE file to call the library. The debugging skills are the same as that of common application projects.


Figure 3 Database debugging and running"


There is usually a better way to debug than the above practice, that is, place the library project and application project (the project that calls the Library) in the same VC work zone, and only debug the application project, set a breakpoint at the statement of the function in the application project call library, and press F11 after execution, so that the function in the library is entered in a single step. The libtest and libcall projects in section 2nd are placed in the same workspace, as shown in project structure 4.



Figure 4 put the project of the library project and the project of the calling library into the same workspace for debugging


The above debugging method is the same for static and dynamic link libraries. Therefore, all the source code provided for download in this article contains the Library Project and the call library project, both of which are included in a work zone, which is the intention of the author to provide such package download.

The export interface in the dynamic link library can be viewed using the depends tool of Visual C ++. Let's use depends to open user32.dll in the system directory, right? There are several versions of MessageBox in the red circle! It actually exists here. It turns out to be here!



Figure 5 view DLL with depends


Of course, the depends tool can also display the DLL hierarchy. If you use it to open an executable file, you can see which DLL is called by this executable file.

Well, Let's enter the world of Dynamic Link Libraries. Let's take a look at the most common DLL, that is, non-mfc dll.

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.