public static long SendMsg(string str, System.IntPtr desthandle, System.IntPtr sourcehandle, int type, int timeout) { if ((desthandle == IntPtr.Zero) || (sourcehandle == IntPtr.Zero)) { return(-1); // fail on invalid handles } WMUtil.COPYDATASTRUCT cds = new WMUtil.COPYDATASTRUCT(); cds.dwData = (IntPtr)type; str = str + '\0'; cds.cbData = str.Length + 1; System.IntPtr pData = Marshal.StringToCoTaskMemAnsi(str); cds.lpData = pData; IntPtr res = IntPtr.Zero; IntPtr err = WMUtil.SendMessageTimeout(desthandle, (uint)WM_COPYDATA, (IntPtr)sourcehandle, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, (uint)timeout, out res); if (err == IntPtr.Zero) { return(0); } Marshal.FreeCoTaskMem(pData); return(res.ToInt64()); }