Recently with the WIN32 DLL, in order to facilitate the use of some of the MFC library, and do not want to change the project, on the Internet to find a lot of methods, found no detailed introduction, some also do not work, and now successfully used in WIN32 DLL MFC, record a bit in case later use to forget
First, modify the precompiled header file (stdafx.h)
Add the following code to the StdAfx.h file, including some MFC header files, which can be copied in an MFC project.
#define_atl_cstring_explicit_constructors//some CString constructors will be explicit for the#include <afxwin.h>//MFC core components and standard components#include <afxext.h>//MFC Extensions#ifndef _afx_no_ole_support#include <afxole.h>//MFC OLE Classes#include <afxodlgs.h>//MFC OLE dialog box Classes#include <afxdisp.h>//MFC Automation Classes#endif //_afx_no_ole_support#ifndef _afx_no_db_support#include <afxdb.h>//MFC ODBC Database Classes#endif //_afx_no_db_support#ifndef _afx_no_dao_support#include <afxdao.h>//MFC DAO Database Classes#endif //_afx_no_dao_support#include <afxdtctl.h>//MFC support for Internet Explorer 4 common Controls#ifndef _afx_no_afxcmn_support#include <afxcmn.h>//MFC support for Windows common controls#endif //_afx_no_afxcmn_support
Second, modify the compilation configuration
I made some VS2003.
1. Use of general->mfc, properties-----use MFC in a static library (either dynamic or static)
2, linker-input,
Because the link order problem causes the link error when compiling, so we need to change the link order of two lib.
Ignore the two DLLs in the ' Ignore specified library ', respectively Uafxcw.lib and libcpmt.lib, and if it is a debug project, you need to fill in the LIBCMT.lib
Then fill in the ' Additional dependencies ' in the order of Uafxcw.lib Libcpmt.lib
Third, add the code in your main file
Join at the front
#ifdef _DEBUG#undefThis_fileStatic CharThis_file[] =__file__;#endif#defineNew Debug_new///////////////////////////////////////////////////////////////////////////////Global Data//The following symbol used to force inclusion of this module for _usrdll#ifdef _x86_extern "C"{int_afxforceusrdll;}#elseextern "C"{int__afxforceusrdll;}#endif
This does not derive a class from the CWinApp and then the external definition to use the MFC entry point, you can directly use the original written DllMain entry point
It's just a couple of mistakes in linking that piece.
Error LNK2005: "Private: __thiscall type_info::type_info ...
Last used:
Additional Dependencies: Msvcrtd.lib LIBCMTD.lib
Ignore specific libraries: libcmtd.lib;msvcrtd.lib
Compiler passed, MFC's class compiler also recognized
Using MFC in WIN32 DLLs