A Third Approach to Defining Menus

The LoadMenuIndirect function accepts a pointer to a structure of type MENUITEMTEMPLATE and returns a handle to a menu. This function is used within Windows to construct a menu after loading the normal menu template from a resource script. If you're brave, you can try using it yourself.

Be forewarned, however: The MENUITEMTEMPLATE structure has a field defined as LPSTR that is set to a far pointer to a character string, a handle to a popup, or a handle to a bitmap. But you can't simply define a MENUITEMTEMPLATE structure in your program and initialize the field to a character string. During compilation, the pointer to the character string is converted to a far pointer. This violates one of the most important rules discussed in Chapter 7: Don't store far pointers to your data segment. Instead, immediately before calling LoadMenuIndirect, you use a series of assignment statements to set this field to the character string pointers. Between these assignment statements and the LoadMenuIndirect call, you can't make any Windows calls that can result in your data segment being moved (such as GetMessage).