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在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的系 ...
随机推荐
- cf500A New Year Transportation
A. New Year Transportation time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- C++Memset误区
Memset的原型是void *memset(void *s, char ch, size_t n); Memset是按字节赋值的,对char以外的类型赋0(00000000) -1(11111111 ...
- double精度的坑与BigDecimal
近期经常接触支付相关的功能,在开发及测试过程中,开始金额都使用的是double类型,而近期新进的需求存在支付时打折的情况,也就是会出现如 1.23元的情况,那么这时候问题来了,如果是直接使用1.23进 ...
- MAC安装SVNServer
MAC已经自带了SVN,所以,直接使用就好 1.创建svn repository svnadmin create /path/svn/pro //仓库位置,svn是svn的目录,pro是一个版本库的 ...
- udp 不需要 listen
accept()不是监听,accept()是接受新连接.listen()是进入监听状态,表示愿意接收连接请求.listen之后有连接请求就将其放到队列中,accept()时把新连接请求从队列中取出,建 ...
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- Linux常用命令之 查找命令 find —— 细说 -atime,-mtime,-ctime
我们知道 Linux里面一切皆文件 ,那么我们能否查看一个文件是何时创建的呢?答案是否定的.那我们可以知道些文件关于时间的什么信息呢?那就不得不说文件状态的三个时间了,它们分别是 -atime, -c ...
- next数组
首先看看next数组值的求解方法例如: 模式串 a b a a b c a c next值 0 1 1 2 2 3 1 2 next数组的求解方法是:第一位的next值为0 ...
- nginx安装(正式)
一.安装说明 系统环境:CentOS Linux release 7.2.1511 (Core) 系统内核:3.10.0-327.el7.x86_64软件:nginx-1.10.1.tar.gz其他所 ...
- MacOS U盘安装
通过Mac上的App Store下载安装程序 插入U盘 启动终端 输入命令 sudo /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/R ...