public void SendCommandLineArgs() { string[] args = Environment.GetCommandLineArgs(); if (hWndOther != IntPtr.Zero) { IntPtr buffer = IntPtr.Zero; IntPtr pcds = IntPtr.Zero; MemoryStream stream = null; try { ArgsPacket packet = new ArgsPacket(); packet.guid = guid; packet.args = args; stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, packet); byte[] abyte = stream.ToArray(); buffer = Marshal.AllocCoTaskMem(abyte.Length); Marshal.Copy(abyte, 0, buffer, abyte.Length); WindowsManagement.COPYDATASTRUCT cds = new WindowsManagement.COPYDATASTRUCT(); cds.dwData = COPYDATA_TYPE_FILENAME; cds.cbData = abyte.Length; cds.lpData = buffer; pcds = Marshal.AllocCoTaskMem(Marshal.SizeOf(cds)); Marshal.StructureToPtr(cds, pcds, true); WindowsManagement.SendMessage(hWndOther, (int) WindowsMessages.WM_COPYDATA, IntPtr.Zero, pcds); } catch { } finally { if (buffer != IntPtr.Zero) Marshal.FreeCoTaskMem(buffer); if (pcds != IntPtr.Zero) Marshal.FreeCoTaskMem(pcds); if (stream != null) stream.Close(); } } }
protected override void WndProc(ref Message m) { if (m.Msg == (int) WindowsMessages.WM_COPYDATA) { WindowsManagement.COPYDATASTRUCT cds = (WindowsManagement.COPYDATASTRUCT) Marshal.PtrToStructure( m.LParam, typeof(WindowsManagement.COPYDATASTRUCT)); if (cds.dwData == SingleInstance.COPYDATA_TYPE_FILENAME) { MemoryStream stream = null; try { byte[] abyte = new byte[cds.cbData]; Marshal.Copy(cds.lpData, abyte, 0, cds.cbData); stream = new MemoryStream(abyte); BinaryFormatter formatter = new BinaryFormatter(); ArgsPacket packet = (ArgsPacket) formatter.Deserialize(stream); if (packet.guid == si.guid) { if (si.ArgsRecieved != null) si.ArgsRecieved(form, packet.args); m.Result = (IntPtr) 1; } } catch { } finally { if (stream != null) stream.Close(); } } } else if (m.Msg == (int)si.UWM_ARE_YOU_ME) { m.Result = (IntPtr) si.UWM_ARE_YOU_ME; } else base.WndProc (ref m); }