In programming, you often use a DLL file that is encapsulated by someone else, usually with two methods called
I. implicit load-time linking
Xxx.dll files, xxx.lib files, and xxx.h files are required, where xxx.dll files are placed under the EXE file output folder (Debug folder), Xxx.lib and xxx.h files are placed under the project folder.
(1) First add the Xxx.h file to the project (this file has the function declaration in the DLL file, the format as
extern " C "__ declspec ( dllimport " function returns the name of the type function ( parameter list )
), and then in the add # include "Xxx.h" to the header file .
(2) to add Lib to the project, there are several methods:
① LIB file directly into the project file list
Open the File View page in the VC, select the project name, right-click, and then select the Add Files to Project menu, and in the Pop-up dialog box, select the Xxx.lib file you want to add.
② Setup Project settings to load the DLL's lib file
Open the Project Project Settings menu, select Link, and then fill in the xxx.lib file in the text box under Object/library modules.
③ How to pass program code
Pre-compiled directives can be added to the xxx.h file #pragma comment (lib, "Xxx.lib")
Two. explicit run-time links
Only need to Xxx.dll file, put Xxx.dll file in exe file output folder (Debug folder).
You need to add the following code
1. typedef function return value type ( * function Aliases) ( parameter list );//And consistent with the functions defined in the DLL file
2. HInstance hDLL;
3. function Aliases The name of the function; //define a function name for a function alias type
4. hDLL=LoadLibrary("Xxx.dll");//Load Dynamic link library Xxx.dll file
5. Name of function=(function Aliases)GetProcAddress(hDLL,"The corresponding function name in the DLL file");
6. Function name (parameter);//Call function
Three. Common error messages
Debug Error:
Program : ...
Module:
file:i386\chkesp.c
line:42
Workaround:
definition letter Number of pointers (two. explicit run-time links in code first line ) function return value type ( callback * function alias ) ( Parameter list )
WHERE (CALLBACK * can also be _stdcall,winapi). Remove the callback. (This problem has troubled me for two days, but still do not know why, after removing the WINAPI, the program is normal, I hope the Great God can guide)
MFC bit by bit--call DLL and the problem will occur