DLL compilation for standard C + +

Source: Internet
Author: User

DLL is also a dynamic link library, the benefits of using DLL programming everyone should know, but how to do it, today I say.

First, you want to make sure that you want to export those functions, and then you add the following sentence to the name of the function you want to export:

Prefix of output function
#define Dll_export extern "C" __declspec (dllexport)

Dll_export VOID Exportfun ()
{
...
}

is not very simple AH. If you want to export an entire class or global variable, you need to do this:

Prefix of output class
#define Dll_class_export __declspec (dllexport)

To output the prefix of a global variable
#define Dll_global_export extern __declspec (dllexport)

Having done this, we'll call these functions in the main program, using the following method:

HINSTANCE hinst = NULL;
Hinst = LoadLibrary ("*.dll"); Your DLL file name

if (!hinst)
{
MessageBox (HWnd, "unable to load *.") Dll "," Error ", MB_OK);
}

Remember the Exportfun () function I declared above? I can't get that function directly, but I can take the address of that function. The function address is used in the same way as the function. However, for ease of use, you need to define the type of a function pointer. If you want to point to the Exportfun () above, the type of its function pointer is defined as follows:

typedef void (callback* Lpexportfun) (void)

What you need to do then is to declare a pointer and then get the address of Exportfun () in the DLL. The first parameter of the GetProcAddress function is the instance handle of the previously obtained DLL, followed by the function name of that function in the DLL.

Lpexportfun pfun = NULL;
Lpexportfun Pfun = (lpexportfun) GetProcAddress (hinst, "Exportfun");

All right, here we go. The final step is to call that function:

Pfun ();

Done..

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.