[ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
GuidAttribute("000214e4-0000-0000-c000-000000000046")]
public interface IContextMenu
{
[PreserveSig()]
int QueryContextMenu(
uint hMenu,
uint indexMenu,
int idCmdFirst,
int idCmdLast,
uint uFlags);
[PreserveSig()]
void InvokeCommand(IntPtr pici);
[PreserveSig()]
void GetCommandString(
int idcmd,
uint uflags,
int reserved,
StringBuilder commandstring,
int cch);
}
I found that GetCommandString gave me problems when defining the variable commandstring as a StringBuilder. (It had an unexplainable limit of 16 characters.) I found that by changing my definition to IntPtr, and then copying a
string into this pointer by using lstrcpy works for the true buffer size designated by cch.
You may have to explicitly call lstrcpynW or lstrcpynA based uFlags
void IContextMenu.GetCommandString(int idCmd, uint uFlags, int reserved, IntPtr commandString, int cchMax)
{
string commandStr = "" ;
if ((uFlags & (uint)GCS.VERB) != 0)
{
//obviously you could insert some cases here
switch (idCmd)
{
default:
commandStr = "...";
break;
}
}
if((uFlags & (uint)GCS.HELPTEXT) != 0)
{
switch (idCmd)
{
case 0:
commandStr = "Menu Item 1. And a brief description of what it does.";
break;
case 1:
commandStr = "Menu Item 2. And a brief description of what it does." ;
break;
case 2:
commandStr = "Menu Item 3. And a brief description of what it does.";
break;
case 3:
commandStr = "Menu Item 4. And a brief description of what it does.";
break;
default:
commandStr = "...";
break;
}
}
// must limit the return string to cchMax
if (commandStr.Length >= cchMax) commandStr = commandStr.Substring(0, cchMax-1) ;
// now return unicode or ansi based on uFlags
if((uFlags & (uint)GCS.UNICODE) != 0 )
{
str = Marshal.StringToHGlobalUni(commandStr);
Helpers.lstrcpynW(commandString, str, cchMax);
Marshal.FreeHGlobal(str);
}
else
{
str = Marshal.StringToHGlobalAnsi(commandStr);
Helpers.lstrcpynA(commandString, str, cchMax);
Marshal.FreeHGlobal(str);
}
}