• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:


Hi, All:

I have a big regular C++ program.  It is impossible to change it to MFC application framework.  But I really want to use the functions of MFC(such as the CDialog).  I am thinking if I can use MFC DLL and export them to my origianal C++ program.  Anybody have idea about this?  Please give a detailed example instead of some abstract words.  


  • 3
  • 3
  • 3
  • +1
1 Solution
Why do you want to make it a MFC DLL.
If you have a C++ program then you can directly use it's classes from MFC application framework.

Don't you want that.
Hi U can call MFC dll in C++ app
I have created a consol app like below
a simple main program
which calls dll's Init function
dll is MFC based
#include <iostream.h>
      extern "C" _declspec(dllexport) void Init();
void main(void)

in dll (MFC)
extern "C" _declspec(dllexport) void Init();

void Init()
      CMyDialog* myDlg = new CMyDialog;
      myDlg->Create(IDD_DIALOG1, NULL);

Hope this helps
tao_shaobinAuthor Commented:
basant:  Can you believe that the C++ program I have is a almost-MFC?  I mean it has its own message processing technology.  For dialogs, it creates the items one by one by using CreateItem() functions, something like that.  It is difficult for me to trace the program.  That is why I cannot use MFC for my program and why I want to use MFC DLL.  Hope this answers your curiosity.  Thanks for your message.

It is my mistake that I did not explain it well.  I want to export MFC classes to my c++ program.  About the technique, you mentioned.  It does work.  I have learned that in the book << programming visual c++>> 5th edition, by david j. kruglinski.  According to that book, it seems impossible to export MFC classes to ordinary c++ program.  This is my understanding.  I am not sure if it is correct.  Thanks.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

So far I know there is no direct way to use MFC DLL. But using the MFC partial Source code, one can do that but that is not straight forward.

What kind of MFC classes you r looking for. You can use other utility libraries e.g STL if feasible in ur case.
tao_shaobinAuthor Commented:
Hi, basant, I know STL programming.  I am just wandering if I could use CDialog etc.  I need not change the existing C++ program and enjoy MFC features.  What do you think?
STL don't give any CDialog kind of classes. It gives OS indepenedent basic Math's and other utility classes e.g map.

I can give One suggestion That might work.

You create a MFC DLL from there you use MFC classes and use CDialog there.

From that DLL you export your fns.

Thus you don't have to bother much about whether ur code will compile with MFC.

When you will load DLL, you will only know those functions which you had exported.

I will try to show You through some workable examples:
(i.e. the best way, that You make an MFC DLL with MFC extension project in VC++ and You will see what You have to do):
1.) You have to have a *.cpp file which contains the DllMain function:

// ISCommonCtrl.cpp : Defines the initialization routines for the DLL.

#include "stdafx.h"
#include <afxdllx.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;


extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
      // Remove this if you use lpReserved

      if (dwReason == DLL_PROCESS_ATTACH)
            TRACE0("ISCOMMONCTRL.DLL Initializing!\n");
            // Extension DLL one-time initialization
            if (!AfxInitExtensionModule(ISCommonCtrlDLL, hInstance))
                  return 0;

            // Insert this DLL into the resource chain
            // NOTE: If this Extension DLL is being implicitly linked to by
            //  an MFC Regular DLL (such as an ActiveX Control)
            //  instead of an MFC application, then you will want to
            //  remove this line from DllMain and put it in a separate
            //  function exported from this Extension DLL.  The Regular DLL
            //  that uses this Extension DLL should then explicitly call that
            //  function to initialize this Extension DLL.  Otherwise,
            //  the CDynLinkLibrary object will not be attached to the
            //  Regular DLL's resource chain, and serious problems will
            //  result.

            new CDynLinkLibrary(ISCommonCtrlDLL);
      else if (dwReason == DLL_PROCESS_DETACH)
            TRACE0("ISCOMMONCTRL.DLL Terminating!\n");
            // Terminate the library before destructors are called
      return 1;   // ok

After that You have to add to Your project the files which contains Your classes (CDialog or other things too), but for each class which you want to export you have to add :
"AFX_EXT_CLASS" keyword i.e.
class AFX_EXT_CLASS CMemDC : public CDC

and thats it!

Like i said the simpliest way is to generate an empty MFC DLL with MFC extension project in VC++ and after that to add Your classes with AFX_EXT_CLASS keyword.

If You want this way, tell me an e-mail address and I'll send You the sample project.

tao_shaobinAuthor Commented:
Hi,CF, I am interested in knowing more about your method.  My email address is taosb@hotmail.com.  It will be best if you could send me your sample project.

I have send You! You will find a readme file in it.
I hope it helped.

Ok. Here I am.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now