This wizard allows you to build a managed C++ dynamic link library
which also serves as an MFC regular DLL. I developed this wizard when
I was trying to figure out a way to convert MFC controls into windows forms
control (an article on this would follow). Since I still used
existing MFC code through IJW it made sense to make the managed assembly
also a MFC regular DLL.
The wizard creates following files :-
- <project name>.cpp - Implementation and declaration of CWinApp derived
class.
- <project name>.h - Decalration of a managed class Class1
- AssemblyInfo.cpp - Assembly attributes decalrations
- stdafx.h - Has #includes for common MFC headers and #using for
mscorlib.dll.
- stdafx.cpp - To create precompiled header file
- resource.h
- <project name>.rc
- res\<project name>.rc2
The wizard takes care of all the required project settings. Here is how the
<project name.cpp> file looks like
#include "stdafx.h"
#include "Test.h"
#pragma unmanaged
class CTestApp : public CWinApp
{
public:
CTestApp();
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
END_MESSAGE_MAP()
CTestApp::CTestApp()
{
}
CTestApp theApp;
BOOL CTestApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
#pragma managed
Note that the implementation is enclosed within #pragma unmanaged and #pragma
managed blocks to force the generation of native code. By default any file
include in the project gets compiled into IL.
I recommend that you build a separate MFC static library for any MFC code
you want to include in the managed code and link it with this DLL. Place
only managed code in this DLL. This clean separation turns out to be very
useful. One of the thing to be careful about is to mainatin the state of MFC
module using AFX_MANAGE_STATE
If this DLL is dynamically linked
against the MFC DLLs (default). Any managed methods called from external
sources should be enclosed with AFX_MANAGE_STATE(AfxGetStaticModuleState());
This
is neccessary if the code calls any (well most) MFC functions.
Here are the steps you must take to install the wizard :-
- Unzip all files to any directory. Lets call
the directory <install dir>
- Copy ManagedMFCDLL.vsdir, ManagedMFCDLL.vsz, ManagedMFCDLL.ico
to <vsinstalldir>/VC7/VCProjects directory. <vsinstalldir> is
the directory where you installed VS.NET.
- Finally you need to modify ManagedMFCDLL.vsz which looks like this
VSWIZARD 7.0
Wizard=VsWizard.VsWizardEngine
Param="WIZARD_NAME = ManagedMFCDLL"
Param="ABSOLUTE_PATH = G:\wksrc\ManagedMFCDLL"
Param="FALLBACK_LCID = 1033"
Param="WIZARD_UI = FALSE"
Param="SOURCE_FILTER = txt"
You need to replace g:\wksrc\ManagedMFCDLL to the path where you
unzipped the files (<install dir>).
That's all there is to it.