Windows dynamic link library DLLs

Source: Internet
Author: User

1. What is a DLL
DLL, the dynamic-link library, is a library file that contains several functions that can be called when other programs run.

Advantages and disadvantages of 2.DLL
Pros: Code reuse, which can be called by multiple programs at the same time

Cons: prone to version conflicts
Other programs that use this DLL may not work correctly when the new version of the dynamic link library is incompatible with the old version.
To address this problem, we need to add a manifest file to the program that indicates the version number of the DLL used by the program.
At this point, the program loads the specified version of the DLL according to manifest, otherwise it is loaded by the default version.

3. About Lib and DLLs
Lib, is a static link library, divided into static Lib and dynamic Lib.
static lib: contains function code declarations and implementations, and all code is embedded in the host program after compilation. no additional calls to the DLL are required.
dynamic lib: equivalent to A. h header file, which is a declaration of the export portion of the Implementation section (. DLL). The post-compilation host program also includes only the Declarations section. the runtime needs to invoke the appropriate DLL file .

4.DLL Writing
1) in VS, select "File"-"new"-"Project" and select "Visual C + +"-"Win32 Project" to create the DLL class library.
As shown in the following:

2) Add header file (. h) and source file (. cpp)
Mydll.h

extern " C " int Add (intint

extern keyword : functions can be used in this module or other modules;
"C": indicates that the function must be compiled in C, because C + + supports the overloading of functions, and the function name is changed at compile time (by function name + parameter type).
Therefore, "C" needs to be declared so that the corresponding function can be found correctly when linking.
_declspec (dllexport) keyword : This function can be exported from a DLL, when a program introduces a DLL, only the function that declares the keyword can be imported using

Mydll.cpp

" mydll.h " int Add (intint//     return a + b;}

After compiling, we can find the corresponding DLL file and lib file in the build directory.

MyDLL.DLL,mydll.lib

5.DLL Call
There are two ways to call a DLL.
1) explicit linking (using only DLLs)

Features: no need to use the corresponding LIB file, can load and unload the DLL at any time, convenient and flexible.

voidMainvoid) {typedefint(*padd) (intAintb);    HINSTANCE hDLL;    Padd add; hDLL=loadlibrary ("MyDll.dll");//load the dynamic link library MyDll.dll file;Add= (Padd) GetProcAddress (hDLL,"Add"); intResult=add (5,8); Printf ("The result of the calculation is%d\n", result); FreeLibrary (hdll);//uninstall MyDll.dll file;}

The previous example loads a DLL into the current application through Loadlibray () and returns a handle to the current DLL file.
The function pointers that are imported into the application are then obtained through the GetProcAddress () function.
After the function call is complete, unload the DLL file using FreeLibrary ().

2) implicit linking (using Lib,dll)

Feature: when the program starts executing, the DLL file is loaded into the application, and the DLL is released after the program finishes.

#pragma comment (lib, "MyDll.lib")extern"C"int Add (int A,int  b); void Main () {    int  A;    A=add (8,ten)    printf (" The result of the calculation is%d\n" , a);}

#pragma comment is a preprocessing directive that allows the linker to find the "MyDll.lib" library when it is linked to the library.

Windows dynamic-link library DLLs

Related Article

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.