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在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的系 ...
随机推荐
- 【转】Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例--不错
原文网址:http://www.cnblogs.com/skywang12345/p/3311252.html 概要 这一章,我们对HashSet进行学习.我们先对HashSet有个整体认识,然后再学 ...
- jQuery/CSS3实现图片层叠展开特效
这是一款基于jQuery和CSS3的图片层叠展开特效,让鼠标滑过图片时即可触发这些特效.其中有一款就像扇子展开收拢一样,看起来效果都非常不错.当然本文主要还是来分析一下用jQuery实现这一效果的方法 ...
- weblogic 的应用 常见问题处理 db2 链接不上(转载)
xingkaistart weblogic10之Failed to initialize the application 'wss-1-1' due to error weblogic. Weblog ...
- wpf在异步中给前台赋值
wpf,新建异步方法: Thread newThread = new Thread(new ParameterizedThreadStart(GetResult)); newThread.Start( ...
- iOS语音识别,语音播报,文字变语音播报,语音变文字
首先使用的是科大讯飞的sdk 1.语音识别部分 AppDelegate.m #import "AppDelegate.h" #import <iflyMSC/iflyMSC. ...
- C++编程规范之20:避免函数过长,避免嵌套过深
摘要: 短胜于长,平胜于优,过长的函数和嵌套过深的代码块的出现,经常是因为没能赋予一个函数以一个紧凑的职责所致,这两种情况通常都能够通过更好的重构予以解决. 每个函数都应该顾其名而能知其义,易于理解的 ...
- lesson5:利用jmeter来压测消息队列(activemq)
本文讲述了利用jmeter来压测消息队列,其中消息队列采用apache的activemq,jmeter本身是支持符合jms标准消息队列的压测,由于jmeter的官方sampler配置比较复杂,本文直接 ...
- Android 之 资源文件的介绍及使用
Android 之 资源文件的介绍及使用 1.资源的简单介绍: 在res文件夹中定义:字符串.颜色.数组.菜单.图片.视频等:在应用程序中使用这些资源. 2.使用资源的长处:降低代码量,同一时候为 ...
- [每日一题] OCP1z0-047 :2013-08-15 描述GROUPING 函数 .......................................43
正确答案:C ,否则返回0. 官方解释:GROUPING distinguishes superaggregate rows fromregular grouped rows. GROUP BY ex ...
- 近段时间学习html和CSS的一些细碎总结
1.边框圆角属性:border-radius,取值能够是 百分比 / 自己定义长度,不能够取负值.假设是圆,将高度和宽度设置相等,而且将border-radius设置为100% 2.IE6,IE7,I ...