Search code examples
visual-studio-2010visual-c++mfcmigration

How to locate (& remove) MFC dependent code in a project?


I've got a huge legacy project which uses MFC and COM. The idea being to migrate it to Linux (winelib doesn't work for this), I need to identify the portions of the code that use MFC. Strange thing is that the solution contains two DLL projects which inherit from CWinApp and instantiate it. Besides, I don't see the purpose of CWinApp in these DLL's because the actual GUI code is in a separate non-dll project.

Two questions:
1. Is there any tool which can help me locate the MFC specific code so that I can remove it? Already saw the Qt option.
2. Why is CWinApp instantiated (as below) in a DLL project which isn't doing any GUI work at all? Is it used for message passing? I don't see any such syntax though. Removing the CWinApp instantiation results in a pointer of another project not being initialized. Weird!

One of the DLL project's code goes like this:

#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include "dlldatax.h"
//removed some project-specific includes for posting on SO

#ifdef _MERGE_PROXYSTUB
extern "C" HINSTANCE hProxyDll;
#endif

CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_MyManager, CMyManager)
//removed some other similar lines as the line above
END_OBJECT_MAP()

// Component Category Helper Functions
static HRESULT CreateComponentCategory( CATID catid, WCHAR* catDescription );
static HRESULT UnRegisterCategory( CATID catid );

class CMyClassWrapperApp : public CWinApp
{
public:
    public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMyClassWrapperApp, CWinApp)
END_MESSAGE_MAP()

CMyClassWrapperApp theApp;

BOOL CMyClassWrapperApp::InitInstance()
{
#ifdef _MERGE_PROXYSTUB
    hProxyDll = m_hInstance;
#endif
    _Module.Init(ObjectMap, m_hInstance, &LIBID_MyClassWrapperLib);
    return CWinApp::InitInstance();
}

int CMyClassWrapperApp::ExitInstance()
{
    _Module.Term();
    return CWinApp::ExitInstance();
}

Solution

    1. In your MFC project settings, change to Use of MFC to Use Standard Windows Libraries. Then remove all includes starting with 'afx' from your StdAfx.h. The compilation errors will guide you to all MFC specific parts!
    2. CWinApp is there to allow your MFC executable to correctly use any MFC code in your DLL. The framework will initialize your DLL through this object. For non-MFC DLLs you would just use DllMain.

    Regarding COM and no MFC, I would start by googling this: atl com codeproject