Differences between lib and DLL in Windows

Source: Internet
Author: User

There are two types of databases:
One is that LIB contains information about the DLL file where the function is located and the Function Location (entry) in the file. The code is provided by the DLL loaded in the process space during runtime, it is called the dynamic link library.
One is that LIB contains the Function Code itself. during compilation, the code is directly added to the program, called the static Link Library of the static Link Library.
There are two Connection Methods:
The dynamic link uses the dynamic link library to permit the execution of the dig (.dll file or the .exe file) only contains the information required to locate the executable code of the DLL function at runtime.
Static links use static link libraries. The linker obtains all referenced functions from the static Link Library lib and puts the libraries and codes together in the executable files.

The differences between lib and DLL are as follows:
(1) Lib is used for compiling and DLL is used for running. To compile the source code, you only need Lib. To run the dynamic link program, you only need DLL.
(2) If a DLL file exists, Lib is generally an index that records the entry and position of the function in the DLL. The dll contains the specific content of the function. If only the Lib file exists, the LIB file is statically compiled, and the index and implementation are all in it. The static library does not need to be attached to the dynamic library when running the program. The disadvantage is that the application is large and the flexibility of the dynamic library is lost, only release new applications when releasing a new version.
(3) In the case of dynamic links, there are two files: one is the Lib file and the other is the DLL file. LIB contains the name and location of the function exported by the DLL. The dll contains the actual functions and data. The application uses the Lib file to link to the DLL file. In the executable file of an application, the address of the corresponding function code in the DLL is not stored in the called function code, thus saving memory resources. DLL and Lib files must be released along with the application; otherwise, errors may occur in the application. 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 for loading.

Note the following two files when using Lib:
(1) The. h header file contains the class or symbol prototype or data structure described in Lib. When an application calls Lib, the file must be included in the application's source file.
(2). Lib file, omitted.

When using DLL, pay attention to three files:
(1). h header file, which contains a. h file indicating the output class or symbol prototype or data structure in DLL. When an application calls a DLL, the file must be included in the application's source file.
(2 ). the LIB file is generated after the DLL is compiled and linked successfully. It is used to introduce the file into the application when other applications call the DLL. Otherwise, an error occurs. 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 for loading.
(32.16.dll file, which is a real executable file. Only the. exe file and. dll file are available when the application program behind the development is running, and the. Lib file and. h file are not required.

Use Lib:
In static Lib, a lib file is actually a collection of any OBJ files, which are compiled and generated by the CPP file. When compiling such a static library project, there will be no link error at all; even if there is a mistake, it will only be exposed in the ext file or DLL project using this Lib.
Create a static library project Lib in VC and add test. CPP file and test. h file (including the function declaration in the header file), and then compile it to generate Lib. lib file.
There are two ways to use this Lib for other projects:
(1) Add lib to project> link> Object/Library module. lib file (first query the project directory, then query the lib directory); or add the command # pragma comment (Lib, "Lib. lib ").
(2) Upload Lib. lib to the directory where the project is located, the directory generated by the execution file, or the system lib directory.
(3) Add the corresponding header file test. h.

DLL method:
The Lib in the dynamic link is not a collection of OBJ files, that is, there is no actual implementation in it, it only provides the information required to dynamically link to the DLL, this lib can be generated by the compiler when compiling a DLL project.
Method for creating a DLL Project (omitted ).
(1) implicit link
The first method is to add it to the project-> link-> Object/librarymodule. lib file (or add the command # pragma comment (Lib, "Lib. lib), and. place the DLL file in the directory where the project is located, and then add the corresponding. h header file.

# Include "stdafx. H"
# Include "dllsample. H"

# Pragma comment (Lib, "dllsample. lib") // you can set the Library link in the project properties.

Int main ()
{
Testdll (123); // The function in DLL, which is declared in dllsample. h.
Return (1 );
}

(2) explicit link
The function pointer and Win32 API functions loadlibrary and getprocaddress are required for loading. This loading method is not required. lib file and. h header file, only. DLL file. DLL files are placed in the project directory ).

# Include <iostream>
# Include <windows. h> // use functions and some special Variables
Typedef void (* dllfunc) (INT );
Int main ()
{
Dllfunc;
Hinstance hinstlibrary = loadlibrary ("dllsample. dll ");

If (hinstlibrary = NULL)
{
Freelibrary (hinstlibrary );
}
Dllfunc = (dllfunc) getprocaddress (hinstlibrary, "testdll ");
If (dllfunc = NULL)
{
Freelibrary (hinstlibrary );
}
Dllfunc (123 );
STD: cin. Get ();
Freelibrary (hinstlibrary );
Return (1 );
}

The loadlibrary function uses a name as a parameter to obtain the DLL instance (the hinstance type is the instance handle). Generally, after calling this function, you need to check whether the function returns success, if the call fails, null is returned (the handle is invalid). The freelibrary function is called to release the dll Memory.
The getprocaddress function uses the DLL handle and function name as the parameter to return the corresponding function pointer. At the same time, strong conversion is required. It determines whether the function pointer is null, if yes, call the freelibrary function to release the dll Memory. Then, you can use the function pointer to call the actual function.
Remember to use the freelibrary function to release the memory.

Note: How does the application find the DLL file?
When you use the loadlibrary explicit link, you can specify the full path of the DLL file in the function parameters. If you do not specify a path or perform an implicit link, windows will follow the search order below to locate the DLL:
(1) directory containing the EXE file
(2) project directory
(3) Windows System directory
(4) Windows Directory
(5) A series of directories listed in the PATH environment variable


From: http://www.cppblog.com/amazon/archive/2009/09/04/95318.html


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.