C + + generates a DLL and invokes a DLL's method instance (GO)

Source: Internet
Author: User

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);

C + + generates a DLL and invokes a DLL's method instance (GO)

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.