进程间发送消息整理(高级方案)

接上篇:

高级方案与简易方案的区别主要在于:前者可以定义需要传递的struct。

公用结构:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Common
{
    //发送共享消息的结构
    [StructLayout(LayoutKind.Sequential)]
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cbData;        
        public int lpData;
    }

    //定义要传递的Struct
    [StructLayout(LayoutKind.Sequential)]
  public   struct WholeInfo
    {
        //SizeConst指定字符串长度很重要,后面要用到
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10000)]
        public string str1;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10000)]
        public string str2;
        public bool status;
    }
}

发送方:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace ProjectA
{
    public partial class SenderForm : Form
    {

        const int WM_COPYDATA = 0x004A;

        //发送消息的API
        [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 Common.COPYDATASTRUCT lParam // second message parameter
        );

        //通过窗体标题找出窗体的Handle
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string  lpWindowName);

        public SenderForm()
        {
            InitializeComponent();
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {

            Common.WholeInfo h = new Common.WholeInfo();
            h.str1  = txtMsg.Text;
            h.str2 = txtMsg2.Text ;
            h.status = true;
            int size = Marshal.SizeOf(typeof(Common.WholeInfo));
            byte[] Bytes = new byte[size];
            //根据定义的尺寸分配内存块
            GCHandle GC = GCHandle.Alloc(Bytes, GCHandleType.Pinned);
            IntPtr ptr1 = GC.AddrOfPinnedObject();
            //获得Struct对应的IntPtr
            Marshal.StructureToPtr(h, ptr1, false);

            Common.COPYDATASTRUCT SendData = new Common.COPYDATASTRUCT();

            SendData.lpData = ptr1.ToInt32();
            SendData.cbData = size;
            //找出接收方窗体的FormHandle
            int intHWnd = FindWindow(null, @"接收方");
            if (intHWnd > 0)
            {
                //发送消息
                SendMessage(intHWnd, WM_COPYDATA, 0, ref (SendData));
            }                        
        }
    }
}

接收方:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;


namespace ProjectB
{
    public partial class ReceiverForm : Form
    {
        
        const int WM_COPYDATA = 0x004A;


        public ReceiverForm()
        {
            InitializeComponent();
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case WM_COPYDATA:
                    //取出结构
                    Common.COPYDATASTRUCT RecvData = (Common.COPYDATASTRUCT)m.GetLParam(typeof(Common.COPYDATASTRUCT));
                    //转换取出定义的传递结构
                    Common.WholeInfo h = (Common.WholeInfo)Marshal.PtrToStructure((IntPtr) RecvData.lpData, typeof(Common.WholeInfo));

                    this.txtMsg.Text = h.str1;
                    this.txtMsg2.Text = h.str2;

                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

    }

}




 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值