为什么我的GetMenuItemInfo调用不起作用?

为什么我的GetMenuItemInfo调用不起作用?

问题描述:

所以,我正在使用它来检索菜单作为字符串列表并将其返回到主程序。请注意,Win32只是一个单独的类,我编写了一些主要的Win32调用。



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.

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;





然而,每当我运行程序时,dwTypeData仍然返回null。我知道我的声明和语法是正确的,因为GetMenuItemInfo返回true。必须有一些我缺少的东西......任何想法?



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?

你需要初始化dwTypeData和cch数据成员...

You need to initialize dwTypeData and cch data members...
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;