VC ++创建Cdialog类断言错误

时间:2015-08-19 16:01:59

标签: c++ mfc

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

我只是试图实例化一个Cdialog类,我正在创建一个dll,最终需要在Window中有一些activeX命令。

我尝试了几种方法来实例化,但我总是得到一个断言错误。

所以在我的calldllclass.h中,我有:

/page/2

在我的课程中扩展了Cdialog:

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

我创建了一个资源类:

//{{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}}

最后在我的cpp中我有:

#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\0"
END

2 TEXTINCLUDE MOVEABLE PURE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
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"
    "\0"
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
/////////////////////////////////////////////////////////////////////////////

当obj-> ShowWindow(SW_SHOW);被称为我得到了 调试断言失败! winocc.cpp第329行

// 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;
}

我在showWindow之后添加了一个更新,它返回1400

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);
}

添加回答sugenstion 我有这个初始化方法

   DWORD dw = GetLastError();

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

2 个答案:

答案 0 :(得分:0)

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

另外,根据MSDN docs on the constructor

  

要构建无模式对话框,使用受保护的形式   CDialog构造函数。构造函数是受保护的,因为您必须   派生自己的对话框类以实现无模式对话框。   构建无模式对话框分为两步。第一   调用构造函数;然后调用Create成员函数来创建一个   基于资源的对话框...

因此,请将您的类声明为CMyclass() : CDialog() {...},并使用以下代码显示它:

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

P.S。:不要忘记在程序结束时删除变量!

答案 1 :(得分:0)

我的代码是对的,

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

为我解决这个问题

相关问题