Solved

Is the WIN95 version of GetActiveDocument broken?

Posted on 1998-06-17
4
184 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 37
Get filename and folder into excel 7 69
twoTwo  challenge 35 93
Modbus - whats the maximum I can store in one register? 4 78
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 (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now