I'm trying to get the text of a menu item in a context/popupmenu in an
external application. I have a simple mouse hook catching the right
click, and then the left click on the context menu that was opened by
the application, however the GetMenuItemInfo() method is returning an
error code 87, incorrect parameter msg. I know I am probably not
initializing my struct correctly, but I can't find any documentation on
it. Below is some sample code:
// GetMenuItemInfo
[DllImport("user32.dll")]
static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, bool
fByPosition,ref MENUITEMINFO lpmii);
[StructLayout(LayoutKind.Sequential)]
internal struct MENUITEMINFO
{
public int cbSize;
public int fMask;
public int fType;
public int fState;
public int wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public string dwTypeData;
public IntPtr dwItemData;
public int cch;
public IntPtr hbmpItem;
}
// MenuItem Contstants
public const int MIIM_FTYPE = 0x100;
public const int MIIM_STRING = 0x40;
MENUITEMINFO mif = new MENUITEMINFO();
mif.cbSize = Marshal.SizeOf(typeof(MENUITEMINFO));
mif.fMask = MIIM_STRING;
mif.cch = 256;
// e.hookStruct.hwnd is the handle of the context menu that was opened.
// GetMenuItemInfo always returns false
bool retrieved = GetMenuItemInfo(e.hookStruct.hwnd,4,true,ref mif);
Console.WriteLine("Menu Item Retrieved = {0}",retrieved);
if ( !retrieved )
Console.WriteLine("Error recieved:
{0}",Marshal.GetLastWin32Error());