WM_COPUDATA Message

来源:互联网 发布:scientific linux 7 编辑:程序博客网 时间:2024/05/22 06:43
2WM_COPYDATA Description2.1WM_COPYDATA DescriptionAn 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.2The Working Principal of WM_COPYDATAUse 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: 1Parameters: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 .2Return 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.3Use WM_COPYDATA Passing Data3.1Coding and ExplanationGive code from Sending application and receiving application. 3.2Sending 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 codeprivate 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.3Receiving 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;
原创粉丝点击