進程間發送消息整理(高級方案)

接上篇:

高級方案與簡易方案的區別主要在於:前者可以定義需要傳遞的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;
            }
        }

    }

}




 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章