【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)

时间: 2024-10-15 18:48:04

【C#】Send data between applications的相关文章

【学术报告】【2015-05-08】Big Data:A Practitioner's Perspective

应本科母校数计学院院长陈明玉老师的邀请,下周五(2015年5月8日),将回泉州师范学院作一个学术报告,对大数据相关的技术.应用.八卦作一个相对全面和完整的介绍. 希望感兴趣的老师和同学们参加.以下是相关poster. [学术报告][2015-05-08]Big Data:A Practitioner's Perspective

【DataGuard】部署Data Guard相关参数详解 (转载)

原文地址:[DataGuard]部署Data Guard相关参数详解 作者:secooler 有关物理Data Guard部署参考<[DataGuard]同一台主机实现物理Data Guard配置安装>(http://space.itpub.net/519536/viewspace-578181),本文对部署Data Guard过程中主备库使用到的参数进行比较描述. 1.DB_NAME,数据库名字,需要保持同一个Data Guard 中所有数据库DB_NAME相同primary端和standb

【CF802L】Send the Fool Further! (hard) 高斯消元

[CF802L]Send the Fool Further! (hard) 题意:给你一棵n个节点的树,每条边有长度,从1号点开始,每次随机选择一个相邻的点走,走到一个叶子时就停止,问期望走的总路程. $n\le 10^5$ 题解:很自然想到游走那题,于是想到高斯消元,但是正常高斯消元是$O(n^3)$的.不过我们有一个套路:在树上进行高斯消元的复杂度是$O(n)$的. 先列出方程:设f(x)表示从x开始期望还要走的路程,x的度数是d,那么$f(x)=\frac {f(fa)+len} d+\f

【DataStructure】Linked Data Structures

Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. But if you want to maintain an ordered list that allows quick insertions and deletions, you should use a linked data structure. so inserting an elemen

【转】form data和request payload的区别

HTML <form> 标签的 enctype 属性 在下面的例子中,表单数据会在未编码的情况下进行发送: <form action="form_action.asp" enctype="text/plain">   <p>First name: <input type="text" name="fname" /></p>   <p>Last name: 

【转】System.Data.OracleClient requires Oracle client software version 8.1.7 or greater

安装完ASP.NET,Oracle9i客户端后,使用System.Data.OracleClient访问Oracle数据库如果出现这种错误:System.Data.OracleClient requires Oracle client software version 8.1.7 or greater. 原因Oracle 9i Release 2 客户端在安装到Windows的NTFS分区下时的安全认证设置不正确,引起本机的Authenticated Users用户无法看到ORACLE_HOME

【ArcGIS】ArcGIS Data Store配置

一.错误提示 Unable to configure the ArcGIS Data Store with the GIS Server. Please make sure that the GIS Server URL is accessible, the account specified has administrative privileges to the site, and the publishing tools is started on the GIS Server. 二.解决

【原创】Spring Data Redis &lt;=2.0.3反序列化漏洞

Spring Data Redis隶属于Spring Data家族, 提供简单易用的方式来访问Redis缓存. Spring Data Redis在往Redis里面写数据的时候,默认会先对数据进行序列化,然后把序列化之后的字节码写入Redis:然后当Spring Data Redis从Redis里取数据的时候,会取出字节码进行反序列化操作,在反序列化的过程中没有对目标类进行校验和过滤,可导致远程任意代码执行. 攻击路径: 1.首先准备反序列化payload,参考ysoserial系列. 2.把生

【12c】扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE

[12c]扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE 在12c中,与早期版本相比,诸如VARCHAR2, NAVARCHAR2以及 RAW这些数据类型的大小会从4K以及2K字节扩展至32K字节.只要可能,扩展字符的大小会降低对LOB 数据类型的使用.为了启用扩展字符大小,你必须将MAX_STRING_SIZE的初始数据库参数设置为EXTENDED. 要使用扩展字符类型需要执行以下过程: 1.关闭数据库 2.以升级模式重启数据库3.更改参数: ALT