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)

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

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

    [CF802L]Send the Fool Further! (hard) 题意:给你一棵n个节点的树,每条边有长度,从1号点开始,每次随机选择一个相邻的点走,走到一个叶子时就停止,问期望走的总路程. ...

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

    原文地址:[DataGuard]部署Data Guard相关参数详解 作者:secooler    有关物理Data Guard部署参考<[DataGuard]同一台主机实现物理Data Gua ...

  3. 【机器学习】Iris Data Set(鸢尾花数据集)

    [机器学习]Iris Data Set(鸢尾花数据集) 注:数据是机器学习模型的原材料,当下机器学习的热潮离不开大数据的支撑.在机器学习领域,有大量的公开数据集可以使用,从几百个样本到几十万个样本的数 ...

  4. 【转】char data[0]用法总结

    @2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ...

  5. 【WinForm】“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常,无法识别的配置节 system.serviceModel

    出现问题的原因: 在本机上没有出现问题,让一个同事测试的时候,在另外一台电脑上出现连接数据库失败,系统不能打开的问题 在网上搜了一下,有说是数据库连接字符串错误的,有说app.config文件配置不匹 ...

  6. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  7. 【原创】System.Data.SQLite内存数据库模式

    对于很多嵌入式数据库来说都有对于的内存数据库模式,SQLite也不例外.内存数据库常常用于极速.实时的场景,一个很好的应用的场景是富客户端的缓存数据,一般富客户端的缓存常常需要分为落地和非落地两种,而 ...

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

    安装完ASP.NET,Oracle9i客户端后,使用System.Data.OracleClient访问Oracle数据库如果出现这种错误:System.Data.OracleClient requi ...

  9. 【机器学习】Iris Data Set(鸢尾属植物数据集)

    注:数据是机器学习模型的原材料,当下机器学习的热潮离不开大数据的支撑.在机器学习领域,有大量的公开数据集可以使用,从几百个样本到几十万个样本的数据集都有.有些数据集被用来教学,有些被当做机器学习模型性 ...

随机推荐

  1. ASP.NET中Partial Class部分类

    原文:ASP.NET中Partial Class部分类 如果您在开发项目的公用函数库,用于公用函数库的内容越丰富越好,但这不可能一下子就写好,需要一点一点的积累,这时就可以使用Partial Clas ...

  2. .NET和JAVA的比较- 体系结构

    .NET体系结构   对于.NET Framework体系结构,参考了"你必须知道的.NET"并”借用“别人的经典体系结构图从宏观上说明一下我的理解. 图1 简单的说下几个名词: ...

  3. String.Join的实现

    String.Join的实现 在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的 ...

  4. beanutils获取带参数get方法

    public Employee getEmployee(int index) {        return new Employee();    } 1.PropertyUtils.getIndex ...

  5. 一步一步实现基于Task的Promise库(三)waitFor方法的设计

    在上一篇中我们已经完成了Task.js里面的all和any方法,已经可以完美的解决大部分需求,我们再来看一个需求: 我们要先读取aa.txt的内容,然后去后台解析,同时由用户指定一个文件,也要读取解析 ...

  6. C# 4.0 Parallel

    C# 4.0 并行计算部分   沿用微软的写法,System.Threading.Tasks.::.Parallel类,提供对并行循环和区域的支持. 我们会用到的方法有For,ForEach,Invo ...

  7. Git工作流:中心工作流(翻译)

    使用Git作为版本控制器,有众多可能的工作流(Workflow),这使得我们这些新鸟不知道在实际工作中不知道该选择哪种工作流.这里我们对最常见的Git工作流做一个对比,为企业团队提供一个参考. 正如你 ...

  8. EasyUI搭建前端框架

    EasyUI搭建前端框架 前言: 最近在忙公司的项目,也没太多时间来更新博客,谢谢大家的关注. 好啦,有了前面的系统整体简介和用户登录界面,我们开始使用EasyUI搭建前端页面框架! EasyUI官方 ...

  9. Xps实现文档显示、套打功能

    wpf采用Xps实现文档显示.套打功能(原创) 近期的一个项目需对数据进行套打,用户要求现场不允许安装office.页面预览显示必须要与文档完全一致,xps文档来对数据进行处理.Wpf的Documen ...

  10. VS调试技巧与快捷键&&VS快捷键

    VS调试技巧与调试快捷键 1.添加断点或取消断点:F9(或者点击代码行最左边的灰色行)   2.调试:F10逐过程(不进入函数内部,直接获取函数运行结果)  F11逐语句(会进入函数),如果想跳出函数 ...