0
votes

I'm a complete noob with Visual Studio, it's probably a stupid mistake.

I'm only trying to instantiate a Cdialog class, I'm making a dll that in the end will need to had some activeX commands in the Window.

I'm tried several ways to instantiate, but I always getting an assertion error.

So in my calldllclass.h I had:

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

in my class that extend 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}}

I created an Resource 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\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
/////////////////////////////////////////////////////////////////////////////

and finally in my cpp I had:

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

When obj->ShowWindow(SW_SHOW); is called I get an Debug Assertion Failed! winocc.cpp Line 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);
}

a little update I put after showWindow it returns 1400

   DWORD dw = GetLastError();

add answer sugenstion I had this to init method

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

Now I'm having in obj->Create(100)); objcore.cpp Line: 40

2
I don't see a call to CDialog::Create anywhere. You need to create a dialog before you can show it.Tony The Lion
this is not enought? CMyclass(CWnd* pParent = NULL) : CDialog(100, pParent){Roy Bean
@RoyBean That creates the CDialog object but it does not create the window. You must call Create to create a modeless dialog window.ScottMcP-MVP
@ScottMcP-MVP I added CMyclass(CWnd* pParent = NULL) : CDialog(100,pParent){ //{{AFX_DATA_INIT(CMyclass) //}}AFX_DATA_INIT CDialog::Create(100,pParent); results in an assertions fail alsoRoy Bean
That's probably because dialog template "100" does not exist. I would recommend you read some "getting started" tutorial on MFC.Nikolay

2 Answers

1
votes

@ScottMcP-MVP is right, modeless dialog boxes need a call to the Create method.

Also, according to the 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...

So declare your class like CMyclass() : CDialog() {...} and use the following code to display it:

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

P.S.: don't forget to delete the variable at the end of your program!

0
votes

My code was right,

after many many hours, I went to change the compiler options of the Visual Studio and in Debug Information Format put Program Database (/Zi)

That fix it for me