2 WM_COPYDATA Description 2.1 WM_COPYDATA Description An application sends the WM_COPYDATA message to pass data to another application. You can only use SendMessage to send data using WM_COPYDATA. The Receiving application should consider the received data as read only and copy that data in there local Buffer before using/modifying it. 2.2 The Working Principal of WM_COPYDATA Use SendMessage to send COPYDATASTRUCT which the two or more application send and receive data. SendMessage: To send WM_COPYDATA message, call the SendMessage function as follow: 1 Parameters: a) hWndControl: handle to the window receiving the data . b) wParam: handle to the window .passing the data. c) lParam: pointer to COPYDATASTRUCT structure which contains the data to be passed . 2 Return Value: If the receiving application processes this message, it should return true; otherwise it return false. The data being passed must not contain pointers or other references to objects not accessibly to the application receiving the data. While this message is being sent, the referenced data must not be changed by another thread of sending process. The received application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free memory referenced by the lParam. If the receiving application must access after SetMessage return, COPYDATASTRUCT: The COPYDATASTRUCT structure contains data to be passed to another application by the WM_COPYDATA. 1. dwData: specifies data to be passed to the receiving application. 2. cbData: specifies the size, in bytes, of the data pointer to by the lpData member. 3. lpData: point to data to be passed to the receiving application. the member can be null. 3 Use WM_COPYDATA Passing Data 3.1 Coding and Explanation Give code from Sending application and receiving application. 3.2 Sending Application [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public int dwData; public int cbData; [MarshalAs(UnmanagedType.LPWStr)] public string lpData; } [DllImport("user32", EntryPoint = "SendMessageA")] public static extern int SendMessage(int Hwnd, int wMsg, int wParam, ref COPYDATASTRUCT lParam); const int WM_COPYDATA = 0x004A; //sending application code private void SendMessageToProcess() { string message =”send message test”; COPYDATASTRUCT copydata = new COPYDATASTRUCT(); copydata.cbData = message.Length * UnicodeEncoding.CharSize + 1; // specifies the size, in bytes of the data reference to message. copydata.lpData = message; int handle = FindWindow(null, “HandName”); if (handle == 0) { handle = StartProcess(); //start process } int ret = SendMessage(handle, WM_COPYDATA, 0, ref copydata); } 3.3 Receiving Application ////// over ride defwnd proc /// /// protected override void DefWndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case WM_COPYDATA: COPYDATASTRUCT mystr = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT)); SendMailMessage(mystr.lpData); break; default: base.DefWndProc(ref m); break; } } [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPWStr)] public string lpData; } private const int WM_COPYDATA = 0x004A;
WM_COPUDATA Message
最新推荐文章于 2021-10-24 13:02:39 发布