This documentation is archived and is not being maintained.


Loads a resource from a menu template in memory and attaches it to the CMenu object.

BOOL LoadMenuIndirect(
   const void* lpMenuTemplate 


Points to a menu template (which is a single MENUITEMTEMPLATEHEADER structure and a collection of one or more MENUITEMTEMPLATE structures). For more information on these two structures, see the Platform SDK.

Return Value

Nonzero if the menu resource was loaded successfully; otherwise 0.


A menu template is a header followed by a collection of one or more MENUITEMTEMPLATE structures, each of which may contain one or more menu items and pop-up menus.

The version number should be 0.

The mtOption flags should include MF_END for the last item in a pop-up list and for the last item in the main list. See the AppendMenu member function for other flags. The mtId member must be omitted from the MENUITEMTEMPLATE structure when MF_POPUP is specified in mtOption.

The space allocated for the MENUITEMTEMPLATE structure must be large enough for mtString to contain the name of the menu item as a null-terminated string.

Before exiting, an application must free system resources associated with a menu if the menu is not assigned to a window. An application frees a menu by calling the DestroyMenu member function.


// CMainFrame::OnLoadMenuIndirect() is a menu command handler for 
// CMainFrame class, which in turn is a CFrameWnd-derived class. It 
// shows how to use LoadMenuIndirect() to load a resource from a 
// menu template in memory.
void CMainFrame::OnLoadMenuIndirect() 
   // For simplicity, allocate 500 bytes from stack. May use 
   // GlobalAlloc() to allocate memory bytes from heap.
   BYTE milist[500];
   memset(milist, 0, 500);

   // Fill up the MENUITEMTEMPLATEHEADER structure.
   mheader->versionNumber = 0;
   mheader->offset = 0;

   int bytes_used = sizeof(MENUITEMTEMPLATEHEADER);

   // Add the following menu items to menu bar:
   // File     Edit
   //   Exit     Copy
   //            Paste
   bytes_used += AddMenuItem(milist + bytes_used, L"&File", 0, TRUE, FALSE);
   bytes_used += AddMenuItem(milist + bytes_used, L"E&xit", ID_APP_EXIT, FALSE, TRUE);
   bytes_used += AddMenuItem(milist + bytes_used, L"&Edit", 0, TRUE, TRUE);
   bytes_used += AddMenuItem(milist + bytes_used, L"&Copy", ID_EDIT_COPY, FALSE, FALSE);
   bytes_used += AddMenuItem(milist + bytes_used, L"&Paste", ID_EDIT_PASTE, FALSE, TRUE);
   // Load resource from a menu template in memory.

   // Remove and destroy old menu

   // Add new menu.

   // Assign default menu
   m_hMenuDefault = m_NewMenu.m_hMenu;

// This is a helper function for adding a menu item (either a popup 
// or command item) to the specified menu template.
//    MenuTemplate - pointer to a menu template
//    MenuString   - string for the menu item to be added
//    MenuID       - id for the command item. Its value is ignored if 
                     IsPopup is TRUE.
//    IsPopup      - TRUE for popup menu (or submenu); FALSE for command 
//    LastItem     - TRUE if MenuString is the last item for the popup; 
                     FALSE otherwise.
UINT AddMenuItem(LPVOID MenuTemplate, WCHAR* MenuString, WORD MenuID, BOOL IsPopup, BOOL LastItem)

   UINT  bytes_used = 0;
   if (IsPopup)         // for popup menu
      if (LastItem)
         mitem->mtOption = MF_POPUP | MF_END;
         mitem->mtOption = MF_POPUP;
      bytes_used += sizeof (mitem->mtOption);  
      mitem = (MENUITEMTEMPLATE*) ((BYTE*) MenuTemplate + bytes_used);
      // a popup doesn't have mtID!!!
      wcscpy((WCHAR*) mitem, MenuString);
      bytes_used += sizeof (WCHAR) * (wcslen(MenuString) + 1); // include '\0'   
   else      // for command item
      mitem->mtOption = LastItem ? MF_END : 0;
      mitem->mtID = MenuID;   
      wcscpy(mitem->mtString, MenuString);  
      bytes_used += sizeof (mitem->mtOption ) + sizeof (mitem->mtID) + 
         sizeof (WCHAR) * (wcslen(MenuString) + 1);   // include '\0'

   return bytes_used;

See Also

CMenu Overview | Class Members | Hierarchy Chart | CMenu::DestroyMenu | CMenu::LoadMenu | LoadMenuIndirect | CMenu::AppendMenu