WM_COPUDATA Message

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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值