m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); Assertion Failed: at afxwin1.inl

亡梦爱人 提交于 2019-12-13 04:53:02

问题


I am getting some weird error as Assert Failed f:\dd\...\include\afxwin1.inl. I am searched in Google some solutions, some solutions are for commenting this line (m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);) in Release mode to make it work. But after commenting that line I am getting some more errors.

I have taken a dialog based MFC application. It was working absolutely fine when it was an application.exe. My requirement is to make it static library and I will have another console application which will become main application.exe, I am calling InitInstance from that .exe. Once it fatches the line,

CDialogDlg::CDialogDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CDialogDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

It throws above error.

In my application.cpp

#include "stdafx.h"
#include "DialogDlg.h"
#include "Dialog.h"
#include "afxwin.h"
#include "Resource.h"
#include <afxinet.h> 
CDialogApp theApp;
int _tmain(int argc, _TCHAR* argv[])
{
    //CInitApp cpp;
    theApp.InitInstance();
    return 0;
}

Dialog.cpp

#include "stdafx.h"
#include "Dialog.h"
#include "DialogDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDialogApp

BEGIN_MESSAGE_MAP(CDialogApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CDialogApp construction

CDialogApp::CDialogApp()
{
    m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
}
//CDialogApp theApp;// I have commented this code as I am declaring it in mainapplication

BOOL CDialogApp::InitInstance()
{
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);
    CWinApp::InitInstance();
    AfxEnableControlContainer();
    CShellManager *pShellManager = new CShellManager;
    //SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    CDialogDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
}
    else if (nResponse == IDCANCEL)
    {
    }

    if (pShellManager != NULL)
    {
        delete pShellManager;
    }

    return FALSE;
}

I have commented CDialogApp theApp; line in Dialog.cpp as I am calling it in mainapplication .exe The problem happen when it reaches CDialogDlg dlg;. Please help me how to resolve this error.

In other way is it possible to set a dialog based application as static library. If yes then why am I getting this error. I have tried making the main application in Windows and console based too. Please find the screenshot for better understing, what am I trying to do.


回答1:


A static library does not contain any resources, but your dialog code attempts to load icon and dialog template resources. Did you move the resources into the console app? (I don't know if that would work, but it certainly won't work if you don't.)

The conventional and supported solution is to put the dialog code into a DLL instead of a static lib. A DLL can contain the resources.



来源:https://stackoverflow.com/questions/19367464/m-hicon-afxgetapp-loadiconidr-mainframe-assertion-failed-at-afxwin1-inl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!