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
enter image description here




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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)