Search code examples
visual-c++mfcmdi

Doc ptr always coming NULL in MDI application


I am working on an MDI application where I have 2 views. Here is code to add views and document:

CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_STRING_FILTERWINDOW,
        RUNTIME_CLASS(CEmuDiagnosticsClientDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CFilterWindow));
if (!pDocTemplate)
    return FALSE;
AddDocTemplate(pDocTemplate);


//adding another template    
CMultiDocTemplate* pDocTemplate1;
pDocTemplate1 = new CMultiDocTemplate(IDR_STRING_SIGNALWINDOW,
        RUNTIME_CLASS(CEmuDiagnosticsClientDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CSignalWindow));
if (!pDocTemplate1)
    return FALSE;
AddDocTemplate(pDocTemplate1);

I have 2 views and 1 Document.

My requirement is to update CSignalWindow view based on some notification. When I run this application, I have opened a CSignalWindow view and to update this I have written following piece of code:

for( POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL; )
{
    pTempl = AfxGetApp()->GetNextDocTemplate(pos );
    for( POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )
    {
        if (pos1 == NULL)
            break;
        CDocument* pDoc = pTempl->GetNextDoc( pos1 );
        for( POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL; )
        {
            CView* pView = pDoc->GetNextView( pos2 );
            if( pView->IsKindOf( RUNTIME_CLASS(CSignalWindow) ) )
            {
                pView->UpdateData(true);
            }
        }
    }
}

But pos1 is always coming NULL at this line:

for( POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )

I am unable to understand why it is returning NULL always?


Solution

  • There is copy-paste-error in this line:

    POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )
    

    It should be:

    POSITION pos1 = pTempl->GetFirstDocPosition(); pos1!= NULL; )