Print Page | Close Window

AfxGetMainWnd() returns NULL?

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: General Discussion
Forum Description: Topics Related to Visual C++ MFC Development in General
URL: http://forum.codejock.com/forum_posts.asp?TID=8230
Printed Date: 03 May 2024 at 8:51am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: AfxGetMainWnd() returns NULL?
Posted By: terrym
Subject: AfxGetMainWnd() returns NULL?
Date 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



Replies:
Posted By: Oleg
Date 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


Posted By: terrym
Date 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


Posted By: Oleg
Date Posted: 01 October 2007 at 8:50am
Hi,
 
Obviously you  create CMainFrame after ProcessShellCommand call.


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: terrym
Date 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


Posted By: Oleg
Date 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


Posted By: terrym
Date 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


Posted By: terrym
Date 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


Posted By: terrym
Date 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


Posted By: Roger
Date 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


Posted By: terrym
Date 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



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net