[Switch] How does VC Call DLL files?

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. this method requires the library file compiled by the DLL project to be linked during implicit loading. This file contains a list of all functions that the dll allows the application to call, when the linker finds that the application calls 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, its executable file is generated by the operating system, the system will view the DLL information in the image file, then, the DLL file is mapped to the address space of the process. When the system tries to load the file to the process address space through the DLL file name, it looks for the path of the DLL file in the following order: · The directory when the program is running, directory where the executable file is located; · Current program working directory · system directory: For Windows95/98, you can call the getsystemdirectory function to obtain the file. 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 · There are three methods to load DLL lib files in all directories listed in PATH environment variables: ① 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. ③ Add the pre-compiled command # pragma comment (Lib, "*. lib") to the program code. This method has the advantage 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 (I use this method). Although the implementation of implicit link is relatively simple, it is not only necessary *. *. 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 implementation steps of 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. For example, call the DLL file in an application.

-- In an application, you must load the DLL before calling the function in the export table. For example

Create a project test based on the dialog box, place the "LOAD" button in the dialog box, and add the load code first. 1. First add the variable setting code in the header of testdlg. cpp:

// Set the global variable glibsample to store the DLL handle.

Hinstance glibsample = NULL; // if it is defined as the handle type, an error occurs.

// The second variable showme is directed to the DLL
Pointer of the showme () function in the library

Typedef int (* showme) (void );
Showme;
2. Use classwizard to add the DLL Loading Code for the "LOAD" button.

Void ctestdlg: onloadbutton ()

{

// The code to be added is as follows:

If (glibsample! = NULL)

{

Afxmessagebox ("the sample. dll has already been load .");

Return;

}

// Load sample. dll. If no path is added, search for the Windows System directory \ WINDOWS \ system in the three default paths;

// (2) any directory indicated by path in DOS;

// (3) Directory of the program;


Glibsample = loadlibrary ("sample. dll ");

// Return the address of the showme () function in the DLL.

Showme = (showme) getprocaddress (glibsample, "showme ");

[Switch] How does VC Call DLL files?

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.