Call the DLL file in VC

Source: Internet
Author: User

Dynamic Link Library DLL Link

ApplicationProgramDll can be used 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. Generate a project named testdll. In the dlltest. h and dlltest. cpp files, enter the followingCode:

// 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

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.