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.  


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.