Example #1
0
        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);
            }
        }