Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > General Discussion
  New Posts New Posts RSS Feed - AfxGetMainWnd() returns NULL?
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

AfxGetMainWnd() returns NULL?

 Post Reply Post Reply
Author
Message
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Topic: AfxGetMainWnd() returns NULL?
    Posted: 30 September 2007 at 8:55am
AfxGetMainWnd() is returning null in our application if we double click a file that we have registered with the shell.  Very strange as this means m_pMainWnd is NULL and crashes our app.
 
Any ideas why this would happen, any help much appreciated
 
Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2007 at 9:16am

Reason can be only in code you have. Trace where you created CMainframe and where you call AfxGetMainWnd() .

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 7:05am
It must be in our code, but it seems that when our app calls
 

if ( !ProcessShellCommand( cmdInfo ) )

 

this in turn calls OnOpenDocument (which we have overridden) but if we pass a filename on the commandline then m_pMainWnd is null, if we dont pass a filename then it is correct

Strange, any ideas much appreciated
Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 8:50am
Hi,
 
Obviously you  create CMainFrame after ProcessShellCommand call.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 8:57am

Hi

 
But this is not the case:
 

CSingleDocTemplate *pDocTemplate;

pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS( CMainDoc ), RUNTIME_CLASS( CMainFrame ), RUNTIME_CLASS( CWorkspaceView ) );

AddDocTemplate( pDocTemplate );

EnableShellOpen();

CCommandLineInfo cmdInfo;

ParseCommandLine( cmdInfo );

if ( !ProcessShellCommand( cmdInfo ) )

{
// do error
}
 
 
 
See the above works fine if I just run the app, but if I pass a command line to load a file it then does it
 
it's like something is stopping m_pMainWnd from initialising as it creates the CMainFrame etc. for us using the doc architecture
 
Any ideas much appreciated
Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 9:16am
You didn't paste lines _after_ ProcessShellCommand where you create instance of CMainFrame.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 9:21am
Ah ok, I must be blind and getting dumb the older I get
 
Anychance you can show example below please
Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 10:19am
Surely the document architecture creates the CMainFrame instance in the above code, all we do after is

// show and update window

m_pMainWnd->ShowWindow( SW_SHOW );

m_pMainWnd->UpdateWindow();

Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 10:29am
but m_pMainWnd is NULL :(
Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
Roger View Drop Down
Newbie
Newbie


Joined: 06 February 2006
Location: United Kingdom
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote Roger Quote  Post ReplyReply Direct Link To This Post Posted: 01 October 2007 at 11:57am
AIUI, m_pMainWnd only becomes valid after calling ProcessShellCommand. However, during this function, OnNewDocument() gets called before the frame has been created.
 
This only happens the first time the program starts, in every other call of OnNewDocument(), the main window pointer will be valid.
 
Can you defer your processing until OnInitialUpdate() for a view?
 
Regards,
Roger
Back to Top
terrym View Drop Down
Senior Member
Senior Member


Joined: 13 April 2007
Status: Offline
Points: 836
Post Options Post Options   Thanks (0) Thanks(0)   Quote terrym Quote  Post ReplyReply Direct Link To This Post Posted: 02 October 2007 at 4:16am
Thanks for above, fixed now as we rewrote our own ProcessShellCommand, which works perfect, not sure why the original one worked and after a few changes it stopped, but thanks for above.
 
Just glad to get it working again as behind schedule now ;)
Thank you,
Terry Mancey

email terry@tmancey.ltd.uk | linkedin www.tmancey.ltd.uk | twitter @tmancey
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.156 seconds.