Example #1
0
        /// <summary>
        /// Sends the MSG.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <param name="desthandle">The desthandle.</param>
        /// <param name="sourcehandle">The sourcehandle.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static long SendMsg(string str, System.IntPtr desthandle, System.IntPtr sourcehandle, int type)
        {
            if ((desthandle == IntPtr.Zero) || (sourcehandle == IntPtr.Zero))
            {
                return(-1);                                                              // fail on invalid handles
            }
            WMUtil.COPYDATASTRUCT cds = new WMUtil.COPYDATASTRUCT();
            cds.dwData = type;
            str        = str + '\0';
            cds.cbData = str.Length + 1;

            System.IntPtr pData = Marshal.AllocCoTaskMem(str.Length);
            pData = Marshal.StringToCoTaskMemAnsi(str);

            cds.lpData = pData;

            IntPtr res = WMUtil.SendMessage(desthandle, WMUtil.WM_COPYDATA, (int)sourcehandle, ref cds);

            Marshal.FreeCoTaskMem(pData);
            return(res.ToInt64());
        }