You have to set the fMask
member to specify which data to retrieve, MIIM_STRING | MIIM_ID
(&H40 Or &H2
) to retrieve the text and ID members.
Retrieving the text member requires two steps: Retrieve the text length to allocate a String
with appropriate length, followed by actually retrieving the text:
mii = New MenuItemInfo
mii.cbSize = Marshal.SizeOf(GetType(MenuItemInfo))
mii.dwTypeData = Nothing
mii.fMask = Mask.TEXT ' &H40
' Retrieve the text length member
GetMenuItemInfo(MenuHandle, m.wParam, True, mii)
' Account for terminating NUL character and allocate String
mii.cch += 1
mii.dwTypeData = Space(mii.cch)
' Specify MIIM_STRING | MIIM_ID to retrieve both text and ID
mii.fMask = Mask.TEXT Or Mask.ID ' &H40 Or &H2
' Retrieve data
GetMenuItemInfo(MenuHandle, m.wParam, True, mii)
Note that you have to allocate a string with the appropriate size and specify the buffer size in mii.cch
. The code above uses the Strings.Space Method that returns a string consisting of the specified number of spaces. This is for convenience only, you could have used a StringBuilder
as well.
If you are having problems with character encodings (text that looks like chinese characters) be specific when importing the Windows API function. Most Windows APIs come in two flavors: ANSI and UNICODE with a A
and W
postfix, respectively. In this case you should specify GetMenuItemInfoW
.