VC++ 创建Cdialog Class 断言错误

VC++ create Cdialog Class Assertion error

我完全是个菜鸟 Visual Studio,这可能是个愚蠢的错误。

我只是想实例化一个 Cdialog class,我正在制作一个最终需要在 Window.

中包含一些 activeX 命令的 dll

我尝试了多种实例化方法,但总是出现断言错误。

所以在我的 calldllclass.h 我有:

#pragma once
__declspec(dllexport) long init();

在我的 class 中扩展 Cdialog:

//{{AFX_INCLUDES()
//}}AFX_INCLUDES

class CMyclass: public CDialog{

#include "resource.h"

public:
    CMyclass(CWnd* pParent = NULL)
    : CDialog(100, pParent){
        //{{AFX_DATA_INIT(CMyclass)
        //}}AFX_DATA_INIT   
    };  // standard constructor
    virtual ~CMyclass(){};
    long calc();
// Dialog Data
    //{{AFX_DATA(CMyclass)
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyclass)
    protected:
    //}}AFX_VIRTUAL

protected:
    // Generated message map functions
    //{{AFX_MSG(CMyclass)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()   

};
//{{AFX_INSERT_LOCATION}}

我创建了一个资源 Class:

#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE MOVEABLE PURE 
BEGIN
    "resource.h[=14=]"
END

2 TEXTINCLUDE MOVEABLE PURE 
BEGIN
    "#include ""afxres.h""\r\n"
    "[=14=]"
END

3 TEXTINCLUDE MOVEABLE PURE 
BEGIN
    "#define _AFX_NO_SPLITTER_RESOURCES\r\n"
    "#define _AFX_NO_OLE_RESOURCES\r\n"
    "#define _AFX_NO_TRACKER_RESOURCES\r\n"
    "#define _AFX_NO_PROPERTY_RESOURCES\r\n"
    "\r\n"
    "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
    "#ifdef _WIN32\r\n"
    "LANGUAGE 9, 1\r\n"
    "#pragma code_page(1252)\r\n"
    "#endif //_WIN32\r\n"
    "#include ""res\SimpleVC6SampleEvent.rc2""  // non-Microsoft Visual C++ edited resources\r\n"
    "#include ""afxres.rc""         // Standard components\r\n"
    "#endif\r\n"
    "[=14=]"
END

#endif    // APSTUDIO_INVOKED




/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_ABOUTBOX DIALOG DISCARDABLE  0, 0, 0, 0
STYLE DS_MODALFRAME
FONT 8, "MS Sans Serif"
BEGIN
END




#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////

最后在我的 cpp 中我有:

// SIMPLEDIALOGDLL.cpp : Defines the exported functions for the DLL application.
//
#pragma once
#include "stdafx.h"
#include "myclass.h"
#include "resource.h"
#include "dll2export.h"


BEGIN_MESSAGE_MAP(CMyclass, CDialog)
    //{{AFX_MSG_MAP(CMyclass)
    //}}AFX_MSG
END_MESSAGE_MAP()


long init(){  

    AfxEnableControlContainer();
    CoInitializeEx(NULL,COINIT_MULTITHREADED);
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    CMyclass * obj = new CMyclass();

    //obj->ShowWindow(SW_HIDE);
    obj->ShowWindow(SW_SHOW);

    long res = obj->calc();
    return (long) obj;
}

long CMyclass::calc(){
    return 1+1;
}

当obj->ShowWindow(SW_SHOW);叫做我得到一个 调试断言失败! winocc.cpp 第 329 行

BOOL CWnd::ShowWindow(int nCmdShow)
{
    ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));

    if (m_pCtrlSite == NULL)
        return ::ShowWindow(m_hWnd, nCmdShow);
    else
        return m_pCtrlSite->ShowWindow(nCmdShow);
}

演出后我放了一点更新Window它returns 1400

   DWORD dw = GetLastError();

添加答案建议 我有这个初始化方法

AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

CMyclass *obj = new CMyclass();

if(obj->Create(100)){
    obj->ShowWindow(SW_HIDE);
    DWORD dw = GetLastError();
}else{
    DWORD dw = GetLastError();
}

现在我在 obj->Create(100)); objcore.cpp 行:40

@ScottMcP-MVP 是对的,无模式对话框需要调用 Create method.

此外,根据 MSDN docs on the constructor

To construct a modeless dialog box, use the protected form of the CDialog constructor. The constructor is protected because you must derive your own dialog-box class to implement a modeless dialog box. Construction of a modeless dialog box is a two-step process. First call the constructor; then call the Create member function to create a resource-based dialog box...

所以像CMyclass() : CDialog() {...}一样声明你的class并使用下面的代码来显示它:

CMyclass * obj = new CMyclass();
if (obj->Create(100))
    obj->ShowWindow(SW_SHOW);
else
{ << error >>
}

P.S.: 不要忘记删除程序末尾的变量!

我的代码是正确的,

许多小时后,我去更改 Visual Studio 的编译器选项并在调试信息格式中放入程序数据库 (/Zi)

帮我修好了