Differences between DLL and Lib files

Source: Internet
Author: User
Tags microsoft c
Document directory
  • 1. Create a non-mfc dll dynamic link library using Win32 dynamic-Link Library
  • 2. MFC Appwizard [DLL] method to generate common/extended DLL
What are DLL and Lib files?

Dynamic link library (DLL) is a program module that can be shared by other applications. It encapsulates some routines and resources that can be shared. The file extension of the dynamic link library is generally DLL, or DRV, sys, and Fon. It is very similar to the executable file (exe, the difference is that although the dll contains executable code, it cannot be executed independently, but should be called directly or indirectly by a Windows application.

Dynamic Links are relative to static links. The so-called static link refers to linking the function or process to be called to the executable file to become part of the executable file. In other words, the function and process code is in the program's exe file, which contains all the code required for running. When multiple programs call the same function, multiple copies of the function exist in the memory, which wastes valuable memory resources. The function code called by the dynamic link is not copied to the executable file of the application, instead, it only adds the description information of the called function (usually some relocation information ). Only when the application is loaded into the memory and starts to run, the connection between the application and the corresponding DLL is established under Windows Management. When you want to execute the function in the called DLL, the corresponding function code in the DLL is transferred to Windows based on the relocation information generated by the link.

Generally, if an application uses a dynamic link library, the Win32 system ensures that only one copy of the DLL is in the memory, which is implemented through the memory ing file. The DLL is first transferred to the global stack of the Win32 system, and then mapped to the process address space that calls the DLL. In the Win32 system, each process has its own 32-bit linear address space. If a DLL is called by multiple processes, each process will receive an image of the DLL. Unlike a 16-bit windows, a DLL in Win32 can be considered as the code of each process.

Differences and links between DLL and Lib files

. DLL is a file that is connected only when your program runs. Therefore, it is a relatively small executable file format ,. DLL has other file formats such. OCX, etc. All. all DLL files are executable.

. Lib is the file that is connected when your program compiles the connection. Therefore, you must inform the compiler of where the Lib file is connected. In general, compared with dynamic connection files, lib files are also called static connection libraries. When you compile the code into these files, they will not be changed later. To use the Lib file, you must:

1. Include a corresponding header file to inform the compiler of the specific content in the Lib file.

2. Set the Lib file to allow the compiler to find the compiled binary code.

If you want to separate a DLL file from your code to replace the static Connection Library, you still need a lib file. This lib file will be connected to a program to tell the operating system what DLL file you want to use during running. Generally, the LIB file contains the corresponding DLL file name and a sequence table that specifies the DLL output function entry. If you do not want to use the Lib file or do not have the Lib file, you can use the Win32 API functions loadlibrary and getprocaddress. This is why vs gave a lib file after compiling the DLL?

 

(1) Lib is required during compilation and DLL is required during runtime.

If you need to complete source code compilation, it is enough to have Lib.

If dynamic connection is also enabled, DLL is enough.

In the development and debugging stages, it is best to have both.

(2) common dynamic library programs include lib files and DLL files. The LIB file must be connected to the application during the compilation period, and the DLL file will be called only during the runtime. If there is a DLL file, the corresponding lib file is generally some index information, the specific implementation is in the DLL file. If only the Lib file is available, the Lib file is statically compiled and the indexes and implementations are all included. The advantage of static compiling lib files is that dynamic libraries are no longer needed during installation. However, there are also some disadvantages, that is, the application is relatively large and the flexibility of the dynamic library is lost. During version upgrade, new applications must be released at the same time.

(3) In the case of a dynamic library, there are two files, one is imported into the database (. lib) file. One is a DLL file. The imported file contains the name and location of the function exported by the DLL. The dll contains the actual function and data, the application uses the Lib file to link to the required DLL file. The functions and data in the library are not copied to the executable file. Therefore, in the executable file of the application, stores not the called function code, but the memory address of the function to be called in the DLL, in this way, when one or more applications are running, the program code is linked to the called function code, thus saving memory resources.

 

Http://www.programfan.com/article/showarticle.asp? Id = 2923

I. Preface

 

Since Microsoft launched a 16-bit Windows operating system, every version of Windows operating system is dependent on functions and data in the dynamic link library (DLL, in fact, almost all content in Windows is represented by DLL in one or another form, for example, the displayed fonts and icons are stored in the gdi dll, the Code required to display Windows desktops and process user input is stored in a user DLL, and a large number of APIs are required for Windows programming. the function is also included in the kernel DLL.

 

Using DLL in Windows has many advantages. The most important thing is that multiple applications, or even applications written in different languages, can share a DLL file, resources are truly shared, which greatly reduces the code execution of applications and makes more effective use of memory. Another advantage of using DLL files as a separate program module is that, encapsulation and independence are good. When the software needs to be upgraded, the developer only needs to modify the corresponding DLL file. Moreover, when the function in the DLL changes, as long as it is not a parameter change, the program code does not need to be re-compiled. This is very useful in programming and greatly improves the efficiency of software development and maintenance.

 

Since DLL is so important, it is a problem that programmers have to solve to find out what is DLL and how to develop and use it in windows. To address these problems, this article uses a simple example, that is, to implement the maximum and minimum integers in a DLL, it comprehensively parses the process of programming and implementing DLL in the Visual C ++ compiling environment. The program code used in this article is passed in the Windows98 system and Visual C ++ 6.0 compiling environment.

 

Ii. dll Concept

 

DLL is a library file that contains a number of functions, classes, or resources. functions and data are stored in a DLL (server) and exported by one or more customers. These customers can be applications or other DLL. The dll library is different from the static library. In the case of a static library, functions and data are compiled into a binary file (usually with the extension *. lib ), when processing program code, the visual C ++ compiler restores these functions and data from the static library and combines them with other modules in the application to generate executable files. This process is called "static link". Because all the content required by the application is copied from the library, the static library itself does not need to be released together with the executable file.

 

In the case of a dynamic library, there are two files, one is imported into the database (. lib) file. One is a DLL file. The imported file contains the name and location of the function exported by the DLL. The dll contains the actual function and data, the application uses the Lib file to link to the required DLL file. The functions and data in the library are not copied to the executable file. Therefore, in the executable file of the application, stores not the called function code, but the memory address of the function to be called in the DLL, in this way, when one or more applications are running, the program code is linked to the called function code, thus saving memory resources. From the above description, we can see that the DLL and. Lib files must be released along with the application, otherwise the application will produce errors.

 

Microsoft's Visual C ++ supports three types of DLL: Non-mfc dll (non-MFC dynamic library), regular DLL (conventional DLL), and extensiondll (Extended DLL ). Non-mfc dll refers to a DLL written in C language without the use of the MFC class library structure. The exported function is a standard C interface, it can be called by applications not compiled by MFC or MFC. Regular DLL: similar to the following extension DLLs, it is written using the MFC class library. One obvious feature of this DLL is that there is a class in the source file that inherits cwinapp (note: although such DLL is derived from cwinapp, it does not
Message loop ), the exported function is a C function, C ++ class, or C ++ member function (do not confuse the C ++ class with the Microsoft C ++ class of MFC ), applications that call conventional DLL do not need to be MFC applications, as long as they can call C-like functions, they can be used to develop applications in Visual C ++, dephi, VisualBasic, Borland C, and other compiling environments.

 

Conventional dll can be subdivided into static link to MFC and dynamic link to MFC. The differences between these two types of conventional DLL are described below. Compared with conventional DLL, extended DLL is used to export functions or subclasses that enhance the basic class of MFC. This type of dynamic link library can be used to output a class inherited from MFC.

 

The extension DLL is created using the dynamic link version of MFC, and is called only by applications written in the MFC class library. For example, you have created a derived class from the ctoolbar class of MFC to create a new toolbar. to export this class, you must put it in an extended DLL of MFC. Extension DLL is different from conventional DLL. It does not have a class Object inherited from cwinapp. Therefore, developers must add initialization code and end code to the dllmain function in the DLL.

 

3. Create a dynamic link library

 

In the Visual C ++ 6.0 development environment, open the filenewproject option, you can select Win32 dynamic-link library or mfcappwizard [DLL] to create dynamic link libraries of different types such as non-mfc dll, regular DLL, and extension DLL in different ways.

 

1. Create a non-mfc dll dynamic link library using Win32 dynamic-Link Library

 

Each dll must have an entry point, which is the same as an application written in C. It must have a winmain function. In non-mfc dll, dllmain is a default entry function. You do not need to write your own DLL entry function, with this default entry function, the dynamic link library can be correctly initialized when called. If the application DLL needs to allocate additional memory or resources, or you need to initialize and clear each process or thread, you need. in the CPP file, the dllmain () function is written in the following format.

 

Boolapientry dllmain (handle hmodule, DWORD ul_reason_for_call, lpvoid lpreserved)

{

Switch (ul_reason_for_call)

{

Casedll_process_attach:

.......

Casedll_thread_attach:

.......

Casedll_thread_detach:

.......

Casedll_process_detach:

.......

}

Returntrue;

}

 

In the parameter, hmoudle is a handle that points to itself when the dynamic library is called (in fact, it is a selector pointing to the _ dgroup segment ); ul_reason_for_call indicates the reason why a dynamic library is called. When a process or thread loads or detaches a Dynamic Linked Library, the operating system calls the entry function, it also describes the reason why the dynamic link library is called. All its possible values are dll_process_attach: process called, dll_thread_attach: thread called, dll_process_detach: process stopped, dll_thread_detach: the thread is stopped; lpreserved is a reserved parameter. So far, the DLL entry function has been written, and the remaining implementation is not difficult. You can add the function or variable you want to output in the DLL project.

 

We already know that the DLL is a library file containing several functions. before using the functions in the DLL, the application should first export these functions for application use. There are two methods to export these functions. One is to use the export keyword _ declspec (dllexport) when defining the function, and the other is to use the module definition file when creating the DLL file. def. Note that the def file cannot be used when the first method is used. The following two examples illustrate how to use these two methods to create a DLL file.

 

1) Use the export function keyword _ declspec (dllexport) to create mydll. dll. There are two functions in the dynamic link library, which are used to obtain the maximum and minimum numbers of two numbers respectively. In the mydll. h and mydll. cpp files, enter the following original code:

 

 

// Mydll. h

Extern "C" _ declspec (dllexport) int max (int A, int B );

Extern "C" _ declspec (dllexport) int min (int A, int B );

// Mydll. cpp

# I nclude

# I nclude "mydll. H"

Intmax (int A, int B)

{

If (A> = B) returna;

Else

Returnb;

}

Intmin (int A, int B)

{

If (A> = B) returnb;

Else

Returna;

}

 

After the dynamic link library is compiled successfully, open the DEBUG directory in the mydll project and you can see the mydll. dll and mydll. Lib files. The LIB file contains the DLL file name and the function name in the DLL file. This lib file is only for the "Image File" of the DLL file, and in the DLL file, the length of the Lib file is much smaller. It is used for implicit link to the DLL file. Readers may have noticed the keyword "extern C" in mydll. H, which allows other programming languages to access the functions in your compiled DLL.

 

2) create a project mydll using the. Def File

 

To use. def file to create DLL. Delete mydll in the project created in the previous example first. h file, keep mydll. CPP and delete it in the file header # I nclude mydll. H Statement, and add a text file to the project, named mydll. def, then add the following code to the file:

 

Librarymydll

Exports

Max

Min

 

The library statement indicates that the def file belongs to the corresponding DLL. The exports statement lists the name of the function to be exported. We can add @ n after the export function in the. Def file, for example, Max @ 1, Min @ 2, to indicate the sequence number of the function to be exported. It can be used for explicit connection. After the DLL is compiled, open the DEBUG directory in the project, and you will also see the mydll. dll and mydll. Lib files.

 

2. MFC Appwizard [DLL] method to generate common/extended DLL

 

There are three methods to generate a DLL file in the MFC Appwizard [DLL]. When creating a DLL, select the method to create a DLL according to the actual situation. One is static link of conventional DLL to MFC, and the other is dynamic link of conventional DLL to MFC. The difference between the two is that the former uses the static Link Library of MFC, And the generated DLL file is long. This method is generally not used. The latter uses the dynamic link library of MFC, the length of the generated DLL file is small. All the functions output by the DLL that dynamically link to the MFC rule should start with the following statement:

 

 

Afx_manage_state (afxgetstaticmodulestate () // This statement is used to correctly switch the status of the MFC module.

 

The last one is the MFC extension DLL, which is used to establish the derived class of MFC. The DLL is called only by the application program compiled by the MFC class library. We have already introduced that extension DLLs is different from regular DLLs. It does not have a class Object inherited from cwinapp. The Compiler defaults to a DLL entry function dllmain () as the initialization of DLL, You can implement initialization in this function. The Code is as follows:

 

 

Boolwinapi apientry dllmain (hinstance hinstdll, DWORD reason, lpvoidflmpload)

{

Switch (reason)

{

............... // Initialization code;

}

Returntrue;

}

 

The hinstdll parameter stores the DLL handle. The reason parameter specifies the reason for calling the function. lpreserved is a parameter reserved by the system. The implicit link is a non-zero value, and the explicit link value is zero.

 

When a DLL file is created under MFC, the def file framework will be automatically generated. Other files are no different from the traditional non-mfc dll, as long as the keyword _ declspec (dllexport) is written into the corresponding header file) function Type and function name, or enter the function name under exports in the generated def file. Note that when distributing the MFC extension DLL to other developers, do not forget to provide the header file describing the class in the DLL and the corresponding. the LIB file and DLL itself, and developers will be able to make full use of your developed extended DLL.

 

4. Dynamic Link Library DLL Link

 

Applications can use DLL in two ways: implicit link and explicit link. Before using the DLL, you must first know the structure information of the function in the DLL. Visual c ++ 6.0 stores a small program named dumpbin.exe in the VC indirectory, which can be used to view the function structure in the DLL file. In addition, the Windows system will follow the search order below to locate the DLL: 1. directory that contains the EXE file, 2. current working directory of the Process, 3. windows Directory, 4. windows Directory, 5. A series of directories listed in the PATH environment variable.

 

1. implicit link

 

The implicit link loads the DLL file into the application when the program starts execution. It is easy to implement implicit links. You only need to write the name of the imported function keyword _ declspec (dllimport) to the corresponding header file of the application. The following example uses an implicit link to call the min function in the mydll. dll library. First, generate a project named testdll and enter the following code in the dlltest. h and dlltest. cpp files:

 

// Dlltest. h

# Pragmacomment (Lib, "mydll. lib ")

Extern "C" _ declspec (dllimport) int max (int A, int B );

Extern "C" _ declspec (dllimport) int min (int A, int B );

// Testdll. cpp

# I nclude

# I nclude "dlltest. H"

Voidmain ()

{INTA;

A = min (8, 10)

Printf ("compare result % d", );

}

 

 

Before creating the dlltest.exe file, copy mydll. dll and mydll. lib to the directory where the current project is located, or copy it to the Windows System directory. If the DLL uses the def file, delete the keyword extern "c" in the testdll. h file ". Testdll. the keyword progam commit in the hfile is linked to mydll when the compiler of Visual C + is linked. lib file, of course, developers can also not use # pragma comment (Lib, "mydll. lib "), and directly fill in the object/moduls column on the setting-> link page of the project
Mydll. Lib is supported.

 

2. explicit link

 

Explicit links allow applications to load DLL files at any time during execution, or unload DLL files at any time. This is not implemented by implicit links, therefore, explicit links have better flexibility and are more suitable for explanatory languages. However, implementing explicit links requires a little effort. In the application, call the dynamic link library provided by loadlibrary or the afxloadlibrary provided by MFC explicitly. The file name of the Dynamic Link Library is the parameter of the above two functions, and then use getprocaddress () obtain the function to be introduced. Since then, you can call the introduced function like using a function defined in the application. Before exiting the application, use the afxfreelibrary provided by freelibrary or MFC to release the dynamic link library. The following is an example of calling the max function in DLL through an explicit link.

 

 

# I nclude

# I nclude

Voidmain (void)

{

Typedefint (* Pmax) (int A, int B );

Typedefint (* pmin) (int A, int B );

Hinstancehdll;

Pmaxmax

Hdll = loadlibrary ("mydll. dll"); // load the dynamic link library mydll. dll file;

Max = (Pmax) getprocaddress (hdll, "Max ");

A = max (5, 8 );

Printf ("compare result % d", );

Freelibrary (hdll); // uninstall the mydll. dll file;

}

 

In the above example, use the type definition keyword typedef to define the pointer to the same function prototype as the DLL, and then use loadlibray () load the DLL to the current application and return the handle of the current DLL file. Then, use the getprocaddress () function to obtain the function pointer imported to the application. After the function is called, use freelibrary () to uninstall the DLL file. Before compiling a program, copy the DLL file to the project directory or the Windows System directory.

 

When you use an explicit link to compile an application, you do not need to use the corresponding lib file. In addition, when using the getprocaddress () function, you can use the makeintresource () function to directly use the sequence number in the DLL function, for example, change getprocaddress (hdll, "min") to getprocaddress (hdll, makeintresource (2) (the sequential number of the function min () in the DLL is 2). In this way, the call speed of the function in the DLL is very fast, but remember the sequence number used by the function. Otherwise, an error occurs.

 

This article introduces the concept of Dynamic Link Library, the creation of dynamic link library, and the link of dynamic link library in a simple and clear way, I believe that after reading this article, you can create your own dynamic link library and apply it to subsequent software development. Of course, you must be proficient in DLL operations, we also need to constantly explore in a large number of practices. I hope this article can serve as a reference.

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.