Using MFC in WIN32 DLLs

Source: Internet
Author: User

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

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.