Solved

Is the WIN95 version of GetActiveDocument broken?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 122
Folder Comparison 12 52
Complete beginner needs help making a cron job 9 104
canBalance challenge 34 65
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

18 Experts available now in Live!

Get 1:1 Help Now