【C#】Send data between applications

This sample shows how to send data between different applications, including object data——transform object into byte[] and then transport its CPU location.

Now I'll paste the programs here.(Thanks the blogger Sir.jevan for the template he/she provide,what I have done is just make the object-transportation available.Here is his page.)

 

Sender:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Runtime.InteropServices;
11 using System.IO;  //The following four usings are important.So are they in Reciever and dll.
12 using System.Runtime.Serialization.Formatters.Binary;
13 using System.Runtime.Serialization;
14 using ClassLibrary1;
15 
16 namespace cdn_send
17 {
18     public partial class Form1 : Form
19     {
20         public Form1()
21         {
22             InitializeComponent();
23         }        
24 
25         private void Form1_Load(object sender, EventArgs e)
26         {
27         }
28 
29         private const int WM_COPYDATA = 0x004A;
30         private const uint flag = 0x8000; //I can't understand it.Please tell me(if you got it).
31         [DllImport("User32.dll", EntryPoint = "SendMessage")]
32         private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
33         [DllImport("User32.dll", EntryPoint = "FindWindow")]
34         private static extern int FindWindow(string lpClassName, string lpWindowName);
35         [DllImport("kernel32.dll")]
36         static extern uint GetTickCount();
37 
38         private void button1_Click(object sender, EventArgs e)
39         {
40             int WINDOW_HANDLER = FindWindow(null, @"xxx");  //Find target window.Well,by the way,it's called 'xxx'.
41             if (WINDOW_HANDLER == 0)
42             {
43                 MessageBox.Show("xxx");
44             }
45             else
46             {
47                 
48                 data dd=new data();  //Process data.
49                 dd.x = (int)this.Handle;
50                 dd.y = DateTime.Now;
51                 dd.tx = textBox1.Text;
52                 dd.tk = GetTickCount();
53                 byte[] bt=(new switcher()).Object2Bytes((object)dd);  //Type switch.
54 
55                 COPYDATASTRUCT cds;
56                 cds.dwData = (IntPtr)flag;
57                 cds.cbData = bt.Length;
58                 cds.lpData = Marshal.AllocHGlobal(bt.Length);  //Allocate space.
59 
60                 Marshal.Copy(bt, 0, cds.lpData, bt.Length);  //Memory copy.
61                 SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);  //Send message out.
62             }
63         }
64     }
65     
66 }

 

Reciever:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Runtime.InteropServices;
11 using System.IO;  //Important.
12 using System.Runtime.Serialization.Formatters.Binary;
13 using System.Runtime.Serialization;
14 using ClassLibrary1;
15 
16 namespace cdn_receiver
17 {
18     public partial class Form1 : Form
19     {
20 
21         public Form1()
22         {
23             InitializeComponent();
24         }
25         private void Form1_Load(object sender, EventArgs e)
26         {
27 
28         }
29 
30         const int WM_COPYDATA = 0x004A;
31         private const uint flag = 0x8000;
32         [DllImport("kernel32.dll")]
33         public static extern uint GetTickCount();
34 
35         protected override void DefWndProc(ref System.Windows.Forms.Message m)
36         {
37             switch (m.Msg)
38             {
39                 case WM_COPYDATA:
40 
41                     COPYDATASTRUCT cds = new COPYDATASTRUCT();
42                     cds = (COPYDATASTRUCT)m.GetLParam(cds.GetType()); //Receive information.
43 
44                     byte[] bt = new byte[cds.cbData];
45                     Marshal.Copy(cds.lpData,bt,0,bt.Length);  //Get data array.
46 
47                     data dd = (data)((new switcher()).Bytes2Object(bt));  //Transform back.
48                     long xx = GetTickCount() - dd.tk;  //This line is used to calculate its delay,although mostly it is 0ms.
49                     textBox1.Text = (dd.x.ToString() + " " + dd.y.ToString() + " " + dd.tx);
50                     textBox1.Text += "\r\n" + xx.ToString() + "ms";
51                     
52                     break;
53                 default:
54                     base.DefWndProc(ref m);  //Don't forget this line,or it cannot run properly.
55                     break;
56             }
57 
58         }
59     }
60     
61 
62 }

 

Dll:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Runtime.InteropServices;
 7 using System.Runtime;
 8 using System.IO; //Important
 9 using System.Runtime.Serialization;
10 using System.Runtime.Serialization.Formatters.Binary;
11 namespace ClassLibrary1
12 {
13         
14     [Serializable]  //My datastructure which contains different types.Don't forget this line.
15     public struct data  
16     {
17         public int x;
18         public DateTime y;
19         public string tx;
20         public long tk;
21     }
22 
23     [StructLayout(LayoutKind.Sequential)]  //The datastructure which used as a media to transport.
24     public struct COPYDATASTRUCT
25     {
26         public IntPtr dwData;
27         public int cbData;
28         public IntPtr lpData;
29     }
30 
31     public class switcher  //The switcher object which contains two main switcher function.
32     {
33         public byte[] Object2Bytes(object obj)
34         {
35             IFormatter fmt = new BinaryFormatter();
36             MemoryStream ms = new MemoryStream();
37             fmt.Serialize(ms, obj);
38             return ms.GetBuffer();
39 
40         }
41 
42         public object Bytes2Object(byte[] bt)
43         {
44             IFormatter fmt = new BinaryFormatter();
45             MemoryStream ms = new MemoryStream(bt);
46             return (object)fmt.Deserialize(ms);
47         }
48 
49         public switcher(){
50         }
51     }
52 
53 }

 

It is tested that there is no problem with the correction of the transported data.(My vs version is 2012 ultimate,OS version is win7)

 

posted @ 2016-09-15 23:42  Fefnir  阅读(264)  评论(0编辑  收藏  举报