DLL of Dynamic Link Library

Source: Internet
Author: User


Preface

I recently found that I have made many technical notes on my computer, but these summaries are limited to my computer and I have not shared them with others (although they are also uploaded to Baidu Library ), in fact, this is not good for it. It should be more than shared by others. It not only helps you discover some shortcomings and defects in your understanding of some knowledge, but also helps improve your communication skills, and sometimes you will find unexpected gains!

In fact, I opened my blog very early and wrote some blogs before, but I haven't written a blog for a long time. So I want to use my blog to sort out my previous notes, I would like to share with you some valuable comments and suggestions regarding errors or deficiencies in your study and notes.

Today is July August 31, tomorrow is July September 1, and new students are about to start school. The new semester is coming again, and 2014 is over 2/3. That's the first Technology blog, welcome to a better September! For a better 2014! Come on!


The first technical blog was also hesitant. I was planning to sort out the notes in the head first design mode, however, most of the notes in that book are summative. If you haven't read the book, you can't understand many of the notes for the moment, so you chose C ++ notes, let's start with a more practical dynamic link library!


Introduction to dynamic link library

[2] p701

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/E3/wKioL1QCpBrhcC-TAAVoLrkazHA482.jpg "style =" float: none; "Title =" image1.png "alt =" wKioL1QCpBrhcC-TAAVoLrkazHA482.jpg "/>


Static library and dynamic library

Note:

The. Lib library is not a static library, but an imported file. This file is required when compiling links to form executable files.

The. Lib library is the real static library.

[2] p702

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/E1/wKiom1QCowPSa21DAAOW_KJSXbY953.jpg "style =" float: none; "Title =" image2.png "alt =" wkiom1qcowpsa21daaow_kjsxby953.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E1/wKiom1QCowPDIG76AAO0J_GlGOk095.jpg "style =" float: none; "Title =" image3.png "alt =" wkiom1qcowpdig76aao0j_glgok095.jpg "/>

When compiling a link, the DLL will be loaded only when the file. Lib is imported into the database.

The import file. Lib does not contain the actual code, but only provides the necessary information for the linked program to facilitate the relocation table used when creating dynamic links in the executable file.

Benefits of Using Dynamic Link Libraries

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/E3/wKioL1QCpBrBJnGAAAb8Fkd0ziA416.jpg "style =" float: none; "Title =" image4.png "alt =" wkiol1qcpbrbjngaaab8fkd0zia416.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/E1/wKiom1QCowTih0OnAAep1O9IULQ806.jpg "style =" float: none; "Title =" image5.png "alt =" wkiom1qcowtih0onaaep1o9iulq806.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E3/wKioL1QCpBuzRLOgAAJ4ugsweZA857.jpg "style =" float: none; "Title =" image6.png "alt =" wkiol1qcpbuzrlogaaj4ugsweza857.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/E1/wKiom1QCowTTSkRLAAI_svcWDc4073.jpg "style =" float: none; "Title =" image7.png "alt =" wkiom1qcowttskrlaai_svcwdc4073.jpg "/>



OneDLLThere is only one instance in the memory

The relationship between DLL and process and thread

The DLL module is mapped to the virtual address space of the process that calls it.

The dll Memory is allocated from the virtual address space of the calling process and can only be accessed by the thread of the process.

The DLL handle can be used by the calling process, and the call Process Handle can be used by the DLL.

Dlldll can have its own data segment but does not have its own stack. It uses the stack of the calling process, which is the same as the stack mode of the application that calls it.

2) about shared data segments

The global variables defined by dll can be accessed by the calling process; The dll can access the global data of the calling process. Each process using the same dll has its own DLL global variable instance. If multiple threads concurrently access the same variable, you need to use the synchronization mechanism. For a DLL variable, If you want each thread that uses the DLL to have its own value, the local thread storage should be used (TLS, Thread Local strorage ).

 

Dll library search path

C/C ++/C #This order is used to search for dynamic databases.

[2] p710


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/E3/wKioL1QCpBzRgFfZAASwk82B3ZQ659.jpg "style =" float: none; "Title =" image8.png "alt =" wkiol1qcpbzrgffzaaswk82b3zq659.jpg "/>



When a dll library has a dependent library, you need to load the dependent library.

As we can see above, when the memory of a program exceeds 4 GB, the program will crash. Why did the HKUST project crash after 4 GB of Memory leakage.

If a DLL is found in a directory, the following search path will not be found.

 

There are two loading methods:

1. explicit link

2. implicit link

This part of content will be described in detail in later blogs.

Dllmain Function

[2] p729

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E1/wKiom1QCowXSW6QnAAKdeL8-_FQ964.jpg "style =" float: none; "Title =" image9.png "alt =" wKiom1QCowXSW6QnAAKdeL8-_FQ964.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E3/wKioL1QCpB2zObYjAAUdvWmleWQ039.jpg "style =" float: none; "Title =" image10.png "alt =" wkiol1qcpb2zobyjaaudvwmlewq039.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/E3/wKioL1QCpB7SQnmcAALurGmHAqQ343.jpg "style =" float: none; "Title =" image11.png "alt =" wkiol1qcpb7sqnmcaalurgmhaqq343.jpg "/>


Export and Import Functions

DLLFiles are divided into export and import Functions

To access a function in a DLL, the function must be an exported function.

You can use PE explorer to view

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/E1/wKiom1QCowawYGtiAAFzAsDBcGo722.jpg "style =" float: none; "Title =" image12.png "alt =" wkiom1qcowawygtiaafzasdbcgo722.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E3/wKioL1QCpB6R3oTkAANhyxruiho467.jpg "style =" float: none; "Title =" image13.png "alt =" wkiol1qcpb6r3otkaanhyxruiho467.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/E1/wKiom1QCowfi2XEuAAFNKt4wUgA833.jpg "style =" float: none; "Title =" image14.png "alt =" wkiom1qcowfi2xeuaaftnk4wuga833.jpg "/>

About C ++ compiler function name Processing

For details, see the call conventions. The details of the call conventions will also be discussed in detail in the blog.

View the functions exported from C ++ and DLL

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/E3/wKioL1QCpB7Ds8LhAACvYfAjKSQ647.jpg "style =" float: none; "Title =" image15.png "alt =" wkiol1qcpb7ds8lhaacvyfajksq647.jpg "/>

[2] p707

We can see that the function name is strange,That's because,C ++Supports heavy loadFor multiple overloaded functions, the function names are the same. to differentiate them, C ++ modifies the function name according to its own rules during link compilation, this process is called"Name adaptation"(That is, the question of modifying the name of a function in the call conventions.), Some books are also called "name crushing ".DifferentC ++The compiler uses different rules for name adaptation,In this case, different C ++ compiler programs may encounter problems when calling the functions provided by the other party. At this time, we need to use the following extern "C" method.



Note:

1. In the blog: [1] P22 stands for references 1 22nd pages, [2] p69 stands for references 2 69th pages

This annotation format is used in all technical blogs.

2. The reason for listing references is to make it easy for you to view the source. If you have any questions, you can go to the document to view them. It is also convenient for you to review later.

3. You cannot directly paste images from word to a blog. images are uploaded to a blog one by one, which is very troublesome. If you know how to copy images from word to a blog friend, please reply. Thank you very much!



References

1. c ++ primer (Special Edition 4th)

2. in-depth explanation of VC ++ sun Xin







This article from "QQ" blog, please be sure to keep this source http://qianqing13579.blog.51cto.com/5255432/1547075

DLL of Dynamic Link Library

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.