Which is the best way to use an mfc class dialog as library from a project to another?

AlexS13 41 Reputation points
2022-01-04T11:40:31.343+00:00

Hello. I have two projects. Project A which is an MFC Application (generated from an API wizard) that I need to have only one dialog and call the rest of dialogs from Project B.
When I include the MFC dialog class header and the source file application runs without a problem.

#include "CTestDlg.h"
#include "CTestDlg.cpp"

If I include .obj file from linker settings application thow an exception in this function:

AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()
{ ASSERT(afxCurrentInstanceHandle != NULL);
return afxCurrentInstanceHandle; }

I run this code in project A:

   AFX_MANAGE_STATE(AfxGetStaticModuleState());
   HINSTANCE prevRes = AfxGetResourceHandle();
   HINSTANCE hRes = NULL;
   hRes = LoadLibrary(_T("M:\\AS\\TestProjects\\AddIn\\x64\\Debug\\Dialogs.dll"));

   if (hRes)
   {
       AfxSetResourceHandle(hRes);
   }

   CTestDlg dlg;
   dlg.DoModal();

I tested with two defaults mfc applications and I had no problem, even if I include dialog classes from linker. From the debuggin that I did, I think is something related to the resource files.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,525 questions
{count} votes

Accepted answer
  1. RLWA32 40,276 Reputation points
    2022-01-04T12:38:08.887+00:00

    As I suggested in https://learn.microsoft.com/en-us/answers/questions/664560/trying-to-include-an-mfc-dialog-from-a-library-int.html if you want to re-use your custom MFC dialogs in multiple MFC projects place them in an MFC Extension DLL.


0 additional answers

Sort by: Most helpful