INFO: Reasons for Failure of Menu Functions

ID: Q89739

The information in this article applies to:

SUMMARY

The Menu functions (AppendMenu(), CheckMenuItem(), CreateMenu(), CreatePopupMenu(), DeleteMenu(), DestroyMenu(), GetMenu(), GetMenuItemID(), GetMenuString(), GetSubMenu(), GetSystemMenu(), HiliteMenuItem(), InsertMenu(), LoadMenuIndirect(), ModifyMenu(), RemoveMenu(), SetMenu(), SetMenuItemBitmaps(), and TrackPopupMenu()) can fail for several reasons. Different functions return different values to indicate failure. Read the documentation for information about each function. This article combines the causes of failure for all functions and provides a resolution or explanation. A list of affected functions follows each cause. The causes are:

1. Invalid hWnd parameter.

2. Invalid hMenu parameter.

3. The menu item is not found.

4. No space left in User's heap to hold a string or to hold an

   internal data structure for owner draw menu items or to create a menu
   or to create a window for TrackPopupMenu().

5. There are no items in the menu.

6. The menu resource could not be found (FindResource()) or loaded

   (LoadResource()) or locked (LockResource()) in memory.

7. TrackPopupMenu() is called while another popup menu is being tracked
   in the system.

8. The hMenu that has been passed to TrackPopupMenu() has been deleted.

9. MENUITEMTEMPLATEHEADER's versionNumber field is non-zero.

MORE INFORMATION

Cause 1: Invalid hWnd parameter.

Resolution 1: Validate the hWnd parameter using IsWindow(). Make sure that hWnd is not a child window.

NOTE: Resolution 1 does not apply to TrackPoupuMenu().

Explanation 1: In Windows, menus are always associated with a window. Child windows cannot have menu bars.

Affected Functions: All functions that take hWnd as a parameter except for TrackPoupuMenu().

Cause 2: Invalid hMenu parameter.

Resolution 2: Validate hMenu with IsMenu().

Affected Functions: All functions that take hMenu as a parameter.

Cause 3: The menu item is not found.

Resolution 3: If the menu item is referred to BY_POSITION, make sure that the index is lesser than the number of items. If the menu item is referred to BY_COMMAND, an application has to devise its own method of validating it.

Explanation 3: Menu items are numbered consecutively starting from 0. Remember that separator items are also counted.

Affected Functions: All functions that refer to a menu item.

Cause 4: No space left in User's heap to hold a string or to hold an internal data structure for owner draw menu items or to create a menu.

Resolution 4: Remember to delete all menus and other objects that have been created by the application when they are not needed any more. If you suspect that objects left undeleted by other applications are wasting valuable system resources, restart Windows.

Explanation 4: In Windows 3.0, menus and menu items were allocated space from User's heap. In Windows 3.1, they are allocated space from a separate heap. This heap is for the exclusive use of menus and menu items.

Affected Functions: AppendMenu(), Insertmenu(), ModifyMenu(), CreateMenu(), CreatePopupMenu(), LoadMenu(), LoadMenuIndirect(), TrackPopupMenu(), GetSystemMenu() (when fRevert = FALSE).

Cause 5: There are no items in the menu.

Resolution 5: Use GetMenuItemCount() to make sure the menu is not empty.

Explanation 5: Nothing to be deleted or removed.

Affected Functions : RemoveMenu(), DeleteMenu().

Cause 6: The menu resource could not be found (FindResource()) or loaded (LoadResource()) or locked (LockResource()) in memory.

Resolution 6: Ensure that the menu resource exists and that the hInst parameter refers to the correct hInstance. Try increasing the number of file handles using SetHandleCount() and increasing available global memory by closing some applications. For more information about the causes of failure of resource functions, query this Knowledge Base on the following keywords:

   failure and LoadResource and FindResource and LockResource.

Explanation 6: Finding, loading, and locking a resource involves use of file handles, global memory, and the hInstance that has the menu resource.

Affected Functions: LoadMenu(), LoadMenuIndirect()

Cause 7. TrackPopupMenu() is called while another popup menu is being tracked in the system.

Explanation 7: Only one popup menu can be tracked in the system at any given time.

Affected Function: TrackPopupMenu()

Cause 8. The hMenu that has been passed to TrackPopupMenu() has been deleted. The debug mode of Windows 3.1 sends the following message :

"Menu destroyed unexpectedly by WM_INITMENUPOPUP"

Explanation 8: Windows sends a WM_INITMENUPOPUP to the application and expects the menu to not be destroyed.

Affected Function: TrackPopupMenu()

Cause 9. MENUITEMTEMPLATEHEADER 's versionNumber field is non-zero.

Explanation 9: In Windows 3.0 and 3.1, this field should always be 0.

Affected Function: LoadMenuIndirect()

Additional query words:

Keywords          : kbMenu kbNTOS kbResource kbGrpUser kbWinOS 
Issue type        : kbinfo

Last Reviewed: December 26, 1998