Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
So, I'm currently using this to retrieve a menu as a list of strings and return it back to the main program. Please note that "Win32" is just a separate class where I coded some of the main Win32 calls.

C#
List<string> ls = new List<string>();
            IntPtr hMenu = Win32.GetMenu(hWnd);
     
                if (hMenu.ToInt32() != 0)
                {

                    for (int i = Win32.GetMenuItemCount(hMenu); i >= 0; i--)
                    {
                        uint MIIM_STRING = 0x00000040;
                        uint MFT_STRING  = 0x00000000;
                        Win32.MENUITEMINFO mif = new Win32.MENUITEMINFO();
                        mif.cbSize = (uint)Marshal.SizeOf(typeof(Win32.MENUITEMINFO));
                        mif.fMask = MIIM_STRING;
                        mif.fType = MFT_STRING;
                        mif.dwTypeData = null;
                        bool a = Win32.GetMenuItemInfo(hMenu, 0, true, ref mif);
                        ls.Add(mif.dwTypeData);
                    }
                }
       
            return ls;


Yet, whenever I run the program, dwTypeData still returns null. I know my declarations and syntax is correct because GetMenuItemInfo returns true. There must be something I'm missing... any ideas?
Posted
Comments
Sergey Alexandrovich Kryukov 7-Jun-13 19:47pm    
Why P/Invoke? .NET FCL provides you will all you need to work with menus? Isn't it yet another kind of abuse, like working with a menu of a different process?
—SA
RadXPictures 7-Jun-13 20:00pm    
I don't care... I just need to know how this is done. ASAP.
charles henington 8-Jun-13 23:22pm    
try google
RadXPictures 8-Jun-13 23:42pm    
Do you really think if I hadn't googled already I would be posting on here? I've searched everywhere. This was my last resort. I need help guys.

1 solution

You need to initialize dwTypeData and cch data members...
C#
List<string> ls = new List<string>();
IntPtr hMenu = Win32.GetMenu(hWnd);

    if (hMenu.ToInt32() != 0)
    {
        char[] szString = new char[256];
        for (int i = Win32.GetMenuItemCount(hMenu); i >= 0; i--)
        {
            uint MIIM_STRING = 0x00000040;
            uint MFT_STRING  = 0x00000000;
            Win32.MENUITEMINFO mif = new Win32.MENUITEMINFO();
            mif.cbSize = (uint)Marshal.SizeOf(typeof(Win32.MENUITEMINFO));
            mif.fMask = MIIM_STRING;
            mif.fType = MFT_STRING;
            mif.cch = 256;
            mif.dwTypeData = szString;
           
            bool a = Win32.GetMenuItemInfo(hMenu, 0, true, ref mif);
            ls.Add(mif.dwTypeData);
        }
    }

return ls;
 
Share this answer
 
v2
Comments
RadXPictures 9-Jun-13 18:10pm    
I just tried this code, and it works! Thanks. GetMenuItemInfo returns true. My only concern is, instead of retrieving the actual string value of the menu item, dwTypeData always returns "System.Char[]". The only modification I had to make to your code was adding a ".ToString()" after your szString assignment to dwTypeData, because according to my struct declaration, dwTypeData is type "string" instead of "char[]". Would this have caused a problem? I based my original post off some PInvoke.Net sample code (http://pinvoke.net/default.aspx/user32/GetMenuItemInfo.html) if it's any help.
Kuthuparakkal 9-Jun-13 19:05pm    
Change struct member to char array and try!
RadXPictures 9-Jun-13 21:24pm    
I got the following error:

not of the expected type.
at System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, CleanupWorkList& pCleanupWorkList)
at Win32.Win32.GetMenuItemInfo(IntPtr hMenu, UInt32 uItem, Boolean fByPosition, MENUITEMINFO& lpmii)
at Win32.Menus.getMenuItems(IntPtr hWnd) in E:\My Documents\Visual Studio 2010\Projects\NewShell\Win32\Menus.cs:line 35System.Runtime.InteropServices.SafeArrayTypeMismatchException: Specified array was not of the expected type.
at System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, CleanupWorkList& pCleanupWorkList)
at Win32.Win32.GetMenuItemInfo(IntPtr hMenu, UInt32 uItem, Boolean fByPosition, MENUITEMINFO& lpmii)
at Win32.Menus.getMenuItems(IntPtr hWnd) in E:\My Documents\Visual Studio 2010\Projects\NewShell\Win32\Menus.cs:line 35System.Runtime.InteropServices.SafeArrayTypeMismatchException: Specified array was not of the expected type.
at System.StubHelpers.ValueClassMarshaler.ConvertToA first chance exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in Win32.dll
Native(IntPtr dst, IntPtr src, IntPtr pMT, CleanupWorkList& pCleanupWorkList)
at Win32.Win32.GetMenuItemInfo(IntPtr hMenu, UInt32 uItem, Boolean fByPosition, MENUITEMINFO& lpmii)
at Win32.Menus.getMenuItems(IntPtr hWnd) in E:\My Documents\Visual Studio 2010\Projects\NewShell\Win32\Menus.cs:line 35System.Runtime.InteropServices.SafeArrayTypeMismatchException: Specified array was not of the expected type.
at System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, CleanupWorkList& pCleanupWorkList)
at Win32.Win32.GetMenuItemInfo(IntPtr hMenu, UInt32 uItem, Boolean fByPosition, MENUITEMINFO& lpmii)
at Win32.Menus.getMenuItems(IntPtr hWnd) in E:\My Documents\Visual Studio 2010\Projects\NewShell\Win32\Menus.cs:line 35The thread '<no name="">' (0x1a30) has exited with code 0 (0x0).

I think it has to do with the fact that GetMenuItemInfo is expecting a string for dwTypeData, not a char[]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900