![]()  | 
 
AfxGetMainWnd() returns NULL? | 
 
    Post Reply  
   | 
  
| Author | |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
   
   Oleg  
   
   Senior Member  
   Joined: 21 May 2003 Location: United States Status: Offline Points: 11234  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     Posted: 01 October 2007 at 7:05am | 
 
| 
   
    
   It must be in our code, but it seems that when our app calls 
 
   
  
 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  | 
 |
![]()  | 
 |
   
   Oleg  
   
   Senior Member  
   Joined: 21 May 2003 Location: United States Status: Offline Points: 11234  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     Posted: 01 October 2007 at 8:50am | 
 
| 
   
    Hi, 
Obviously you  create CMainFrame after ProcessShellCommand call. 
    | 
 |
| 
   
     
     Oleg, Support Team 
   
  CODEJOCK SOFTWARE SOLUTIONS  | 
 |
![]()  | 
 |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     Posted: 01 October 2007 at 8:57am | 
 
| 
   
    Hi But this is not the case: 
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  | 
 |
![]()  | 
 |
   
   Oleg  
   
   Senior Member  
   Joined: 21 May 2003 Location: United States Status: Offline Points: 11234  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
   
   Roger  
   
   Newbie  
   Joined: 06 February 2006 Location: United Kingdom Status: Offline Points: 6  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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 
    | 
 |
![]()  | 
 |
   
   terrym  
   
   Senior Member  
   Joined: 13 April 2007 Status: Offline Points: 836  | 
  
   
      Post Options
    
        Thanks(0)
      Quote   Reply
   
     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  | 
 |
![]()  | 
 |
    Post Reply  
   | 
  |
|       
  
  Tweet   	
    | 
 
| Forum Jump | Forum Permissions  ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum  |