0

I get the following errors in my MFC Project: (Please note: the bolded lines matches the title of my Error List in Visual Studio 2022)

Severity Code Description Project File Line Suppression State

Error (active) E1086 the object has type qualifiers that are not compatible with the member function "CappApp::GetThisMessageMap" app C:\Projects\C++Projects\app\app.cpp 21

Error C2662 'const AFX_MSGMAP *CappApp::GetThisMessageMap(void)': cannot convert 'this' pointer from 'const CappApp' to 'CappApp &' app C:\Projects\C++Projects\app\app.cpp 21

Error C1189 #error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] main C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\atlmfc\include\afx.h 24

The last Error comes from an Error Directive, which prints the error as followed by C1189 into my error list. I ignored this in the past (because the program still executed), but I am in additional trouble with error C1086 and C1189. I don't know how to handle this. Could you please help?

This is the code from my MFC project

BEGIN_MESSAGE_MAP(CappApp, CWinAppEx)
    // Standard file based document commands
//  ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
//  ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
END_MESSAGE_MAP()

Please note: The lines ON_COMAND ... I commented out, because I am not using the MFC menu, but if I don't the following messages show:

Severity Code Description Project File Line Suppression State

Error C2248 'CWinApp::OnFileOpen': cannot access protected member declared in class 'CWinApp' app C:\Projects\C++Projects\app\app.cpp 24
Error C2248 'CWinApp::OnFileNew': cannot access protected member declared in class 'CWinApp' app C:\Projects\C++Projects\app\app.cpp 23

Also: in case I would comment out BEGIN_MESSAGE_MAP until and including END_MESSAGE_MAP, I would obtain these error messages:

Severity Code Description Project File Line Suppression State

Error LNK2001 unresolved external symbol "public: virtual struct AFX_MSGMAP const * __cdecl CappApp::GetMessageMap(void)const " (?GetMessageMap@CappApp@@UEBAPEBUAFX_MSGMAP@@XZ) app C:\Projects\C++Projects\app\app.obj 1

Error LNK1120 1 unresolved externals app C:\Projects\C++Projects\app\x64\Debug\app.exe 1

The following is the code I need fixed in order to go ahead with my efforts:

BEGIN_MESSAGE_MAP(CappApp, CWinAppEx)
    // Standard file based document commands
    //ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
    //ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
END_MESSAGE_MAP 

These macros are defined in winafx.h as follows:

#define DECLARE_MESSAGE_MAP() \
protected: \
    static const AFX_MSGMAP* PASCAL GetThisMessageMap(); \
    virtual const AFX_MSGMAP* GetMessageMap() const override; \

#define BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass)          \
    PTM_WARNING_DISABLE                                                     \
    template < typename type_name >                                         \
    const AFX_MSGMAP* theClass< type_name >::GetMessageMap() const          \
        { return GetThisMessageMap(); }                                     \
    template < typename type_name >                                         \
    const AFX_MSGMAP* PASCAL theClass< type_name >::GetThisMessageMap()     \
    {                                                                       \
        typedef theClass< type_name > ThisClass;                            \
        typedef baseClass TheBaseClass;                                     \
        __pragma(warning(push))                                             \
        __pragma(warning(disable: 4640)) /* message maps can only be called by single threaded message pump */ \
        static const AFX_MSGMAP_ENTRY _messageEntries[] =                   \
        {

#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
    PTM_WARNING_DISABLE \
    const AFX_MSGMAP* theClass::GetMessageMap() const \
        { return GetThisMessageMap(); } \
    const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
    { \
        typedef theClass ThisClass;                        \
        typedef baseClass TheBaseClass;                    \
        __pragma(warning(push))                            \
        __pragma(warning(disable: 4640)) /* message maps can only be called by single threaded message pump */ \
        static const AFX_MSGMAP_ENTRY _messageEntries[] =  \
        {

#define END_MESSAGE_MAP() \
        {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
    }; \
        __pragma(warning(pop))  \
        static const AFX_MSGMAP messageMap = \
        { &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
        return &messageMap; \
    }                                 \
    PTM_WARNING_RESTORE
// This is the end of BEGIN/END_MESSAGE_MAP in AfxWin.h
// =====================================================
END_MESSAGE_MAP()

Here is the additional code, requested by Min Xin Yu:

enter code here
// lc.cpp : Defines the class behaviors for the application.
//

#include "pch.h"
#include "framework.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "app.h"
#include "MainFrm.h"
#include "appDoc.h"
#include "appView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CappApp
//  const AFX_MSGMAP* PASCAL GetThisMessageMap();

BEGIN_MESSAGE_MAP(CappApp, CWinApp)
//ON_COMMAND(ID_APP_ABOUT, &CappApp::OnAppAbout)
//// Standard file based document commands
//ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
//ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
//// Standard print setup command
//ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()


// CappApp construction
CappApp::CappApp() noexcept{

// support Restart Manager
m_dwRestartManagerSupportFlags =      
                         AFX_RESTART_MANAGER_SUPPORT_ALL_ASPECTS;
#ifdef _MANAGED
// If the application is built using Common Language Runtime             
// support (/clr):
//     1) This additional setting is needed for Restart Manager 
//        support to work properly.
//     2) In your project, you must add a reference to 
//        System.Windows.Forms in order to build.
          System::Windows::Forms::Application::
SetUnhandledExceptionMode(System
::Windows::Forms::UnhandledExceptionMode::ThrowException);
#endif

// TODO: replace application ID string below with unique ID 
//string; 
// recommended ormat for string is 
// CompanyName.ProductName.SubProduct.VersionInformation
SetAppID(_T("lc.AppID.NoVersion"));

// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

// The one and only CappApp object

CappApp theApp;


// CappApp initialization

BOOL CappApp::InitInstance() {
// InitCommonControlsEx() is required on Windows XP if an 
// application
// manifest specifies use of ComCtl32.dll version 6 or later to 
// enable
// visual styles.  Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to 
// use in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();


// Initialize OLE libraries
if (!AfxOleInit()) {
    AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}

AfxEnableControlContainer();

EnableTaskbarInteraction(FALSE);

// AfxInitRichEdit2() is required to use RichEdit control
// AfxInitRichEdit2();

// Standard initialization
// If you are not using these features and wish to reduce the 
// size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something 
// appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4);  // Load standard INI file options 
                            // (including MRU)


// Register the application's document templates.  Document 
// templates
//  serve as the connection between documents, frame windows and 
// views
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CappDoc),
RUNTIME_CLASS(CMainFrame),       // main SDI frame window
RUNTIME_CLASS(CappView));
if (!pDocTemplate)
    return FALSE;
AddDocTemplate(pDocTemplate);

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);



// Dispatch commands specified on the command line.  Will return 
// FALSE if app was launched with /RegServer, /Register, 
// /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
    return FALSE;

// The one and only window has been initialized, so show and 
// update it
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}

int CappApp::ExitInstance()
  {
//TODO: handle additional resources you may have added
AfxOleTerm(FALSE);

return CWinApp::ExitInstance();
}

// CappApp message handlers


// CAboutDlg dialog used for App About

//class CAboutDlg : public CDialogEx
//{
//public:
//  CAboutDlg() noexcept;
//
//  // Dialog Data
//#ifdef AFX_DESIGN_TIME
//  enum { IDD = IDD_ABOUTBOX };
//#endif
//
//protected:
//  virtual void DoDataExchange(CDataExchange* pDX);    
// DDX/DDV support
//
//  // Implementation
//protected:
//  DECLARE_MESSAGE_MAP()
//};
//
//CAboutDlg::CAboutDlg() noexcept : CDialogEx(IDD_ABOUTBOX)
//{
//}
//
//void CAboutDlg::DoDataExchange(CDataExchange* pDX)
//{
//  CDialogEx::DoDataExchange(pDX);
//}
//
//BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
//END_MESSAGE_MAP()
//
//// App command to run the dialog
//void CappApp::OnAppAbout()
//{
//  CAboutDlg aboutDlg;
//  aboutDlg.DoModal();
//}

// CappApp message handlers

This was the .cpp file, if needed I also deliver the .h file. Thank you.

  • Why is this tagged for the Mac? – Andrew Truckle Jul 08 '23 at 08:32
  • Sorry, I made a mistake. This is for the windows based visual studio. – Michaelinscarbororough Jul 09 '23 at 13:39
  • The visual studio version is 2022 – Michaelinscarbororough Jul 09 '23 at 13:45
  • From the error: you are using /MD[d] and MFC in a static library which is conflicting. – Minxin Yu - MSFT Jul 10 '23 at 05:57
  • Yes, that was my thought as well, but I have no idea how to fix this. Do you think you could give me a hint? – Michaelinscarbororough Jul 10 '23 at 23:29
  • In project's properties->Advanced->Use of MFC-> Use MFC in a shared library. – Minxin Yu - MSFT Jul 11 '23 at 01:19
  • I tried without success. In case I used Standard Windows libraries I got the following: Severity Code Error (active) E0035 Error (active) E1086 Message lnt-uninitialized-local Error C1189 Error C1189 – Michaelinscarbororough Jul 11 '23 at 10:04
  • These are the errors, when changing to use MFC in shared libraries: Severity Code Description Message Conversion loses qualifiers Message see declaration of 'CappApp::GetThisMessageMap' Message while trying to match the argument list '()' Error C2662 'const AFX_MSGMAP *CappApp::GetThisMessageMap(void)': cannot convert 'this' pointer from 'const CappApp' to 'CappApp &' Error (active) E1086 the object has type qualifiers that are not compatible with the member function "CappApp::GetThisMessageMap" – Michaelinscarbororough Jul 11 '23 at 10:09
  • I consider above disturbing, because this is MFC code, only, nothing where I had made changes. – Michaelinscarbororough Jul 11 '23 at 10:11
  • Is there class CWinAppEx ? From your error, it is CWinApp. And can you provide a minimal reproducible example? – Minxin Yu - MSFT Jul 12 '23 at 08:41
  • Do you have a `DECLARE_MESSAGE_MAP()` within the class definition in your application object's header file? – Joseph Willcoxson Jul 12 '23 at 20:48
  • @JosephWillcoxson: Yes, I do. – Michaelinscarbororough Jul 16 '23 at 17:47
  • @Minxin Yu: Please check my question again, my code is included. Sorry for the delayed reply. – Michaelinscarbororough Jul 16 '23 at 18:02
  • I tested your app class, and it compiled well. – Minxin Yu - MSFT Jul 17 '23 at 06:44
  • These are the errors staying with my attempts to build the application: Severity Code Description Error C1189 #error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] Error LNK1266 error reading instrumentation file 'C:\Projects\C++Projects\app\x64\Debug\app.pgd'; File not found Message lnt-uninitialized-local Local variable is not initialized. Error MSB6006 "link.exe" exited with code 1266. Any comments? – Michaelinscarbororough Jul 18 '23 at 16:46
  • @Minxin Yu: How could you compile the app? No header file, no ... – Michaelinscarbororough Jul 18 '23 at 19:34
  • Create a new MFC SDI template with MFC standard named `app`. [![image](https://i.stack.imgur.com/TewFc.png) Replace the code of the app.cpp file with your code. Build the project. Done. Please complete your MFC project based on steps above. – Minxin Yu - MSFT Jul 19 '23 at 02:46
  • @Minxin Yu: These are remaining problems: Severity Code Description Error LNK1266 error reading instrumentation file 'C:\Projects\C++Projects\app\x64\Debug\app.pgd'; File not found Message lnt-uninitialized-local Local variable is not initialized. And also, in case the original app.cpp did compile on your system, why create a new app file. Thank you for your interest. – Michaelinscarbororough Jul 20 '23 at 07:05
  • @Minxin Yu: I did what you suggested. Could you help me with the remainder of my problems, please. – Michaelinscarbororough Jul 25 '23 at 00:50
  • Can you provide a minimal project via GitHub that reproduces the problem? – Minxin Yu - MSFT Jul 25 '23 at 07:18
  • Please give me some time, I never did this before. Possibly I will post all my code, because I have no idea, which code to eliminate in order to have a compiling project. – Michaelinscarbororough Jul 26 '23 at 18:43
  • Sorry, I did not do that. I believe I found out what is going wrong. It is some include file logic and I will inquire in another StackOverflow question. Thank you for bearing with me, all answers are appreciated and useful. – Michaelinscarbororough Aug 17 '23 at 14:33
  • Please see my include logic question here: https://stackoverflow.com/questions/76922995/include-file-logic – Michaelinscarbororough Aug 17 '23 at 15:32

0 Answers0