5,442,164 members and growing! (15,818 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Intermediate License: The Code Project Open License (CPOL)

VC++ Appwizard to create a Managed MFC Regular DLL

By Rama Krishna Vavilala

A wizard to create a Managed C++ class library which is also a MFC regular DLL
C++/CLI, VC7, C++, Windows, .NET, .NET 1.0, MFC, VS.NET2002, Visual Studio, Dev

Posted: 3 Mar 2002
Updated: 8 Mar 2002
Views: 163,423
Bookmarked: 24 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
29 votes for this Article.
Popularity: 6.04 Rating: 4.13 out of 5
1 vote, 6.7%
1
2 votes, 13.3%
2
0 votes, 0.0%
3
2 votes, 13.3%
4
10 votes, 66.7%
5

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)

About the Author

Rama Krishna Vavilala



Occupation: Architect
Location: United States United States

Other popular C++ / CLI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionDoes not work in VS 2005memberdaaboots4:46 27 Dec '07  
GeneralRe: Does not work in VS 2005mvpRama Krishna Vavilala5:55 27 Dec '07  
GeneralRe: Does not work in VS 2005memberdaaboots6:03 27 Dec '07  
QuestionThis is a great Hack, but why does the class derive from CWinApp?membergunag4:14 10 Jul '06  
AnswerRe: This is a great Hack, but why does the class derive from CWinApp?memberRama Krishna Vavilala4:30 10 Jul '06  
GeneralRe: This is a great Hack, but why does the class derive from CWinApp?membergunag23:52 10 Jul '06  
GeneralProblem moving mfc dll to another machinesussAnonymous17:17 3 Dec '04  
QuestionRe: Problem moving mfc dll to another machinememberJulie JP5:53 22 Sep '05  
GeneralManaged Code in MFC 7.0 ApplicationmemberLiaqat9:11 4 May '04  
Generalerror RC2135 : file not found: myproject.tlbmemberfftz21:23 30 Mar '04  
GeneralRe: error RC2135 : file not found: myproject.tlbmemberruida21:45 18 Apr '04  
GeneralRe: error RC2135 : file not found: myproject.tlbmemberEliya Mirov22:44 5 May '07  
GeneralChanges for VS.NET v.7.1memberigor_patlan7:58 6 Feb '04  
GeneralRe: Changes for VS.NET v.7.1sussmensikd2:16 21 Jan '05  
GeneralRe: Changes for VS.NET v.7.1memberMartin_M_843:25 27 Feb '06  
GeneralLNK4243 ErrorsussKeith Rule14:28 4 Feb '04  
Generallink MFC->C++.NETmemberkikos french .NET developer5:31 22 Jan '04  
GeneralDoes not work in VS 2003membersalims21:58 10 Nov '03  
GeneralRe: Does not work in VS 2003 - Fixmemberrcgosse12:51 25 Jan '04  
GeneralI get an afxwin1.inl assertion on line 26 when trying to use an MFC dialog or frame from C#.memberjohnWise10:37 27 Oct '03  
GeneralRe: I get an afxwin1.inl assertion on line 26 when trying to use an MFC dialog or frame from C#.memberjohnWise11:40 27 Oct '03  
GeneralMemory LeakmemberUma Mahes19:14 7 Jul '03  
GeneralRe: Memory LeakmemberJimCryer5:33 1 Aug '03  
GeneralRe: Memory LeakmemberUma Mahes0:57 5 Aug '03  
GeneralRe: Memory LeakmemberYuanGuo21717:55 29 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Mar 2002
Editor: Chris Maunder
Copyright 2002 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project