Solved

Is the WIN95 version of GetActiveDocument broken?

Posted on 1998-06-17
4
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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