【C#】Send data between applications

时间:2023-03-09 00:11:59
【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:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO; //The following four usings are important.So are they in Reciever and dll.
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using ClassLibrary1; namespace cdn_send
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
} private const int WM_COPYDATA = 0x004A;
private const uint flag = 0x8000; //I can't understand it.Please tell me(if you got it).
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("kernel32.dll")]
static extern uint GetTickCount(); private void button1_Click(object sender, EventArgs e)
{
int WINDOW_HANDLER = FindWindow(null, @"xxx"); //Find target window.Well,by the way,it's called 'xxx'.
if (WINDOW_HANDLER == )
{
MessageBox.Show("xxx");
}
else
{ data dd=new data(); //Process data.
dd.x = (int)this.Handle;
dd.y = DateTime.Now;
dd.tx = textBox1.Text;
dd.tk = GetTickCount();
byte[] bt=(new switcher()).Object2Bytes((object)dd); //Type switch. COPYDATASTRUCT cds;
cds.dwData = (IntPtr)flag;
cds.cbData = bt.Length;
cds.lpData = Marshal.AllocHGlobal(bt.Length); //Allocate space. Marshal.Copy(bt, , cds.lpData, bt.Length); //Memory copy.
SendMessage(WINDOW_HANDLER, WM_COPYDATA, , ref cds); //Send message out.
}
}
} }

Reciever:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO; //Important.
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using ClassLibrary1; namespace cdn_receiver
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ } const int WM_COPYDATA = 0x004A;
private const uint flag = 0x8000;
[DllImport("kernel32.dll")]
public static extern uint GetTickCount(); protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_COPYDATA: COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds = (COPYDATASTRUCT)m.GetLParam(cds.GetType()); //Receive information. byte[] bt = new byte[cds.cbData];
Marshal.Copy(cds.lpData,bt,,bt.Length); //Get data array. data dd = (data)((new switcher()).Bytes2Object(bt)); //Transform back.
long xx = GetTickCount() - dd.tk; //This line is used to calculate its delay,although mostly it is 0ms.
textBox1.Text = (dd.x.ToString() + " " + dd.y.ToString() + " " + dd.tx);
textBox1.Text += "\r\n" + xx.ToString() + "ms"; break;
default:
base.DefWndProc(ref m); //Don't forget this line,or it cannot run properly.
break;
} }
} }

Dll:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Runtime;
using System.IO; //Important
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ClassLibrary1
{ [Serializable] //My datastructure which contains different types.Don't forget this line.
public struct data
{
public int x;
public DateTime y;
public string tx;
public long tk;
} [StructLayout(LayoutKind.Sequential)] //The datastructure which used as a media to transport.
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
} public class switcher //The switcher object which contains two main switcher function.
{
public byte[] Object2Bytes(object obj)
{
IFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
fmt.Serialize(ms, obj);
return ms.GetBuffer(); } public object Bytes2Object(byte[] bt)
{
IFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(bt);
return (object)fmt.Deserialize(ms);
} public switcher(){
}
} }

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)