GetMenuItemInfo MENUITEMINFO question

M

murl brown

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());
 
M

Mattias Sjögren

MENUITEMINFO mif = new MENUITEMINFO();
mif.cbSize = Marshal.SizeOf(typeof(MENUITEMINFO));
mif.fMask = MIIM_STRING;
mif.cch = 256;

You also have to initialize dwTypeData to a buffer big enough to hold
256 characters.

// 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);

An HWND isn't the same as an HMENU, which is what the first argument
should be.


Mattias
 
M

murl brown

I initialized the dwTypeData property with a char[256], and set the cch
= 256, but still no luck. If the click event being raised from my hook
is not giving me the HMENU, where am I supposed to get this value?
 
M

Mattias Sjögren

If the click event being raised from my hook
is not giving me the HMENU, where am I supposed to get this value?

What kind of hook is it you have in place and which click event is it
you're handing?


Mattias
 
M

murl brown

I am however, not getting back an error this time, the GetMenuItemInfo
method is returning false instead of true though, but atleast I'm not
getting the error 87 again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top