DLL reference under VC-very practical

Source: Internet
Author: User
To call a DLL, you first need to map the DLL file to the address space of the user process before calling the function. This function is the same as the calling method of common functions in the process. Windows provides two methods to map a DLL to a process address space:

1. Implicit loading Link

This method requires the Lib file compiled by the DLL project, which contains the DLL file that allows applications.ProgramThe list of all called functions. When the linker finds that the application has called a function listed in the Lib file, it adds some information to the file image of the executable file of the application, the information indicates the name of the DLL file containing the function. When the application is running,
That is, when the operating system generates an image file for its executable file, the system will view the DLL information in the image file and then map the DLL file to the address space of the process.

The name of the DLL file,
When trying to load the file to the process address space, it looks for the DLL file path in the following order:

· The directory where the execution file is located;

· Current program working directory

· System directory: For Windows95/98, you can call the getsystemdirectory function. For WindowsNT/2000, it refers to the 32-bit Windows System directory, you can also call the getsystemdirectory function to obtain the value system32.

· Windows Directory

· All directories listed in the PATH environment variable

There are three methods to load DLL lib files in VC:

① Add the Lib file directly to the project file list

Open the file view page in VC, select the project name, right-click the project, and select the "add files to project" menu, in the pop-up file dialog box, select the Lib file to be added to the DLL.

② Set project settings of the project to load the DLL lib File

Open the Project Settings menu of the project, select link, and enter the DLL lib file in the text box under object/library modules.

③ ProgramCodeMethod

Add the pre-compiled command # pragma comment (Lib, "*. lib"). The advantage of this method is that the conditional pre-compiled command can be used to link lib files of different versions. In the debug mode, the generated lib file is the debug version, such as regd. Lib. In the release mode, the generated lib file is the release version, such as Regr. Lib.

After the application loads the Lib file of the DLL, it also needs to load the header file (*. h) include it. In this header file, the prototype of the function defined in DLL is provided, and then declared.

2. Explicit runtime Link

Although the implementation of implicit links is relatively simple, except for the required *. *. h file and *. lib files, which only provide *. DLL files cannot be used, but can only be explicitly linked. This method is used to load and detach a DLL by calling an API function, which can use the memory more effectively. This method is often used when writing large applications. The specific steps for programming this method are as follows:

① Use the Windows API function load library or the afxloadlibrary provided by MFC to mirror the DLL module to the memory space of the process and dynamically load the DLL module.

② Use the getprocaddress function to obtain the pointer to the function in the DLL to be called.

③ When no DLL is used, use the free library function or the afxfreelibrary function to explicitly uninstall the DLL from the address space of the process.

 

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.