Click here to Skip to main content
12,395,143 members (65,134 online)
Click here to Skip to main content
Add your own
alternative version

Stats

220.6K views
2K downloads
32 bookmarked
Posted

VC++ Appwizard to create a Managed MFC Regular DLL

, 8 Mar 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
A wizard to create a Managed C++ class library which is also a MFC regular DLL
<!-- Download Links --> <!-- Article image -->

Sample Image - ManagedMFCDLL.gif

<!-- Add the rest of your HTML here -->

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)

Share

About the Author

Rama Krishna Vavilala
Architect
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Question[My vote of 1] rofl i learned alot about nothing from this article Pin
a ahole26-Jun-11 8:19
membera ahole26-Jun-11 8:19 
QuestionCould you please update to support VS2005? Pin
wshcdr5-May-10 21:04
memberwshcdr5-May-10 21:04 
GeneralUse VB2005 dll in unmanaged vc++ Pin
cmasantos22-Oct-08 1:50
membercmasantos22-Oct-08 1:50 
QuestionDoes not work in VS 2005 Pin
daaboots27-Dec-07 3:46
memberdaaboots27-Dec-07 3:46 
GeneralRe: Does not work in VS 2005 Pin
Rama Krishna Vavilala27-Dec-07 4:55
mvpRama Krishna Vavilala27-Dec-07 4:55 
GeneralRe: Does not work in VS 2005 Pin
daaboots27-Dec-07 5:03
memberdaaboots27-Dec-07 5:03 
QuestionThis is a great Hack, but why does the class derive from CWinApp? Pin
gunag10-Jul-06 3:14
membergunag10-Jul-06 3:14 
AnswerRe: This is a great Hack, but why does the class derive from CWinApp? Pin
Rama Krishna Vavilala10-Jul-06 3:30
memberRama Krishna Vavilala10-Jul-06 3:30 
GeneralRe: This is a great Hack, but why does the class derive from CWinApp? Pin
gunag10-Jul-06 22:52
membergunag10-Jul-06 22:52 
GeneralProblem moving mfc dll to another machine Pin
Anonymous3-Dec-04 16:17
sussAnonymous3-Dec-04 16:17 
QuestionRe: Problem moving mfc dll to another machine Pin
Julie JP22-Sep-05 4:53
memberJulie JP22-Sep-05 4:53 
GeneralManaged Code in MFC 7.0 Application Pin
Liaqat4-May-04 8:11
memberLiaqat4-May-04 8:11 
Generalerror RC2135 : file not found: myproject.tlb Pin
fftz30-Mar-04 20:23
memberfftz30-Mar-04 20:23 
GeneralRe: error RC2135 : file not found: myproject.tlb Pin
ruida18-Apr-04 20:45
memberruida18-Apr-04 20:45 
GeneralRe: error RC2135 : file not found: myproject.tlb Pin
Eliya Mirov5-May-07 21:44
memberEliya Mirov5-May-07 21:44 
GeneralChanges for VS.NET v.7.1 Pin
igor_patlan6-Feb-04 6:58
memberigor_patlan6-Feb-04 6:58 
GeneralRe: Changes for VS.NET v.7.1 Pin
mensikd21-Jan-05 1:16
sussmensikd21-Jan-05 1:16 
GeneralRe: Changes for VS.NET v.7.1 Pin
Martin_M_8427-Feb-06 2:25
memberMartin_M_8427-Feb-06 2:25 
GeneralLNK4243 Error Pin
Keith Rule4-Feb-04 13:28
sussKeith Rule4-Feb-04 13:28 
Generallink MFC-&gt;C++.NET Pin
kikos french .NET developer22-Jan-04 4:31
memberkikos french .NET developer22-Jan-04 4:31 
GeneralDoes not work in VS 2003 Pin
salims10-Nov-03 20:58
membersalims10-Nov-03 20:58 
GeneralRe: Does not work in VS 2003 - Fix Pin
rcgosse25-Jan-04 11:51
memberrcgosse25-Jan-04 11:51 
GeneralI get an afxwin1.inl assertion on line 26 when trying to use an MFC dialog or frame from C#. Pin
johnWise27-Oct-03 9:37
memberjohnWise27-Oct-03 9:37 
GeneralRe: I get an afxwin1.inl assertion on line 26 when trying to use an MFC dialog or frame from C#. Pin
johnWise27-Oct-03 10:40
memberjohnWise27-Oct-03 10:40 
GeneralMemory Leak Pin
Uma Mahes7-Jul-03 18:14
memberUma Mahes7-Jul-03 18:14 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 9 Mar 2002
Article Copyright 2002 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid