Dynamic connection library creation steps:
First, create a Non-MFC DLL dynamic link library
1. Open the file -> new -> Project Option, choose Win32 Dynamic-Link Library -> Sample Project
-> Engineering name: DLLDEMO
2, create a new .h file DLLDEMO.H
#ifdef dlldemo_exports
#define dllapi __declspec (dllexport)
#ELSE
#define dllapi __declspec (dllimport)
EXTERN "C" //
{
DLLAPI INT __STDCALL MAX (int A, int b); // __ stdcall makes non-C / C language to call API
}
#ENDIF
3. Import the dlldemo.h file in the dlldemo.cpp file and implement the max (int, int, int) function
#include "dlldemo.h"
DLLAPI INT __STDCALL MAX (Int A, INT B)
{
IF (a == b)
Return NULL;
ELSE IF (A> B)
Return A;
Else
Return B;
}
4, compiler generation dynamic connection library
Second, create a dynamic connection library DLLDEMO.DLL with the .def file.
1. Delete the dlldemo.h file in the DLLDEMO project.
2, in the DLLDEMO.CPP file header, delete the #include dlldemo.h statement.
3. Add a text file to this project, named dlldemo.def and write as follows:
Library mydllexportsmax @ 1
4, the compiler generates a dynamic connection library.
Dynamic linking steps:
First, implicit call
1. Establish a DLLCNSLTEST project
2, copy the file dlldemo.dll, dlldemo.lib to the directory where the DLLCNSLTEST project is located
3, add the following statement in Dllcnsltest.h:
#define dllapi __declspec (dllimport)
#pragma comment (LIB, "DLLDEMO.LIB") // Link to DLLDEMO.LIB files when the editor link
EXTERN "C"
{
DLLAPI INT __STDCALL MAX (Int A, INT B);
}
4, add the following statement in the dllcnsltest.cpp file:
#include "dllcnsltest.h" // or #include "dlldemo.h"
void main ()
{
Int value;
Value = max (2, 9);
Printf ("THE MAX VALUE IS% D / N", Value);
}
5, compile and generate applications DLLCNSLTEST.EXE
Second, explicit call
1. Establish a DLLWINTEST project
2. Copy the file dlldemo.dll to the directory where the DLLWINTEST project is located or Windows system directory.
3. View the function structure in the DLL file (DLLDemo.dll) with the Dumpbin.exe applet under VC / BIN.
4. Use the type definition keyword typedef, define the same function prototype pointer in the DLL.
example:
TYPEDEF INT (* LPMAX) (INT A, INT B); / / This statement can be placed in .h file
5. Load the DLL into the current application through LoadLibray () and returns the handle of the current DLL file.
example:
Hinstance hdll; // Declare a DLL instance file handle HDLL = LoadLibrary ("dlldemo.dll"); // Import DLLDEMO.DLL Dynamic Library
6. Get the function pointer imported into the application via the getProcAddress () function.
example:
LPMAX MAX;
Max = (lpmax) getProcAddress (HDLL, "MAX");
Int value;
Value = max (2, 9);
Printf ("THE MAX VALUE IS% D", Value);
7. After the function call is completed, use freelibrary () to uninstall the DLL file.
Freelibrary (HDLL);
8, compile and generate applications DLLWINTEST.EXE
Note: Explicit link applications do not need to use the corresponding lib files when compiling.