The following implementation taken from this list some time ago no longer seems to work in VS2012 for a dynamic command. Works fine in VS2010/2008/2005.
public static void SetText(IntPtr pCmdTextInt, string text) { Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT pCmdText = (Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT)Marshal.PtrToStructure(pCmdTextInt, typeof(Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT)); var menuText = text.ToCharArray(); // Get the offset to the rgsz param. This is where we will stuff our text // IntPtr offset = Marshal.OffsetOf(typeof(Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT), "rgwz"); IntPtr offsetToCwActual = Marshal.OffsetOf(typeof(Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT), "cwActual"); // The max chars we copy is our string, or one less than the buffer size, // since we need a null at the end. // int maxChars = Math.Min((int)pCmdText.cwBuf - 1, menuText.Length); Marshal.Copy(menuText, 0, (IntPtr)((long)pCmdTextInt + (long)offset), maxChars); // append a null character Marshal.WriteInt16((IntPtr)((long)pCmdTextInt + (long)offset + maxChars * 2), 0); // write out the length // +1 for the null char Marshal.WriteInt32((IntPtr)((long)pCmdTextInt + (long)offsetToCwActual), maxChars + 1); }
The declaration of the command in the .vsct file is as follows:
<Menu guid="guidMyCmdSet" id="IDMX_MY_MENU" priority="0x0150" type="Menu"><Parent guid="guidMyCmdSet" id="IDG_CTX_MY_NODE"/><CommandFlag>DefaultInvisible</CommandFlag><CommandFlag>DynamicVisibility</CommandFlag><Strings><ButtonText>MyButtonText</ButtonText><CommandName>MyCommandName</CommandName></Strings></Menu>