Solved

Is the WIN95 version of GetActiveDocument broken?

Posted on 1998-06-17
4
187 Views
Last Modified: 2013-11-20
No matter what I do, GetActiveDocument() seems to always return NULL. For example, I build a MDI app called Test using VC++5, accepting all the Wizard defaults. Then I add a menu item called Test, and add a handler to the CTestView class as shown below:

void CTestView::OnTest()
{
    CTestDoc* pDoc = GetDocument();
    ASSERT(pDoc);

    CFrameWnd* pFrame = DYNAMIC_DOWNCAST(CFrameWnd,theApp.m_pMainWnd);
    ASSERT(pFrame);

    if (pFrame)
    {
        CDocument* pGenericDoc = pFrame->GetActiveDocument();
        ASSERT(pGenericDoc);

        CTestDoc* pTestDoc = DYNAMIC_DOWNCAST(CTestDoc,pGenericDoc);
        ASSERT(pTestDoc);
    }
}

theApp is the CTestApp instance produced by the wizard; I add "extern CTestApp theApp;" at the top of TestView.cpp.

When I run the app, I of course get the standard initial blank document, so THERE IS an active document, and sure enough, the call to CTestView::GetDocument returns non-null (I put this in just to make sure I wasn't nuts). The rest of the code in the handler is copied from Mike Blaszczak's "Professional MFC" book, page 185, which gives an example of how you get a pointer to the current document from some place other than your view class.

Anyway, the ASSERT(pGenericDoc) always blows. pFrame is non-NULL, but maybe isn't initialized by MFC to actually point to the main window?? Have I maybe found something that works under NT but not 95??
0
Comment
Question by:jadcock
  • 2
4 Comments
 
LVL 3

Expert Comment

by:shaig
ID: 1318133
Get the acvive document through:
1. the document template
2. the application
0
 

Author Comment

by:jadcock
ID: 1318134
Re shaig answer: (2) is what I'm trying! theApp IS the application instance. Incidentally, rather than using theApp, AfxGetApp() will also give the same results. The question is, when, if ever, does GetActiveDocument() work?
0
 
LVL 1

Accepted Solution

by:
amby earned 50 total points
ID: 1318135
In sted of simple use of:
GetActiveDocument(),

because you are in a view, you must:
FrameWnd->GetActiveFrame()->GetActiveDocument()

you already have FrameWnd here because of your pFrame, so the anwer is:

pFrame->GetActiveFrame()->GetActiveDocument()

Have a nice day...

0
 

Author Comment

by:jadcock
ID: 1318136
Yes!!! The extra GetActiveFrame() fixes it. I suspect the original code might work for an SDI app (no child frames)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 64
Macro to save highlighted rows as a separate file 5 43
isEverywhere  challenge 19 75
either24  challenge 19 97
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question