Description of the C language DLL file and how to generate and use it

Source: Internet
Author: User

Recently, in some small projects, the dynamic link library becomes the perfect choice because it involves interacting with other languages. To this end also consulted a lot of information, the dynamic link library related knowledge to do a collation.

  I. Dynamic link library Overview

Dynamic Link Library is a non-executable binary program file that allows multiple programs to share the code and other resources necessary to perform special tasks. In Windows, DLLs are in most cases files with a ". dll" extension, but may also be ". ocx" or other extensions; Linux systems often have ". So" files. Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. The executable code for a function is in a DLL file that contains one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLLs also help to share data and resources. Multiple applications can access the contents of a single copy of a DLL in memory at the same time. Using a dynamic-link library makes it easier to apply updates to individual modules without affecting other parts of the program. is an essential part of developing a large project.

  Ii. Advantages and Disadvantages

  Advantages : (1) Save memory and code reuse: When multiple programs use the same library, DLLs can reduce the amount of duplication of code loaded in disk and physical memory and help reuse code.

(2) Modularity: DLLs help facilitate modular program development. Modularity allows you to simply change the code and data in one DLL used by several applications without changing the application itself. It is suitable for large-scale software development, makes the development process independent, the coupling degree is small, facilitates the development and the test between different developers and the development organization. This basic form of modularity allows applications such as Microsoft Office, Microsoft Visual Studio, and even windows itself to use more compact patches and service packs.

(3) Extend the characteristics of the application, the use of DLL files can make the application can be easily extended function, many of the program's plug-in mechanism is through the DLL file implementation.

(4) can be compiled and called in many languages, because each language has its own unique development advantages, in dealing with a certain kind of transaction has a unique advantage, so in the process of multi-language programming, can use DLL files as a bridge, can play the advantages of many languages.

  cons : DLL Hell: DLL Hell, a version conflict that occurs when several applications use the same shared DLL library.

The reason, eight words: Into also shared, defeated also shared. Because DLL hell is the result of a dynamic link library that can share functions and resources with other programs.

There are two main types of situations :

Imagine a scenario where program a uses the 1.0 version of dynamic link library x, and the 1.0 version of dynamic link library x is installed when program a installs to the system. Assuming another program B also uses the dynamic link library x, then program B is copied directly to the hard disk to run normally, because the dynamic link library already exists in the system. One day, however, another program C also uses dynamic link library x, but because program C is developed later, it needs a newer version---The 2.0 version of dynamic link Library x. When program C is installed on the system, the 2.0 version of dynamic link library x must also be installed on the system, and the 1.0 version of the dynamic link library in the system will be replaced by the 2.0 version (replace).

Scenario 1: The new version of the dynamic link library is incompatible with the old version. For example, a, B needs x to provide the function, after upgrading to 2.0, the new version of X unexpectedly put this function canceled (it's hard to imagine, hehe, but sometimes it is ...). )。 At this point, although C works correctly, neither a nor B can work.

Scenario 2: The new version of the dynamic link library is compatible with the old version, but there is a bug.

  Third, the entry point

Just like the main function of the application, the DLL file has an entry function called DllMain (), and its prototype is this:

1 BOOL apientry DllMain (2HANDLE hmodule,//handle to DLL module3DWORD Ul_reason_for_call,//reason for calling this function4LPVOID lpreserved//reserved5 ) {6     Switch(Ul_reason_for_call)7     {8          CaseDll_process_attach:9             //The process is loading this DLLTen          Break; One          CaseDll_thread_attach: A             //a thread is created -          Break; -          CaseDll_thread_detach: the             //a thread exits normally -          Break; -          CaseDll_process_detach: -             //The process is unloading this DLL +          Break; -     } +     returnTRUE;//returns True, indicating successful execution of this function A}

The entry point function should only perform simple initialization tasks and should not call any other DLL load functions or terminate functions. For example, in an entry point function, you should not call the LoadLibrary function or the LoadLibraryEx function directly or indirectly. In addition, the FreeLibrary function should not be called when the process terminates.

  Iv. Generating DLL files

The following generates a DLL file, defining only one simple function for the sake of convenience.

The build DLL file requires two files, a header file, a dll_add.h, and a source file, DLL_ADD.C

Header file Contents:

1 #ifndef _dll_demo_h_2 #define_dll_demo_h_3 #ifdef dlldemo_exports4 #defineDll_demo _declspec (dllexport)5 #else6 #defineDll_demo _declspec (dllimport)7 #endif8 extern "C"Dll_demointADD (intAintb);9 #endif

Source file Contents:

1 " dll_demo.h " 2 3 int ADD (intint  b)4{5     return (A + b); 6 }

Because there is no need for special handling of function loading and unloading, you can not use the entry function.

Using the vs2015, in debug mode or Release mode after debugging will be in the corresponding directory generated DLL files, can be used.

  V. Calling DLL files

There are two ways to generate a DLL that is naturally called to call a DLL.

  Static invocation: Using the. H+.lib+.dll

  

1#include <windows.h>2#include <iostream>3#include"dll_demo.h"4 using namespacestd;5 #pragmaComment (lib, "Dll_demo.lib")6 7 extern "C"_declspec (dllimport)intADD (intAintb);8 intMainintargcChar*argv[])9 {Tencout << Add (2,3) <<Endl; OneSystem"Pause"); A     return 0; -}

Both the head and Lib files and DLL files can be used in the same directory as the source files. Of course, the path can be reset.

  Dynamic invocation: Use only DLL files

1#include <windows.h>2#include <iostream>3 using namespacestd;4typedefint(*addfunc) (intAintb);5 intMainintargcChar*argv[])6 {7hmodule hdll = LoadLibrary (L"Dll_demo.dll");8       if(hDLL! =NULL)9       {TenAddfunc add = (addfunc) GetProcAddress (hDLL,"ADD"); One             if(Add! =NULL) A             { -Cout<<add (2,3) <<Endl; -             } the FreeLibrary (hdll); -       } -}

Add an L function to the string: The Unicode character set is two bytes. L NOTICE the compiler uses a two-byte Unicode character set.

You can also use DLLs to share classes and variables, and you can also implement memory sharing because there is not much research, so this is not covered here.

  

Description of the C language DLL file and how to generate and use it

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.