m170115
m170115

Reputation: 121

Creating an MFC that can run other dlls. Debug Assertion Failed after project is ran

I have 2 functioning dialog based projects. My goal now is to create another simple MAIN project, such that when the MAIN project in ran, 2 push buttons appear and when the user clicks on them, the respective 2 projects will appear.

I have added the 2 projects into the MAIN project. Converted them into .dll, added the .lib files into additional dependencies and included the paths. I have also added the additional include paths so that i can include the .h of the 2 projects into the MAIN cpp file.

This is how part of my code looks like. This is just a test, that's why I only included 1 project instead of 2.

enter code here

// MAIN3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "MAIN3.h"
#include "MAIN3Dlg.h"
#include "afxdialogex.h"
#include "Resource3.h"
#include "test3Dlg.h"
.
.
.
void CMAIN3Dlg::OnBnClickedCancel()
{
    Ctest3Dlg TEST3_DIALOG;
    TEST3_DIALOG.DoModal();
}

The program can build, but when I run it, I get a debug assertion failed! The error points to this line: "ASSERT(AfxGetThread() == NULL);"

How can I fix this, or I doing something wrong? Any help would be appreciated. Thank you.

Upvotes: 0

Views: 269

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51825

Using MFC classes in both a main executable and in a DLL can cause many, many problems, due to the way MFC is initialised. Generally, you would need to define your DLL as an MFC Extension DLL (even if you are not actually adding extensions), in order that all modules share the same MFC instance. This is not a trivial task.

You can get more information here: https://learn.microsoft.com/en-us/cpp/build/extension-dlls?view=vs-2019

As far as I know, this is the only way you can switch control between your EXE and DLL(s) when both/all are concurrently using MFC.

Upvotes: 1

Related Questions