在线观看免费黄色网址,一区二区视频,亚洲国产综合网,欧美VA免费高清在线观看

在Wince/WM實現進程間通信

來源:網絡

點擊:1858

A+ A-

所屬頻道:新聞中心

關鍵詞: Wince/WM,進程通信

    做WM上的進程間通信,使用WindowMessage實現兩個進程間的通信,感覺MessageWindow不太好用,所以就用別的方法實現接收WindowsMessage。

    先來封裝一下需要使用的功能,命名為Cls_Message:

    view plaincopy to clipboardprint?
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Runtime.InteropServices;  
    using Microsoft.WindowsCE.Forms;  
    using System.Windows.Forms;  
    class Cls_Message  
    {  
        private struct COPYDATASTRUCT  
        {  
            public int dwData;  
            public int cbData;  
            public IntPtr lpData;  
        }  
        //-------------------------------------------------------------------------------  
        private const int WM_COPYDATA = 0x004A;  
        private const int GWL_WNDPROC = -4;  
        private const int LMEM_FIXED = 0x0000;  
        private const int LMEM_ZEROINIT = 0x0040;  
        private const int LPTR = (LMEM_FIXED | LMEM_ZEROINIT);  
        private  IntPtr oldWndProc = IntPtr.Zero;  
        private  WndProcDelegate newWndProc;  
        private IntPtr formHandle;  
        //-------------------------------------------------------------------------------  
        delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);  
        [DllImport("coredll.dll")]  
        static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);  
        [DllImport("coredll.dll", EntryPoint = "GetWindowLong")]  
        private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);  
        [DllImport("coredll.dll")]  
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);  
        [DllImport("coredll.dll", EntryPoint = "FindWindow")]  
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
        [DllImport("coredll.dll")]  
        private static extern int SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);  
        [DllImport("coredll.dll")]  
        private static extern IntPtr LocalAlloc(int flag, int size);  
        [DllImport("coredll.dll")]  
        private static extern IntPtr LocalFree(IntPtr p);  
        /// <summary>  
        /// 初始化消息類  
        /// </summary>  
        /// <param name="handle">接受消息的窗體的句柄</param>  
        public Cls_Message(IntPtr formHandle)  
        {  
            this.formHandle = formHandle;  
            newWndProc = new WndProcDelegate(WndProc);  
            oldWndProc = GetWindowLong(formHandle, GWL_WNDPROC);  
            int success = SetWindowLong(formHandle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(newWndProc));  
        }  
        /// <summary>  
        /// 消息處理  
        /// </summary>  
        private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)  
        {  
            if (msg == WM_COPYDATA)  
            {  
                COPYDATASTRUCT st = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));  
                string str = Marshal.PtrToStringUni(st.lpData);  
                MessageBox.Show(str);  
            }  
            return CallWindowProc(oldWndProc, this.formHandle, msg, wParam, lParam);  
        }  
          
        static private IntPtr AllocHGlobal(int cb)  
        {  
            IntPtr hMemory = new IntPtr();  
            hMemory = LocalAlloc(LPTR, cb);  
            return hMemory;  
        }  
        static private void FreeHGlobal(IntPtr hMemory)  
        {  
            if (hMemory != IntPtr.Zero)  
                LocalFree(hMemory);  
        }  
        /// <summary>  
        /// 發送消息  
        /// </summary>  
        /// <param name="formTitle">目標窗體的名稱</param>  
        /// <param name="message">消息內容</param>  
        static public void SendMessage(String formTitle,String message)  
        {  
            IntPtr hWndDest = FindWindow(null, formTitle);  
            COPYDATASTRUCT oCDS = new COPYDATASTRUCT();  
            oCDS.cbData = (message.Length + 1) * 2;  
            oCDS.lpData = LocalAlloc(LPTR, oCDS.cbData);  
            Marshal.Copy(message.ToCharArray(), 0, oCDS.lpData, message.Length);  
            oCDS.dwData = 1;  
            IntPtr lParam = AllocHGlobal(oCDS.cbData);  
            Marshal.StructureToPtr(oCDS, lParam, false);  
            SendMessageW(hWndDest, WM_COPYDATA, IntPtr.Zero, lParam);  
            LocalFree(oCDS.lpData);  
            FreeHGlobal(lParam);  
        }  

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using Microsoft.WindowsCE.Forms;
    using System.Windows.Forms;
    class Cls_Message
    {
        private struct COPYDATASTRUCT
        {
            public int dwData;
            public int cbData;
            public IntPtr lpData;
        }
        //-------------------------------------------------------------------------------
        private const int WM_COPYDATA = 0x004A;
        private const int GWL_WNDPROC = -4;
        private const int LMEM_FIXED = 0x0000;
        private const int LMEM_ZEROINIT = 0x0040;
        private const int LPTR = (LMEM_FIXED | LMEM_ZEROINIT);
        private  IntPtr oldWndProc = IntPtr.Zero;
        private  WndProcDelegate newWndProc;
        private IntPtr formHandle;
        //-------------------------------------------------------------------------------
        delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
        [DllImport("coredll.dll")]
        static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("coredll.dll", EntryPoint = "GetWindowLong")]
        private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("coredll.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);
        [DllImport("coredll.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("coredll.dll")]
        private static extern int SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("coredll.dll")]
        private static extern IntPtr LocalAlloc(int flag, int size);
        [DllImport("coredll.dll")]
        private static extern IntPtr LocalFree(IntPtr p);
        /// <summary>
        /// 初始化消息類
        /// </summary>
        /// <param name="handle">接受消息的窗體的句柄</param>
        public Cls_Message(IntPtr formHandle)
        {
            this.formHandle = formHandle;
            newWndProc = new WndProcDelegate(WndProc);
            oldWndProc = GetWindowLong(formHandle, GWL_WNDPROC);
            int success = SetWindowLong(formHandle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(newWndProc));
        }
        /// <summary>
        /// 消息處理
        /// </summary>
        private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            if (msg == WM_COPYDATA)
            {
                COPYDATASTRUCT st = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
                string str = Marshal.PtrToStringUni(st.lpData);
                MessageBox.Show(str);
            }
            return CallWindowProc(oldWndProc, this.formHandle, msg, wParam, lParam);
        }
       
        static private IntPtr AllocHGlobal(int cb)
        {
            IntPtr hMemory = new IntPtr();
            hMemory = LocalAlloc(LPTR, cb);
            return hMemory;
        }
        static private void FreeHGlobal(IntPtr hMemory)
        {
            if (hMemory != IntPtr.Zero)
                LocalFree(hMemory);
        }
        /// <summary>
        /// 發送消息
        /// </summary>
        /// <param name="formTitle">目標窗體的名稱</param>
        /// <param name="message">消息內容</param>
        static public void SendMessage(String formTitle,String message)
        {
            IntPtr hWndDest = FindWindow(null, formTitle);
            COPYDATASTRUCT oCDS = new COPYDATASTRUCT();
            oCDS.cbData = (message.Length + 1) * 2;
            oCDS.lpData = LocalAlloc(LPTR, oCDS.cbData);
            Marshal.Copy(message.ToCharArray(), 0, oCDS.lpData, message.Length);
            oCDS.dwData = 1;
            IntPtr lParam = AllocHGlobal(oCDS.cbData);
            Marshal.StructureToPtr(oCDS, lParam, false);
            SendMessageW(hWndDest, WM_COPYDATA, IntPtr.Zero, lParam);
            LocalFree(oCDS.lpData);
            FreeHGlobal(lParam);
        }
    }
     


    接下來貼出調用代碼,實現自發自收,如果要發給別的進程,只需要把SendMessage的第一個參數改為目標窗體的名稱即可(當然目標窗體也必須引用了Cls_Message實現收信息處理):

    view plaincopy to clipboardprint?
    Cls_Message clsMessage;//初始化  
    public Form1()  
    {  
        InitializeComponent();  
    }  
    private void Form1_Load(object sender, EventArgs e)  
    {  
        clsMessage = new Cls_Message(this.Handle);//使本窗體能夠接收WindowMessage  
    }  
    private void button1_Click(object sender, EventArgs e)  
    {  
        Cls_Message.SendMessage("Form1", "hello form1");  

    (審核編輯: 智匯小新)

    聲明:除特別說明之外,新聞內容及圖片均來自網絡及各大主流媒體。版權歸原作者所有。如認為內容侵權,請聯系我們刪除。

    BLACKPINK未公开的物料 | 时代少年团 录播| 12306回应有人光脚踩座椅怎么办 缅甸强震已致3名中国公民遇难 | 乘风2025最新排名| 乘风2025四公帮唱组队征集 | 好房子的新标准来了| 真正的智能座舱从不挑手机品牌| 男子肝癌晚期只打一针获新生| 少吃水果少生很多病不具有普遍性10元盒饭姐喊话浪费顾客一辈子别来 | 沉浸式感受广西三月三| 提高墙体楼板隔声性能| 甲亢哥成都行直播| 好房子的新标准来了| 中国咖啡98%来自云南| 好房子的新标准来了| 入室抢婴案奶奶因自责12年独自过春节| 找工作不要限制于招聘app| IU说雪莉是最漂亮的人| 心理师锐评赵露思新综艺| 为什么每年都会怀念张国荣| 男子肝癌晚期只打一针获新生| 甲亢哥直播打破外网的单向认知| BLACKPINK未公开的物料 | 美国又一小飞机坠毁| 愚人节站姐团建| 白敬亭 宋轶| 乘风2025四公帮唱组队征集| 李昀锐好标准的体育生下楼梯| 白敬亭 宋轶| 南京推出公积金全家桶政策助力买房| 韩国庄仕洋| 乘风2025最新排名| 淘宝推出首个AI机器人带货主播| 金秀贤拍的金赛纶睡觉照片 | 韩国庄仕洋| 姚安娜带华为手机参加活动| 好房子的新标准来了| 金秀贤一边哭一边喝水| 愚人节站姐团建| 甲亢哥针灸正骨后已老实| 陈昊宇陈丽君四公帮唱|