Windows provides two ways to image a DLL to the process address space (implicit and Explicit)

Source: Internet
Author: User
Tags function prototype

Calling a DLL, you first need to image the DLL file into the address space of the user process before you can make a function call, which is the same as calling methods of the general function inside the process. Windows provides two ways to image a DLL to the process address space:

1. Implicit load-time linking

This method requires the DLL project's compiled lib file, which contains a list of all the functions that the DLL allows the application to call, and when the linker discovers that the application called a function listed by the Lib file, it adds some information to the file image of the application's executable file. This information indicates the name of the DLL file that contains the function. When the application runs, that is, its executable file is generated by the operating system image file, the system will look at the information about the DLL in this image file, and then image the DLL file to the process's address space.

When the system tries to load this file into the process address space by the name of the DLL file, it looks for the path of the DLL file in the following order:

• The directory where the files are located when the program is running;

• Current Program Working directory

• System directory: For WINDOWS95/98, you can call the GetSystemDirectory function to get, for windowsnt/2000, refers to the 32-bit Windows system directory, You can also call the GetSystemDirectory function to get the value of SYSTEM32.

· Windows directory

• All directories listed in the PATH environment variable

There are three ways to load a DLL's lib file in a VC:

①lib files are added directly to the project file list

Open the File View page in the VC, select the project name, right-click, and select the "Add Files to Project" menu, in the Pop-up Files dialog box, select the Lib file to join the DLL.

② Setup project settings to load the DLL's lib file

Open the Project Project Settings menu, select Link, and then enter the Lib file for the DLL in the text box under Object/library modules.

③ How to Pass program code

The advantage of adding precompiled Directives #pragma comment (lib, "*.lib") is that the conditional precompiled directives can be used to link different versions of the Lib file. Because, in the debug mode, the resulting LIB file is the debug version, such as Regd.lib; in release mode, the resulting LIB file is the release version, such as Regr.lib.

When an application loads the DLL's lib file, it also needs to include the DLL's corresponding header file (*.h) into which the function prototype defined in the DLL is given, and then declared.

2 Explicit run-time links (I'm using this method)

Implicit linking is simpler to implement, but requires DLL's *.h and *.lib files in addition to the required *.dll files, which are not available in the case where only the *.dll files are provided, but only in the form of an explicit link. This way, by invoking API functions to load and unload DLLs, they can use memory more efficiently, which is often used when writing large applications. This method of programming is implemented in the following steps:

① uses the Windows API functions load Library or MFC provides afxloadlibrary to image DLL modules into the memory space of the process and dynamically load DLL modules.

② uses the GetProcAddress function to get a pointer to the function in the DLL to invoke.

③ explicitly unload the DLL from the address space of the process using the Free library function or the AfxFreeLibrary function when the DLL is not used.

Example: calling a DLL file in an application

--The functions in the exported table are not called until the DLL is loaded in the application, such as with MFC

Create a dialog-based project test and place the Load button on the dialog box to add the load code first.

1. First add the variable setting code at the Testdlg.cpp header:

Set global variable glibsample for storing DLL handles

HINSTANCE Glibsample=null; If the handle type is defined, an error occurs

The second variable, SHOWME, is a pointer to a DLL
Pointer to the SHOWME () function in the library

typedef INT (* SHOWME) (void);


Showme Showme;


2. Add code to load the DLL using ClassWizard for the "Load" button

void Ctestdlg::onloadbutton ()

{

The code to add is as follows

if (glibsample!=null)

{

AfxMessageBox ("The Sample.dll has already been load.");

Return

}

Mount Sample.dll, without path, will look for (1) The system directory of Windows in three default paths:/windows/system;

(2) Any directory indicated by Path in DOS;

(3) The directory where the program is located;


Glibsample=loadlibrary ("Sample.dll");

Returns the address of the SHOWME () function in the DLL

Showme= (SHOWME) GetProcAddress (glibsample, "showme");

http://blog.csdn.net/jiangxinyu/article/details/4736577

Windows provides two ways to image a DLL to the process address space (implicit and Explicit)

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.