Microsoft KB Archive/229964

= FIX: AfxMessageBox Help Fails to Bring Up Proper Topic =

Article ID: 229964

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Professional Edition

 Microsoft Visual C++ 6.0 Standard Edition 

-

<div class="notice_section">

This article was previously published under Q229964

<div class="symptoms_section">

SYMPTOMS
When F1 is pressed to invoke context sensitive help in response to a call to AfxMessageBox, the default help topic is brought up instead of the help topic associated with the call to AfxMessageBox. This problem may be seen when MFC42.DLL is updated with a version greater than or equal to 6.0.8168.0.

<div class="cause_section">

CAUSE
The implementation of context sensitive help for AfxMessageBox was changed for Visual C++ version 6.0. A private message WM_HELPPROMPTADDR is sent to retrieve the address of m_dwPromptContext from the associated frame window. MFC uses an internal helper function CWnd::GetSafeOwner_, to determine which window to send this message to. MFC uses the value returned by the pWndTop parameter of CWnd::GetSafeOwner_ for the target window handle. The problem is that in most scenarios this value is NULL. The NULL value causes the call ::SendMessage(hWndTop, WM_HELPPROMPTADDR, 0, 0) to have no affect.

<div class="resolution_section">

RESOLUTION
One workaround for this problem is to override the virtual function CWinApp::DoMessageBox, and copy and paste the base class implementation. Next, change the SendMessage call to use the HWND returned from AfxGetMainWnd->GetSafeHwnd, for example:

...
 * 1) include "afxpriv.h"

int CHelpApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt) {   // disable windows for modal dialog EnableModeless(FALSE); HWND hWndTop; HWND hWnd = CWnd::GetSafeOwner_(NULL, &hWndTop);

// set help context if possible DWORD* pdwContext = NULL; HWND hWnd2 = AfxGetMainWnd->GetSafeHwnd; if (hWnd2 != NULL) {       // use app-level context or frame level context LRESULT lResult = ::SendMessage(hWnd2, WM_HELPPROMPTADDR, 0, 0); // Use "MainWnd" HWND if (lResult != 0) pdwContext = (DWORD*)lResult; }   // for backward compatibility use app context if possible if (pdwContext == NULL && this != NULL) pdwContext = &m_dwPromptContext;

DWORD dwOldPromptContext = 0; if (pdwContext != NULL) {       // save old prompt context for restoration later dwOldPromptContext = *pdwContext; if (nIDPrompt != 0) *pdwContext = HID_BASE_PROMPT+nIDPrompt; }

// determine icon based on type specified if ((nType & MB_ICONMASK) == 0) {       switch (nType & MB_TYPEMASK) {       case MB_OK: case MB_OKCANCEL: nType |= MB_ICONEXCLAMATION; break;

case MB_YESNO: case MB_YESNOCANCEL: nType |= MB_ICONEXCLAMATION; break;

case MB_ABORTRETRYIGNORE: case MB_RETRYCANCEL: // No default icon for these types, since they are rarely used. // The caller should specify the icon. break; }   }

if ((nType & MB_ICONMASK) == 0) TRACE0("Warning: no icon specified for message box.\n");
 * 1) ifdef _DEBUG
 * 1) endif

TCHAR szAppName[_MAX_PATH]; LPCTSTR pszAppName; if (this != NULL) pszAppName = m_pszAppName; else {       pszAppName = szAppName; GetModuleFileName(NULL, szAppName, _MAX_PATH); }

int nResult = ::MessageBox(hWnd, lpszPrompt, pszAppName, nType);

// restore prompt context if possible if (pdwContext != NULL) *pdwContext = dwOldPromptContext;

// re-enable windows if (hWndTop != NULL) ::EnableWindow(hWndTop, TRUE); EnableModeless(TRUE);

return nResult; }

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.

<div class="moreinformation_section">

Steps to Reproduce Behavior

 * 1) Create an AppWizard MFC SDI application with context sensitive help support.
 * 2) Add a menu item called Message Box Help.
 * 3) In the handler for the Message Box Help menu item add the following call: AfxMessageBox("Message Box Help", MB_OK, IDD_ABOUTBOX-0x10000);.
 * 4) Compile and run the program.
 * 5) Invoke Message Box Help menu item.
 * 6) When the message box is displayed, press F1.NOTE: The default help topic is displayed - not the help topic for the About box.

<div class="references_section">