两个winform.exe程序之间的通讯一————————发送消息

本文详细介绍使用WinForms实现进程间通信的方法,通过调用user32.dll API,利用WM_COPYDATA消息指令,在两个WinForms应用程序之间传递数据。示例代码展示了如何在发送端和接收端设置通信结构和消息处理,使不同进程的窗口能够相互通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,两个winform.exe在系统里体现是两个进程,而进程是是由系统管理。

2,user32.dll是Windows用户界面相关应用程序接口,用于包括Windows处理,基本用户界面等特性,如创建窗口和发送消息。

3,所有可以通过调用user32.dll的api来进行两个进程之间的通信。

4,发送端exe.源码

        

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Send
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public struct CopyDataStruct
        {
            public IntPtr dwData;
            public int cbData;

            [MarshalAs(UnmanagedType.LPStr)]

            public string lpData;
        }

        public const int WM_COPYDATA = 0x004A;
        //当一个应用程序传递数据给另一个应用程序时发送此消息指令

        //通过窗口的标题来查找窗口的句柄 
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);

        //在DLL库中的发送消息函数
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage
            (
            int hWnd,                         // 目标窗口的句柄  
            int Msg,                          // 在这里是WM_COPYDATA
            int wParam,                       // 第一个消息参数
            ref CopyDataStruct lParam        // 第二个消息参数
           );

        private void button1_Click(object sender, EventArgs e)
        {

            //将文本框中的值, 发送给接收端           
            string text = textBox1.Text;
            CopyDataStruct cds;
            cds.dwData = (IntPtr)1; //这里可以传入一些自定义的数据,但只能是4字节整数      
            cds.lpData = text;    //消息字符串
            cds.cbData = System.Text.Encoding.Default.GetBytes(text).Length + 1;  
            //注意,这里的长度是按字节来算的
            SendMessage(FindWindow(null, "接收端"), WM_COPYDATA, 0, ref cds);     
            // 这里要修改成接收窗口的标题“接收端”
        }
    }
}

5,接收端exe源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reserve
{
    public partial class Form1 : Form
    {
        //WM_COPYDATA消息所要求的数据结构
        public struct CopyDataStruct
        {
            public IntPtr dwData;
            public int cbData;

            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        private const int WM_COPYDATA = 0x004A;
        //接收消息方法
        protected override void WndProc(ref System.Windows.Forms.Message e)
        {
            if (e.Msg == WM_COPYDATA)
            {
                CopyDataStruct cds = (CopyDataStruct)e.GetLParam(typeof(CopyDataStruct));
                textBox1.Text = cds.lpData.ToString();  //将文本信息显示到文本框
                                                        //MessageBox.Show(cds.lpData);
            }
            base.WndProc(ref e);
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }
    }
}

6,原理:window提供了发送消息指令,0x004A,我们只需根据特定的函数和结构,发送消息,系统会自动找到另一个窗口名字的进程的窗口句柄,重写该窗口的接收消息函数,接收发送的消息。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左左在右边

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值