This article shows the method of WinForm to realize cross-process communication and is shared with you for your reference. The specific methods are as follows:
The main function codes are as follows:
public class WinMessageHelper { private struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs()] public string lpData; } //COPYDATA for cross-process communication public const int WM_COPYDATA = 0x004A; [DllImport("", EntryPoint = "SendMessage")] private static extern int SendMessage( int hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter ref COPYDATASTRUCT lParam // second message parameter ); [DllImport("", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); /// <summary> /// Send a message /// </summary> /// <param name="windowReceiveTitle">Receiver form title name</param> /// <param name="strData">Data to be sent</param> public static void Send(string windowReceiveTitle, string strData) { int winHandler = FindWindow(null,windowReceiveTitle); if (winHandler != 0) { byte[] sarr = (strData); int len = + 1; COPYDATASTRUCT cds; = (IntPtr)100; = strData; = len; SendMessage(winHandler, WM_COPYDATA, 0, ref cds); } } /// <summary> /// Receive message /// </summary> /// <example> /// Overwrite the receiving message function in the form /// protected override void DefWndProc(ref m) /// { /// switch() /// { /// case WinMessageHelper.WM_COPYDATA: /// string str = (ref m); /// break; /// default: /// (ref m); /// break; /// /// } /// } /// </example> /// <returns>Received data</returns> public static string Receive(ref m) { COPYDATASTRUCT cds = new COPYDATASTRUCT(); Type cdsType = (); cds = (COPYDATASTRUCT)(cdsType); return ; } }
I hope that the examples described in this article will be helpful to everyone's C# programming.