InsertMenuItem cutting off the string to first char
InsertMenuItem cutting off the string to first char
I am trying to modify window's system menu using InsertMenuItem (instead of InsertMenu - the reason is that I want to insert submenus eventually). Language is C#.
Tried inserting a new item, tried getting existing item and reinserting it, it inserts, but the item string is shown in the menu only as the first character. Anyone have a clue about what I am missing?
// try creating one from scratch
MENUITEMINFO it = new MENUITEMINFO();
it.cbSize = (uint)Marshal.SizeOf(it);
it.fMask = 64;// MIIM_STRING
it.wID = 12345;
it.dwTypeData = "Now is the time";
it.fType = 0;
it.cch = 0;
InsertMenuItem(hSysMenu, 7, true, ref it);
//try copying one
GetMenuItemInfo(hSysMenu, (uint)0, true, ref it);
it.cch += 10;
it.dwTypeData = new string(' ', (int)(menuItemInfo.cch+10));
GetMenuItemInfo(hSysMenu, (uint)0, true, ref it);
it.wID = 123456;
var err = InsertMenuItem(hSysMenu, 1, true, ref it);
The result of the above code
1 Answer
1
Ok I finally figured it out. I will leave this here so maybe someone else later will find it when they are baffled by this.
The reason was that the InsertMenuItem p/invoke was defined with
[DllImport("user32.dll")] in the pinvoke.net site - see https://www.pinvoke.net/default.aspx/user32/insertmenu.html?diff=y
and it needs to be
[DllImport("user32.dll", CharSet = CharSet.Auto) ]
I changed the pinvoke.net page to reflect it.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.