Click here to Skip to main content
15,887,585 members
Articles / Desktop Programming / MFC
Article

VC++ Appwizard to create a Managed MFC Regular DLL

Rate me:
Please Sign up or sign in to vote.
4.43/5 (15 votes)
8 Mar 2002CPOL2 min read 256.6K   2.1K   33   44
A wizard to create a Managed C++ class library which is also a MFC regular DLL

Sample Image - ManagedMFCDLL.gif

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

// This is the main DLL file.

#include "stdafx.h"

#include "Test.h"

#pragma unmanaged

//Place all unmanaged code between these blocks

// CTestApp
// See Test.cpp for the implementation of this class
//

class CTestApp : public CWinApp
{
public:
	CTestApp();

// Overrides
public:
	virtual BOOL InitInstance();

	DECLARE_MESSAGE_MAP()
};

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

// CTestApp

BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
END_MESSAGE_MAP()

// CTestApp construction

CTestApp::CTestApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CTestApp object

CTestApp theApp;

// CTestApp initialization

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 :-

  1. Unzip all files to any directory. Lets call the directory <install dir>
  2. Copy ManagedMFCDLL.vsdir, ManagedMFCDLL.vsz, ManagedMFCDLL.ico to <vsinstalldir>/VC7/VCProjects directory. <vsinstalldir> is the directory where you installed VS.NET.
  3. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Memory Leak Pin
JimCryer1-Aug-03 4:33
JimCryer1-Aug-03 4:33 
GeneralRe: Memory Leak Pin
Uma Mahes4-Aug-03 23:57
Uma Mahes4-Aug-03 23:57 
GeneralRe: Memory Leak Pin
YuanGuo21729-Apr-05 16:55
YuanGuo21729-Apr-05 16:55 
GeneralNot working in Visual Studio .NET 2003 Pin
Uma Mahes23-Jun-03 20:11
Uma Mahes23-Jun-03 20:11 
GeneralRe: Not working in Visual Studio .NET 2003 Pin
zahirhas6-Oct-03 5:32
zahirhas6-Oct-03 5:32 
GeneralRe: Not working in Visual Studio .NET 2003 Pin
sconnor13-Nov-04 11:58
sconnor13-Nov-04 11:58 
GeneralManaged Code in MFC Pin
Anonymous12-Jul-02 7:03
Anonymous12-Jul-02 7:03 
GeneralRe: Managed Code in MFC Pin
Rama Krishna Vavilala12-Jul-02 7:07
Rama Krishna Vavilala12-Jul-02 7:07 
GeneralRe: Managed Code in MFC Pin
hyph3n30-Nov-03 9:32
hyph3n30-Nov-03 9:32 
GeneralNew to Visual C++ Pin
20-May-02 11:29
suss20-May-02 11:29 
Generaldon't worry Pin
archi13-Mar-02 12:12
archi13-Mar-02 12:12 
GeneralRe: don't worry Pin
Rama Krishna Vavilala13-Mar-02 12:17
Rama Krishna Vavilala13-Mar-02 12:17 
GeneralGood Article! Pin
Josef Haslinger9-Mar-02 20:51
Josef Haslinger9-Mar-02 20:51 
GeneralRe: Good Article! Pin
Rama Krishna Vavilala10-Mar-02 8:13
Rama Krishna Vavilala10-Mar-02 8:13 
GeneralRe: Good Article! Pin
15-Mar-02 4:42
suss15-Mar-02 4:42 
GeneralRe: Good Article! Pin
Paul Selormey21-Nov-02 0:12
Paul Selormey21-Nov-02 0:12 
GeneralPlease share some information... Pin
Paul Selormey5-Mar-02 14:01
Paul Selormey5-Mar-02 14:01 
GeneralRe: Please share some information... Pin
Rama Krishna Vavilala5-Mar-02 14:54
Rama Krishna Vavilala5-Mar-02 14:54 
GeneralRe: Please share some information... Pin
Paul Selormey5-Mar-02 15:06
Paul Selormey5-Mar-02 15:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.