Export classes in C ++ DLL

Source: Internet
Author: User

Develop a small program today
Requirement: Make a DLL that contains a function. The main function of this function is to display a MessageBox. The caller calls this function to display some messages.

First, define a class. Because we should instantiate this class in use, we should add _ declspec (dllexport) before this class to ensure that the class constructor is exported.
Because we use MFC, we should add it before the code.

# Ifndef _ afxwin_h __
# Error include 'stdafx. H' before including this file for PCH
# Endif

//---------------------------------------------------------------

# Pragma once
# Ifndef _ afxwin_h __
# Error include 'stdafx. H' before including this file for PCH
# Endif

Class _ declspec (dllexport) cxymesstest
{
Public:
Cxymesstest (void );
~ Cxymesstest (void );

Void cxyshowmess (lpctstr mess );
};
// Class implementation

# Include "stdafx. H"
# Include ". cxymesstest. H"

Cxymesstest: cxymesstest (void)
{
}

Cxymesstest ::~ Cxymesstest (void)
{
}
Void cxymesstest: cxyshowmess (lpctstr mess)
{
Afxmessagebox (mess, mb_ OK, 0 );
}

Next, define the function to be exported in the def file.

Step 3: Call
Step 1: C ++ Application
Step 2: copy the generated DLL to the DEBUG directory,
Step 3: copy the. h file of C ++ DLL to the app directory and add it to the app.
Step 4: # include "cxymesstest. H"
Call it.
Cxymesstest * test;
Test = new cxymesstest ();
Test-> cxyshowmess ("Hell world ");

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.