winform进程间通信

进程之间通讯的几种方法:
在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有:
使用内存映射文件
通过共享内存DLL共享内存
使用SendMessage向另一进程发送WM_COPYDATA消息
比起前两种的复杂实现来,WM_COPYDATA消息无疑是一种经济实惠的一中方法.(ZT)
WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。SDK文档推荐用户使用SendMessage函数,接受方在数据拷贝完成前不返回,这样发送方就不可能删除和修改数据:
这个函数的原型及其要用到的结构如下:
SendMessage(hwnd,WM_COPYDATA,wParam,lParam);
其中,WM_COPYDATA对应的十六进制数为0x004A
wParam设置为包含数据的窗口的句柄。lParam指向一个COPYDATASTRUCT的结构:
typedef struct tagCOPYDATASTRUCT{
DWORD dwData;//用户定义数据
DWORD cbData;//数据大小
PVOID lpData;//指向数据的指针
}COPYDATASTRUCT;
该结构用来定义用户数据。

具体过程如下:
首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.
接受方在DefWndProc事件中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.
具体体代码如下:
//----------------------------------发送方----------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da ta;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinFormSendMsg
{
    public partial class Form1 : Form
    {
        const int WM_COPYDATA = 0x004A;
        public Form1()
        {
            InitializeComponent();
        }

        [DllImp ort("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
        );

        [DllImp ort("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);

        private void button1_Click(object sender, EventArgs e)
        {
            //int WINDOW_HANDLER = FindWindow(null, @"欲发送程序窗口的标题");
            int WINDOW_HANDLER = FindWindow(null, @"接收窗口的标题");
            if (WINDOW_HANDLER != 0)
            {
                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;
    }
}
//----------------------------------接收方----------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da ta;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinFormGetMsg
{
    public partial class Form1 : Form
    {
        const int WM_COPYDATA = 0x004A;

        public Form1()
        {
            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);
                    this.textBox1.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;
        }
    }
}
在 Unity 中生成的 exe 可以通过使用 .NET Framework 的进程间通信机制来接收来自 WinForm 进程的数据。其中一个常用的方法是使用命名管道(Named Pipes)来进行通信。 以下是一个简单的例子,演示了如何在 Unity 中创建一个名为“UnityPipe”的命名管道,并等待来自 WinForm 进程的消息: ```csharp using System; using System.IO; using System.IO.Pipes; using System.Text; using UnityEngine; public class PipeServer : MonoBehaviour { private NamedPipeServerStream _pipeServer; private void Start() { // 创建命名管道 _pipeServer = new NamedPipeServerStream("UnityPipe"); // 等待 WinForm 进程连接 _pipeServer.WaitForConnection(); Debug.Log("WinForm 进程已连接"); // 读取来自 WinForm 进程的消息 var reader = new StreamReader(_pipeServer); var message = reader.ReadLine(); Debug.Log($"收到消息:{message}"); // 关闭命名管道 _pipeServer.Close(); } } ``` 在 WinForm 进程中,可以使用以下代码向“UnityPipe”命名管道发送消息: ```csharp using System.IO.Pipes; using System.Text; var pipeClient = new NamedPipeClientStream(".", "UnityPipe", PipeDirection.Out); pipeClient.Connect(); var writer = new StreamWriter(pipeClient); writer.WriteLine("Hello, Unity!"); writer.Flush(); pipeClient.Close(); ``` 通过这种方式,Unity 中生成的 exe 就可以与 WinForm 进程进行通信,并接收来自 WinForm 进程的数据了。注意,在使用命名管道进行通信时,需要确保 Unity 中的命名管道名称和 WinForm 中的命名管道名称是一致的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值