GetMenuStringW function (winuser.h)

Copies the text string of the specified menu item into the specified buffer.

Note  The GetMenuString function has been superseded. Use the GetMenuItemInfo function to retrieve the menu item text.
 

Syntax

int GetMenuStringW(
  [in]            HMENU  hMenu,
  [in]            UINT   uIDItem,
  [out, optional] LPWSTR lpString,
  [in]            int    cchMax,
  [in]            UINT   flags
);

Parameters

[in] hMenu

Type: HMENU

A handle to the menu.

[in] uIDItem

Type: UINT

The menu item to be changed, as determined by the uFlag parameter.

[out, optional] lpString

Type: LPTSTR

The buffer that receives the null-terminated string. If the string is as long or longer than lpString, the string is truncated and the terminating null character is added. If lpString is NULL, the function returns the length of the menu string.

[in] cchMax

Type: int

The maximum length, in characters, of the string to be copied. If the string is longer than the maximum specified in the nMaxCount parameter, the extra characters are truncated. If nMaxCount is 0, the function returns the length of the menu string.

[in] flags

Type: UINT

Indicates how the uIDItem parameter is interpreted. This parameter must be one of the following values.

Value Meaning
MF_BYCOMMAND
0x00000000L
Indicates that uIDItem gives the identifier of the menu item. If neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified, the MF_BYCOMMAND flag is the default flag.
MF_BYPOSITION
0x00000400L
Indicates that uIDItem gives the zero-based relative position of the menu item.

Return value

Type: int

If the function succeeds, the return value specifies the number of characters copied to the buffer, not including the terminating null character.

If the function fails, the return value is zero.

If the specified item is not of type MIIM_STRING or MFT_STRING, then the return value is zero.

Remarks

The nMaxCount parameter must be one larger than the number of characters in the text string to accommodate the terminating null character.

If nMaxCount is 0, the function returns the length of the menu string.

Security Warning

The lpString parameter is a TCHAR buffer, and nMaxCount is the length of the menu string in characters. Sizing these parameters incorrectly can cause truncation of the string, leading to possible loss of data.

Examples

For an example, see Creating User Editable Accelerators.

Note

The winuser.h header defines GetMenuString as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.

Requirements

Requirement Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header winuser.h (include Windows.h)
Library User32.lib
DLL User32.dll
API set ext-ms-win-ntuser-menu-l1-1-3 (introduced in Windows 10, version 10.0.14393)

See also

Conceptual

GetMenuItemID

Menus

Reference