1) Build DLL
Established two files xxx.h, xxx.cpp
Xxx.h content is as follows:
#ifdef Build_xxx_dll
#define EXPORT __declspec (dllexport)
#else
#define EXPORT __declspec (dllimport)
#endif
extern "C" {
EXPORT void example (void);
... ...
}
Xxx.cpp content is as follows:
#define Build_xxx_dll
#include "xxx.h"
void Example (void)
{
}
... ...
Then compile from the DOS console (assuming you have installed MINGW and added environment variables)
g++-shared-wl,--kill-at,--output-def,xxx.def-o xxx.dll xxx.cpp
(because C + + implements the function overloading by modifying the function name, we use the extern "C" with the--kill-at compile option to avoid the correction of the function name, and the Build_xxx_dll macro is used to select the function of the prototype declaration)
2) Call DLL statically
Add the following content to the new document Yyy.cpp;
#include "xxx.h"
#pragma comment (lib, "Xxx.dll")
The generated DLLs do not require DEF files and CPP files and can also be
If you do not include xxx.h, you need to add the function prototype declaration in the H file into the Yyy.cpp
You need to add a DLL at compile time, like this:
g++-L-o yyy.exe yyy.cpp xxx.dll
3) Dynamic Call DLL
First, you need to include windows.h
#include <windows.h>
You also need a handle to save the loaded DLL file
HInstance hdll=loadlibrary ("Xxx.dll");
The corresponding function pointer type that declares the required function
typedef void (*PFUNC) (void);
Get a pointer to a function
Pfunc pf= (pfunc*) GetProcAddress (hDLL, "example");
To release the DLL file when you are finished using
FreeLibrary (hDLL);