c++ - m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);断言失败 : at afxwin1. inl

标签 c++ visual-studio-2010 visual-c++ mfc static-libraries

我收到一些奇怪的错误,如 Assert Failed f:\dd\...\include\afxwin1.inl。我在 Google 中搜索了一些解决方案,一些解决方案用于在 Release模式下注释此行 (m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);) 以使其工作。但是在评论该行之后,我遇到了更多错误。

我采用了基于对话框的 MFC 应用程序。当它是 application.exe 时,它工作得非常好。我的要求是让它成为 static library 我将有另一个 console application 它将成为主要的 application.exe,我正在调用 来自 .exe 的 InitInstance。一旦它捕获了这条线,

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

}

它抛出上述错误。

在我的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;
}

我在 Dialog.cpp 中评论了 CDialogApp theApp; 行,因为我在 mainapplication .exe 中调用它 问题发生在它到达CDialogDlg dlg;。 请帮我解决这个错误。

以其他方式可以将基于对话框的应用程序设置为静态库。如果是,那为什么我会收到此错误。我也尝试过在 Windows 和控制台中制作主要应用程序。请找到屏幕截图以便更好地理解我要做什么。 enter image description here

最佳答案

静态库不包含任何资源,但您的对话框代码会尝试加载图标和对话框模板资源。您是否将资源移至控制台应用程序? (我不知道这是否行得通,但如果你不这样做肯定行不通。)

常规且受支持的解决方案是将对话框代码放入 DLL 而不是静态库。 DLL 可以包含资源。

关于c++ - m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);断言失败 : at afxwin1. inl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19367464/

相关文章:

c++ - 即使函数存在,也出现 undefined reference 错误

c++ - 用于保存不同元素的 2 个 vector 的单个迭代器 (c++)

c++ - 找不到过载成员

指向具有特定参数的函数的指针的c++ vector

c++ - 包含目录 Visual Studio 2010

c++ - 遇到值时中断

visual-studio-2010 - 运行已编辑的解决方案时,Visual Studio 2010崩溃

c++ - (继承类)c++ 不存在默认构造函数

c++ - 如何在 C++ 中将 CString 转换为 char?

c++ - 将字符串、整数和 float 序列化为字符数组,以便在没有库的情况下联网