基本信息
源码名称:c#进程之间通信,c#exe 之间发消息,c#exe 相互通信
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2016-05-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
c#进程之间通信,c#exe 之间发消息,c#exe 相互通信 vs2005 编写的 接收 发送都有
原理:根据窗口名称找到 窗口的句柄,然后给窗口 SendMessage
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace WinFormSendMsg { public partial class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; const int WM_COPYDATA = 0x004A; public Form1() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(184, 24); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(128, 21); this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1"; // // button1 // this.button1.Location = new System.Drawing.Point(344, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 32); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click = new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(536, 142); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.textBox1}); this.Name = "Form1"; this.Text = "发送方窗体"; this.ResumeLayout(false); } static void Main() { Application.Run(new Form1()); } [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( int hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter ref COPYDATASTRUCT lParam // second message parameter ); [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); private void button1_Click(object sender, System.EventArgs e) { int WINDOW_HANDLER = FindWindow(null, @"接收方窗体"); if (WINDOW_HANDLER == 0) { } else { byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text); int len = sarr.Length; COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = this.textBox1.Text; cds.cbData = len 1; SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds); } } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }