利用Use32.dll的SendMessage实现 进程间通信

项目一览

利用Use32.dll的SendMessage实现 进程间通信_第1张图片

关键代码:

UserMessage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace MyMessage
{
    public class SystemMessage
    {
        public const int WM_COPYDATA = 0x004A;
    }

    public struct MessageInfo
    {
        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.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SenderForm_A
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref MyMessage.MessageInfo lParam);
        /// <summary>
        /// 这个函数有两个参数,第一个是要找的窗口的类,第二个是要找的窗口的标题。
        /// 在搜索的时候不一定两者都知道,但至少要知道其中的一个。有的窗口的标题是比较容易得到的,如"计算器",
        /// 所以搜索时应使用标题进行搜索。但有的软件的标题不是固定的,如"记事本",
        /// 如果打开的文件不同,窗口标题也不同,这时使用窗口类搜索就比较方便。
        /// 如果找到了满足条件的窗口,这个函数返回该窗口的句柄,否则返回0。
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr intPtr = FindWindow(null, "System.Windos.Forms.Message_Demo");
            if (intPtr != IntPtr.Zero)
            {
                string szText = this.textBox1.Text.Trim();
                if (string.IsNullOrEmpty(szText))
                {
                    MessageBox.Show("内容为空");
                }
                else
                {
                    byte[] b = System.Text.Encoding.Default.GetBytes(szText);
                    MyMessage.MessageInfo info;
                    info.dwData = this.Handle;
                    info.cbData = b.Length + 1;
                    info.lpData = szText;
                    SendMessage(intPtr, MyMessage.SystemMessage.WM_COPYDATA, IntPtr.Zero,ref info);
                }

            }
        }
    }

}


接收窗口:

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

namespace System.Windos.Forms.Message_Demo
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == MyMessage.SystemMessage.WM_COPYDATA)
            {
                MyMessage.MessageInfo messageInfo = new MyMessage.MessageInfo();
                System.Type myType = messageInfo.GetType();
                messageInfo = (MyMessage.MessageInfo)m.GetLParam(myType);
                string szText = messageInfo.lpData;
                this.textBox1.Text += DateTime.Now.ToString("yyyy MM/dd HH:mm:ss") + ":" + szText + Environment.NewLine;
                this.textBox1.Update();
            }
        }
    }
}


效果图:

利用Use32.dll的SendMessage实现 进程间通信_第2张图片

你可能感兴趣的:(C#,sendmessage,进程间通信,User32.ll)