Recently, I meet a request that modify a DLL based on SDK to support MFC common class.It seems a easy task, but I meet a great difficult. So I have a idea of writing something to record my experience and hope others can benefit from it.
For there are some common class like CFileDialog,CFileFind,CString which provide a easy way to suit your needs.otherwise you would need multi-effort If you implement by API .Using MFC can save a lot of time. Following is the steps:
1. add Related Header file.
For Application based on SDK need include “windows.h”, When you uing MFC common class , adding “afx.h”would result in conflicting with “windows.h”. The way to solve it is removing “windows.h”,after that add #include “stdafx.h”in front of all “.H”header file.
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#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
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#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
Notes: above code must be added in front all head file and its order cann’t change, or it occurs some compile errors.
2. Modify compile setting.
Open Project->Setting->General, select Use MFC in a Shared DLL or Use MFC in static Library,and change Use runing-time library From Single-Threaded to Multithreaded From project->Setting->C/C++.
2. Modify compile setting.
Open Project->Setting->General, select Use MFC in a Shared DLL or Use MFC in static Library,and change Use runing-time library From Single-Threaded to Multithreaded From project->Setting->C/C++.
After above modification, I re-compile project, there occurs a error: error LNK2005: _DllMain@12 already defined in OCRDLL.obj. Don’t worry , you should only remove _USRDLL from preprocessor definitions seetings.
OK. You have successfully modify a sdk application to support MFC common class!