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