1. Definitions of non-mfc dll, mfc dll, and MFC extension DLL
Non-mfcdll does not use the MFC class library structure. Its export function is a standard C interface and can be called by non-MFC or MFC programs. The DLL of the MFC rule contains classes inherited from cwinapp, however, there is no message loop. The MFC extension DLL is created using the dynamic connection version of MFC, which can only be used by the program written by the MFC class library.
2. dll header file template
The DLL header file is used in the DLL definition and the DLL calling program. However, when defining the DLL, the header file must be declared and exported. In the called program, the header file must be declared as an import function. At the same time, it should be noted that the export function of the Standard C compiler type and the export function of the C ++ type compiler have compatibility issues, so the corresponding declaration should also be made in the header file. The above common problems can all be unified into a head file using macro-defined methods, so the following header file template is available. As follows:
# Ifndef _ nonemfcdll_h # DEFINE _ nonemfcdll_h // If the C compiler file is used, use extern "c" to declare # ifdef _ cplusplusextern "C" {# endif // macro-defined export function, DLL definition Program # ifdef nonemfcdll_exports # define nonemfcdll_api _ declspec (dllexport) # else // macro definition import function, call Program # define nonemfcdll_api _ declspec (dllimport) # endif // description of the exported functions, variables, and classes added here: nonemfcdll_api int add (const int A, const int B); nonemfcdll_api void show (); // If the C compiler file is used, use extern "c" to declare # ifdef _ cplusplus} # endif
3. Example
When creating a project, select the Win32 application and select DLL to create a non-mfcdll project. Vs will automatically generate some items. However, we need to make some modifications, the above header file template is mainly used in the past. The example header file is nonemfcdll. h:
// The following ifdef block is a standard method for creating a simpler export from DLL // macro. All files in this DLL are compiled with the nonemfcdll_exports // symbol defined on the command line. This symbol should not be defined on // any other project that uses this DLL. In this way, any other project that contains this file in the source file will regard the // nonemfcdll_api function as being imported from the DLL, the dll will use the // symbol defined by this macro as exported. # Ifndef _ nonemfcdll_h # DEFINE _ nonemfcdll_h // If the C compiler file is used, use extern "c" to declare # ifdef _ cplusplusextern "C" {# endif # ifdef define # define DEFINE _ declspec (dllexport) # else # define nonemfcdll_api _ declspec (dllimport) # endif // description of the exported functions, variables, and classes added here: nonemfcdll_api int add (const int A, const int B); nonemfcdll_api void show (); # ifdef _ cplusplus} # endif // The following content is automatically generated by Vs, which can be deleted or ignored. // Automatic Generation // This class is from nonemfcdll. DLL Export Class nonemfcdll_api cnonemfcdll {public: cnonemfcdll (void); // todo: add your method here .}; Extern nonemfcdll_api int nnonemfcdll; nonemfcdll_api int fnnonemfcdll (void); // automatically generated # endif
In the DLL header file, declare the ADD and show functions of the export function (for DLL, it is best to declare the export function in a header file). Next, you need to declare it in nonemfcdll. define the export function in the CPP file. As follows:
// Nonemfcdll. cpp: defines the export function of the DLL application. /// # Include "stdafx. H" # include "nonemfcdll. H" // This is an example of the export variable nonemfcdll_api int nnonemfcdll = 0; // This is an example of the export function. Nonemfcdll_api int fnnonemfcdll (void) {return 42 ;}// this is the constructor of the exported class. // For information about class definition, see nonemfcdll. hcnonemfcdll: cnonemfcdll () {return;} // defines the export function. You only need to declare it as an export function when declaring it, int add (const int A, const int B) {return a + B;} void show () {MessageBox (null, text ("show function from nonemfcdll_api! "), Text (" nonemfcdll_api message "), mb_ OK | mb_iconinformation );}
It should be noted that for the definition export function, you can not declare this function as the export function, just follow the normal function definition. After such a non-mfcdll project is completed, you only need to compile it to generate the DLL file.
4. Call DLL (Call DLL implicitly)
Implicit DLL call adds the Lib file generated by the dynamic link library to the project of the application, when using this function, you only need to declare the export function (reference the template header file), so it is relatively simple, but note that after the DLL is re-compiled, the application needs to be re-compiled.
Note the following points for implicit calls:
A) include the header file, that is, the declaration of the export function.
B) Use # pragma comment (Lib, "DLL. lib") to include Lib in the project.
C) copy the corresponding DLL file to the directory where the application is located.
Then, use the export function as a normal function. The CPP file in the example is as follows:
// Loadnonmfcdll. cpp: defines the entry point of the console application. // # Include "stdafx. H "// contains the DLL header file and declares the import function # include ".. /.. /nonemfcdll. H "# include <iostream> using namespace STD; // implicit call to include the generated lib file in the Project # pragma comment (Lib ,".. /.. /nonemfcdll/debug/nonemfcdll. lib ") int _ tmain (INT argc, _ tchar * argv []) {cout <" 23 + 4 = "<add (23, 4) <Endl; show (); Return 0 ;}
5. Display call
Non-MFC Program
// Loadnonmfcdll. cpp: defines the entry point of the console application. // # Include "stdafx. H "// contains the DLL header file and declares the import function # include ".. /.. /nonemfcdll. H "// The loadlibrary function requires # include <windows. h> # include <iostream> using namespace STD; // implicit call to include the generated lib file in the project // # pragma comment (Lib ,".. /.. /nonemfcdll/debug/nonemfcdll. lib ") // define the export function type typedef int (* padd) (const int A, const int B); typedef void (* pshow )(); /** display Call DLL: Call the dynamic link library in the program using loadlibrary or afxloadlibrary provided by MFC, and then use g The etprocaddress function obtains the function entry. When the program exits, the afxfreeloadlibrary provided by freelibrary or MFC is applied to release the dynamic link library. The advantage of calling DLL is that the program is simple. Fast. * The disadvantage is that it is not conducive to the use of DLL that outputs too many functions. * // Display the Call DLL example void test () {hinstance hdll = NULL; // load dllhdll =: loadlibrary (text (".. /.. /nonemfcdll/debug/nonemfcdll. DLL "); If (hdll = NULL) {MessageBox (null, text (" nonemfcdll. DLL loading failed! "), Text (" error "), mb_ OK | mb_iconerror); return;} // declare the export function pointer padd paddproc = NULL; pshow pshowproc = NULL; // get the export function address through getprocaddress and assign the pointer paddproc = (padd): getprocaddress (hdll, "add"); pshowproc = (pshow): getprocaddress (hdll, "show"); // check the result. If the result is successful, call the function if (paddproc = NULL) {MessageBox (null, text ("nonemfcdll. DLL file cannot find the export function Add "), text (" cannot find the export function "), mb_ OK | mb_iconinformation );} else {cout <"23 + 4 =" <paddproc (23, 4) <Endl;} If (pshowproc = NULL) {MessageBox (null, text ("nonemfcdll. DLL file cannot find export function show "), text (" export function not found "), mb_ OK | mb_iconinformation);} else {pshowproc ();} // release the loaded dll: freelibrary (hdll);} int _ tmain (INT argc, _ tchar * argv []) {// cout <"23 + 4 =" <add (23, 4) <Endl; // show (); test (); Return 0 ;}
MFC Program
Void cmfcloadalldlg: onbnclickedbutton1 () {// todo: add the control notification handler code here // define the export function type typedef void (* pshow) (); hinstance hdll = NULL; // load dllhdll =: loadlibrary (text (".. /.. /nonemfcdll/debug/nonemfcdll. DLL "); If (hdll = NULL) {MessageBox (text (" nonemfcdll. DLL loading failed! "), Text (" error "), mb_ OK | mb_iconerror); return;} // declare the export function pointer pshow pshowproc = NULL; // get the export function address through getprocaddress and assign the value to the pointer pshowproc = (pshow): getprocaddress (hdll, "show"); // check the result, then call the function if (pshowproc = NULL) {MessageBox (text ("nonemfcdll. DLL file cannot find export function show "), text (" export function not found "), mb_ OK | mb_iconinformation);} else {pshowproc ();} // release the loaded dll: freelibrary (hdll );}
Running result: