public static void SetCommandText(IntPtr pCmdTextInt, string text) { if (text != null) { OLECMDTEXT olecmdtext = (OLECMDTEXT)Marshal.PtrToStructure(pCmdTextInt, typeof(OLECMDTEXT)); if ((olecmdtext.cmdtextf & (uint)OLECMDTEXTF.OLECMDTEXTF_NAME) == 0) { return; } char[] source = text.ToCharArray(); IntPtr bufferOffset = Marshal.OffsetOf(typeof(OLECMDTEXT), "rgwz"); IntPtr lengthOffset = Marshal.OffsetOf(typeof(OLECMDTEXT), "cwActual"); int length = Math.Min(((int)olecmdtext.cwBuf) - 1, source.Length); // copy the new text long bufferAddress = (long)pCmdTextInt + (long)bufferOffset; Marshal.Copy(source, 0, (IntPtr)bufferAddress, length); // null terminator Marshal.WriteInt16(pCmdTextInt, (int)bufferOffset + (length * 2), 0); // length including null terminator Marshal.WriteInt32(pCmdTextInt, (int)lengthOffset, length + 1); } }
/// <summary> /// Gets the flags of the OLECMDTEXT structure /// </summary> /// <param name="pCmdTextInt">The structure to read.</param> /// <returns>The value of the flags.</returns> public static OLECMDTEXTF GetFlags(IntPtr pCmdTextInt) { Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT pCmdText = (Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT)Marshal.PtrToStructure(pCmdTextInt, typeof(Microsoft.VisualStudio.OLE.Interop.OLECMDTEXT)); if ((pCmdText.cmdtextf & (int)OLECMDTEXTF.OLECMDTEXTF_NAME) != 0) { return(OLECMDTEXTF.OLECMDTEXTF_NAME); } if ((pCmdText.cmdtextf & (int)OLECMDTEXTF.OLECMDTEXTF_STATUS) != 0) { return(OLECMDTEXTF.OLECMDTEXTF_STATUS); } return(OLECMDTEXTF.OLECMDTEXTF_NONE); }
private static string GetCommandText(IntPtr structPtr) { if (structPtr == IntPtr.Zero) { return(string.Empty); } OLECMDTEXT olecmdtext = (OLECMDTEXT)Marshal.PtrToStructure(structPtr, typeof(OLECMDTEXT)); if (olecmdtext.cwActual == 0) { return(string.Empty); } IntPtr offset = Marshal.OffsetOf(typeof(OLECMDTEXT), "rgwz"); IntPtr ptr = (IntPtr)((long)structPtr + (long)offset); return(Marshal.PtrToStringUni(ptr, (int)olecmdtext.cwActual - 1)); }
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)); char[] 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); }
/// <summary> /// Initializes a new instance of the <see cref="OleCommandText"/> class from the specified pointer. /// </summary> /// <param name="oleCmdText">A pointer to the <see cref="OLECMDTEXT"/> structure in unmanaged memory.</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="oleCmdText"/> is <see cref="IntPtr.Zero"/>. /// </exception> private unsafe OleCommandText(OLECMDTEXT* oleCmdText) { Contract.Requires<ArgumentNullException>(oleCmdText != null, "oleCmdText"); _oleCmdText = oleCmdText; }