c#进程间通信(Inter-Process Communication)
原文:c#进程间通信(Inter-Process Communication)
c#进程间通信(IPC, Inter-Process Communication)
接收端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //for MarshalAs namespace receive
{
public partial class Receive : Form
{
public Receive()
{
InitializeComponent();
}
protected override void DefWndProc(ref System.Windows.Forms.Message m) //重构此函数接收数据
{
switch (m.Msg)
{
case Message.WM_TEST: //确定类型后,处理数据
string message = string.Format("收到消息!参数为:{0},{1},{2}", m.Msg, m.WParam, m.LParam);
label1.Text = message;
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
class Message
{
public const int USER = 0X0400;
public const int WM_TEST = USER + ;
public const int WM_MSG = USER + ;
} }
发送端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //for DllImport namespace send
{
public partial class Form1 : Form
{
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int msg, int wParam, int lParam); //发送消息
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName); //获取另一个进程的窗口句柄 public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
int hWnd = FindWindow(null, @"Receive");
SendMessage(hWnd, Message.WM_TEST, , );//参数1、窗口句柄。2 、消息类型。 3、数据1。 4、数据2
}
}
class Message
{
public const int USER = 0X0400;
public const int WM_TEST = USER + ; //简单的数据传输类型,传两个整数。
public const int WM_MSG = USER + ;
}
}


比较复杂的字符串传输:
发送端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //for DllImport namespace send
{
public partial class Form1 : Form
{
[DllImport("User32.dll", EntryPoint = "SendMessage")]
// private static extern int SendMessage(int hWnd, int msg, int wParam, int lParam); //发送消息
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); //获取另一个进程的窗口句柄 const int WM_COPYDATA = 0x004A; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
int hWnd = FindWindow(null, @"Receive");
string message = "你好消息成功发送!";
int i = message.Length;
byte[] sarr = System.Text.Encoding.Default.GetBytes(message);
COPYDATASTRUCT cds;
cds.dwData = (IntPtr);
cds.lpData = message;
cds.cbData = sarr.Length+; //此值错误会引发接收端崩溃
SendMessage(hWnd, WM_COPYDATA, , ref cds);
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
}
接收端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //for MarshalAs namespace receive
{
public partial class Receive : Form
{
const int WM_COPYDATA = 0x004A;
const int WM_MYSYMPLE = 0x005A; public Receive()
{
InitializeComponent();
}
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT myStr = new COPYDATASTRUCT();
Type myType = myStr.GetType();
myStr = (COPYDATASTRUCT)m.GetLParam(myType); //m中获取LParam参数以myType类型的方式,让后转换问结构体。
label1.Text = myStr.lpData;
break;
default:
base.DefWndProc(ref m);
break;
}
} }
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
}

下载:链接:http://pan.baidu.com/s/1c0lWpt2 密码:yvzz
其他方法参考:
http://blog.csdn.net/feiren127/article/details/5459827
附:在程序中使用代码启动程序
using System.Diagnostics; // for ProcessStartInfo ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Users\dell\Documents\Visual Studio 2010\Projects\receive\receive\bin\Debug\receive.exe";
Process pro = new Process();
pro.StartInfo = startInfo;
pro.Start(); //开启
IntPtr hWnd = pro.MainWindowHandle; //进程的句柄
pro.Kill(); //关闭
参考: http://blog.csdn.net/u011000290/article/details/48108557
c#进程间通信(Inter-Process Communication)的更多相关文章
- 〖Linux〗Linux高级编程 - 进程间通信(Interprocess Communication)
[转自: http://blog.csdn.net/Paradise_for_why/article/details/5550619] 这一章就是著名的IPC,这个东西实际的作用和它的名字一样普及.例 ...
- 进程间通信IPC (InterProcess Communication)
一.进程间通信的概念 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区, ...
- The Signals Of Process Communication
在之前大概的概述了进程之间的通信,下面笔者具体述说一下进程通信中最古老的一种通信方式之一---信号(Signals ),信号是用户进程之间通信和同步的一种原始机制,操作系统通过信号来通知进程系统中发生 ...
- Java 编程要点之并发(Concurrency)详解
计算机用户想当然地认为他们的系统在一个时间可以做多件事.他们认为,他们可以工作在一个字处理器,而其他应用程序在下载文件,管理打印队列和音频流.即使是单一的应用程序通常也是被期望在一个时间来做多件事.例 ...
- linux kernel menuconfig【转载】
原文网址:http://www.cnblogs.com/kulin/archive/2013/01/04/linux-core.html Linux内核裁减 (1)安装新内核: i)将新内核copy到 ...
- Python第十三章-网络编程
网络编程 一.网络编程基础 python 的网络编程模块主要支持两种Internet协议: TCP 和 UDP. 1.1通信协议 通信协议也叫网络传输协议或简称为传送协议(Communications ...
- Unix/Linux进程间通信(一):概述
序 Linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进 ...
- c# 进程间通信 IPC
最近在调试一个算法,想通过改变算法的参数看看结果有什么变化. 碰到一个麻烦的事情是,从磁盘加载.构建数据需要15分钟.这就比较讨厌了,也就是说我每次调一个参数前都要等15分钟启动时间? 于是我就想,能 ...
- linux 进程间通信机制(IPC机制)一总览
1.作用:进程间通信机制(Inter Process Communication,IPC),这些IPC机制的存在使UNIX在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的系 ...
随机推荐
- sigaction函数的使用
sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作). 他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该 ...
- Android数据存储三剑客——SharedPreferences、File、SQLite
Android中常用的数据存储一般有三种方式:SharedPreferences.文件和SQLite数据库,用来保存需要长时间保存的数据.本文将通过几个具体的小实例来讲解这三种方式的具体实现. 数据存 ...
- HDU_2024——判断字符串是否是c语言合法标识符
Problem Description 输入一个字符串,判断其是否是C的合法标识符. Input 输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是 ...
- SQL - 配置SQLServer 使其可以远程访问
环境: SQL Server2008 R2 SQL Server Management Studio 今天测试部署项目的时候,发现不能远程访问SQL Server.具体情形就是在Management ...
- HBase的rowkey的设计原则
HBase是三维有序存储的,通过rowkey(行键),column key(column family和qualifier)和TimeStamp(时间戳)这个三个维度可以对HBase中的数据进行快速定 ...
- 使用nvm来管理nodejs版本
nvm 是 Mac 下的 node 管理工具,有点类似管理 Ruby 的 rvm,如果是需要管理 Windows 下的 node,官方推荐是使用 nvmw 或 nvm-windows .nvm主要用来 ...
- 解决IE9以下ie版本不能识别新元素的方法 添加一个js -- Shiv Solution
Thankfully, Sjoerd Visscher created the "HTML5 Enabling JavaScript", "the shiv": ...
- iOS 蓝牙4.0开发
背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...
- magento产品eav笔记【持续跟新...】
//magento把产品信息分在子表中,最顶上的表是catalog_product_entity,仅仅包含产品的信息(SKU) //表eav_attribute,这张表在magento里为全部不 同的 ...
- NSRunLoop个人理解
作者: xwang 出处: http://www.cnblogs.com/xwang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保 ...