title: Should a CDialog based app set AfxGetApp()->m_pMainWnd
body: <p><strong>EDIT:</strong></p>
  <p>I Need to Research some weird stuff first, is there some way to put the question &quot;on hold&quot;?</p>
  <p><strong>Original:</strong></p>
  <p>I am working on an existing codebase using a CDialog based GUI. The Application consists of a CDialog &quot;MainWindow&quot;, which spawns other CDialog &quot;SubWindow&quot;s using CDialog.DoModal.
  This does work, when showing the SubWindow, the MainWindow blocks, etc.</p>
  <p>When we call AfxMessageBox from &quot;SubWindow&quot;, the MainWindow gets reenabled and focused.</p>
  <p>Debugging into AfxMessagebox shows, that the function gets the mainWindow and reenables it. This causes a lot of different bugs. Using ::MesageBox works correctly, but we have about 50 different SubWindows, and, if possible, i would like to make only small, localized changes.</p>
  <p>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\src\mfc\appui1.cpp</p>
  <pre><code>int CWinApp::ShowAppMessageBox(CWinApp *pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
  ...
  HWND hWndTop;
  HWND hWnd = CWnd::GetSafeOwner_(NULL, &amp;hWndTop);
  
  // re-enable the parent window, so that focus is restored 
  // correctly when the dialog is dismissed.
  if (hWnd != hWndTop)
      EnableWindow(hWnd, TRUE);
  ...
  </code></pre>
  <p>In our entry Point we do something like this:</p>
  <pre><code>::AfxGetApp()-&gt;m_pMainWnd = &amp;mainDlg;
  mainDlg.DoModal();
  </code></pre>
  <p>What is the prefered way? Should i comment the line so the member stays NULL?</p>
  <p>or could that cause any side effects?</p>
  <p>I guess (not yet tested) i could also set</p>
  <pre><code>AfxGetApp()-&gt;m_pMainWnd = &amp;subDlg;
  </code></pre>
  <p>prior to subDlg.DoModal() and reset it afterwards, but that would also mean changing 50 different files, one for every SubWindow.</p>
  <p>Does MFC depend on m_pMainWnd, or should i just let it stay at NULL?
  Thanks.</p>
  <p><strong>EDIT:</strong></p>
  <p>I tried passing the MainWindow to the SubWindow's constructor, but to no avail.</p>
  <p>This is, where MainWindow gets reenabled:
  <img src="https://i.stack.imgur.com/aucS7.png" alt="enter image description here" /></p>
  <p>This is, where MFC finds the MainWindow:
  <img src="https://i.stack.imgur.com/6F46s.png" alt="enter image description here" />
  Do i need to manually set m_pActiveWnd ?</p>
  <p>(OFF Topic: I love that there is the source code for mfc available.)</p>
  <p><strong>EDIT 2:</strong></p>
  <p>The MFC App is actually a DLL, which can be invoked in two ways:
  Either loaded by a simple loader.exe, or by anyther big application.
  This other application may also use MFC, so there may be two different CWinApp objects.</p>
  <p>If it is loaded by loader.exe, the error does not occur.</p>