C ++ loading external library files

Source: Internet
Author: User

First Introduction: What is the difference between using # import to import DLL and # pragma comment to import Lib? What is the difference between loadlibrary to load DLL in the program?

(1) # The DLL imported by import is a DLL established by com. It is mainly used to parse the internal structure of COM and identify calls with C ++,

(2), # pragma comment is a static load Library

(3) loadlibrary dynamic loading of dynamic libraries

Windows provides two methods to map a DLL to a process address space:

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.

1.
Link during implicit Loading

This method requires the Lib file compiled by the DLL project, which 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 DLL
The file path is sequential as follows:

· 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.

③ Program code

Add the pre-compiled command # pragma comment (Lib, "*. lib"). This method can be used to link lib files of different versions. Because, in debug mode, the generated lib file is 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.

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;

// 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 (glibmydll! = NULL)

{

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

Return;

}

// Load sample. dll. If no path is added, search for (1) Windows System directory in the three default paths:/Windows/system;

// (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 ");

 

//************************************** **************************************** **************************************** **************************************** **********************

//************************************** **************************************** **************************************** **************************************** **********************

Applications can use DLL in two ways: implicit link and explicit link. Before using the DLL, you must first know the structure information of the function in the DLL. Visual c ++ 6.0 stores a small program named dumpbin.exe in the VC/bindirectory. You can use it to view the function structure in the DLL file. In addition, the Windows system will follow the search order below to locate the DLL: 1. directory that contains the EXE file, 2. current working directory of the Process, 3. windows Directory, 4. windows Directory, 5. A series of directories listed in the PATH environment variable.

1. implicit link

The implicit link loads the DLL file into the application when the program starts execution. It is easy to implement implicit links. You only need to write the name of the imported function keyword _ declspec (dllimport) to the corresponding header file of the application. The following example uses an implicit link to call the min function in the mydll. dll library. First, generate a project named testdll and enter the following code in the dlltest. h and dlltest. cpp files:

// Dlltest. h
# Pragma comment (Lib, "mydll. lib ")
Extern "C" _ declspec (dllimport) int max (int A, int B );
Extern "C" _ declspec (dllimport) int min (int A, int B );
// Testdll. cpp
# Include
# Include "dlltest. H"
Void main ()
{Int;
A = min (8, 10)
Printf ("compare result % d/N", );
}

Before creating the dlltest.exe file, copy mydll. dll and mydll. lib to the directory where the current project is located, or copy it to the Windows System directory. If the DLL uses the def file, delete the keyword extern "c" in the testdll. h file ". Testdll. the keyword progam commit in the hfile is linked to mydll when the compiler of Visual C + is linked. lib file, of course, developers can also not use # pragma comment (Lib, "mydll. lib "), and enter mydll directly in the object/moduls column of the Setting-> link page of the project. lib is supported.

2. explicit link

Explicit links allow applications to load DLL files or uninstall DLL files at any time during execution, which is not implemented by implicit links. Therefore, explicit links provide better flexibility, it is more suitable for explanatory languages. However, implementing explicit links requires a little effort. In the application, call the dynamic link library provided by loadlibrary or the afxloadlibrary provided by MFC explicitly. The file name of the Dynamic Link Library is the parameter of the above two functions, and then use getprocaddress () obtain the function to be introduced. Since then, you can call the introduced function like using a function defined in the application. Before exiting the application, use the afxfreelibrary provided by freelibrary or MFC to release the dynamic link library. The following is an example of calling the max function in DLL through an explicit link.

# Include
# Include
Void main (void)
{
Typedef int (* Pmax) (int A, int B );
Typedef int (* pmin) (int A, int B );
Hinstance hdll;
Pmax Max
Hdll = loadlibrary ("mydll. dll"); // load the dynamic link library mydll. dll file;
Max = (Pmax) getprocaddress (hdll, "Max ");
A = max (5, 8 );
Printf ("compare result % d/N", );
Freelibrary (hdll); // uninstall the mydll. dll file;
}

In the above example, use the type definition keyword typedef to define the pointer to the same function prototype as the DLL, and then use loadlibray () load the DLL to the current application and return the handle of the current DLL file. Then, use the getprocaddress () function to obtain the function pointer imported to the application. After the function is called, use freelibrary () to uninstall the DLL file. Before compiling a program, copy the DLL file to the project directory or the Windows System directory.

When you use an explicit link to compile an application, you do not need to use the corresponding lib file. In addition, when using the getprocaddress () function, you can use the makeintresource () function to directly use the sequence number in the DLL function, for example, change getprocaddress (hdll, "min") to getprocaddress (hdll, makeintresource (2) (the sequential number of the function min () in the DLL is 2). In this way, the call speed of the function in the DLL is very fast, but remember the sequence number used by the function. Otherwise, an error occurs.

Use ad. h and AD. Lib for programming and put them in the current project directory.

Add # include "ad. H" to the header file"

In project setting --> link --> Object/library modules
Add ad. Lib

Put ad. dll in the same directory as your program during execution

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.