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
Solved

Is the WIN95 version of GetActiveDocument broken?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand macro to include alternate header placement and naming 5 75
Use of condition with 'serial' in ansible 2 70
wordmultiple challenge 12 140
Sed question 2 100
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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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