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 == )
  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, , cds.lpData, bt.Length); //Memory copy.
  61. SendMessage(WINDOW_HANDLER, WM_COPYDATA, , 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,,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. }

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)

【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. 使用Prism提供的类实现WPF MVVM点餐Demo

    使用Prism提供的类实现WPF MVVM点餐Demo 由于公司开发的技术需求,近期在学习MVVM模式开发WPF应用程序.进过一段时间的学习,感受到:学习MVVM模式,最好的方法就是用MVVM做几个D ...

  2. C#中文本模板(.tt)

    关于C#中文本模板(.tt)的简单应用 这两天做项目突遇 .tt文件,之前没有接触过,so查询学习做笔记,帮助记忆和后来者. 在项目添加中点击选择文本模板 下面贴出代码,做了简单的注释 1 2 3 4 ...

  3. SDUTOJ 2054 双向链表

    #include<iostream> #include<stdlib.h> using namespace std; typedef int ElemType; typedef ...

  4. 我的Android 4 学习系列之开始入手:配置开发环境与理解Hello World!

    目录 如何安装Android SDK.创建开发环境和调试项目 移动设计中一些注意事项 使用Android虚拟设备.模拟器和其他开发工具 如何安装Android SDK.创建开发环境和调试项目 下载和安 ...

  5. JavaScript实例技巧精选(12)—计算星座与属相

    >>点击这里下载完整html源码<< 这是截图: 核心代码如下: <SCRIPT LANGUAGE="JavaScript"> <!-- ...

  6. 在GridView的中有一个DropDownList,并且DropDownList有回传事件

    在GridView的中有一个DropDownList,并且DropDownList有回传事件 最近做一个项目,需要在GridView中的ItemTemplate中添加一个DropDownList,并且 ...

  7. KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态

    KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态获取 <寒江独钓>内核学习笔记(5)   继续我们的线程相关的数据结构的学习.接下来我们学习 KTH ...

  8. webx学习1

    webx的配置 如果想在web项目中使用webx框架,需要在web-inf/web.xml文件中进行相关的配置. 1.初始化spring容器 初始化spring容器- /web-inf/web.xml ...

  9. Bundles

    Bundles 接着在Global.asax文件的Application_Start方法中调用BundleConfig.RegisterBundles方法: protected void Applic ...

  10. iMac 无线键盘 无法配对

    正好小编手里也有一个 Apple wireless keyboard 键盘,经测试发现确实有他所说的问题.在互联网上找了一圈儿都没找到解决方案,苹果官方也没有给出相关方案.只好自己琢磨,还好终于研究出 ...