智汇工业-智慧工业、智能制造及工业智能、工业互联门户网站,专业的工业“互联网+”传媒

在Wince/WM實(shí)現(xiàn)進(jìn)程間通信

來(lái)源:網(wǎng)絡(luò)

點(diǎn)擊:1893

A+ A-

所屬頻道:新聞中心

關(guān)鍵詞: Wince/WM,進(jìn)程通信

    做WM上的進(jìn)程間通信,使用WindowMessage實(shí)現(xiàn)兩個(gè)進(jìn)程間的通信,感覺(jué)MessageWindow不太好用,所以就用別的方法實(shí)現(xiàn)接收WindowsMessage。

    先來(lái)封裝一下需要使用的功能,命名為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>  
        /// 發(fā)送消息  
        /// </summary>  
        /// <param name="formTitle">目標(biāo)窗體的名稱</param>  
        /// <param name="message">消息內(nèi)容</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>
        /// 發(fā)送消息
        /// </summary>
        /// <param name="formTitle">目標(biāo)窗體的名稱</param>
        /// <param name="message">消息內(nèi)容</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);
        }
    }
     


    接下來(lái)貼出調(diào)用代碼,實(shí)現(xiàn)自發(fā)自收,如果要發(fā)給別的進(jìn)程,只需要把SendMessage的第一個(gè)參數(shù)改為目標(biāo)窗體的名稱即可(當(dāng)然目標(biāo)窗體也必須引用了Cls_Message實(shí)現(xiàn)收信息處理):

    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");  

    (審核編輯: 智匯小新)

    聲明:除特別說(shuō)明之外,新聞內(nèi)容及圖片均來(lái)自網(wǎng)絡(luò)及各大主流媒體。版權(quán)歸原作者所有。如認(rèn)為內(nèi)容侵權(quán),請(qǐng)聯(lián)系我們刪除。

    主站蜘蛛池模板: 溶气气浮机_一体化净水设备_污水处理设备_mbr一体化污水处理设备-明基环保 | 联想南京总代理-联想服务器|联想电脑笔记本代理商|联想工作站|dell服务器|HP服务器|南京IBM代理商|IBM V5000存储总包销-南京宇宽科技有限公司 | 四川超声波清洗机厂家-旋转喷淋清洗机设备-成都鑫荣诚超声波科技有限公司 | 太原塑料托盘,塑料筐,塑料箱,塑料垃圾桶_太原都程塑料制品有限公司 | 烟台废旧物资回收,烟台废品回收,烟台物资回收-烟台金泰再生资源有限公司 | 南山荔枝,深圳南荔农业荔枝园自销-质保优放心选购 | 萘系减水剂|缓凝|早强|聚羧酸|混凝土|石膏板|管桩减水剂厂家-潍坊英宏建材有限公司 | 专注客流统计,客流分析,人流统计系统,客流计数器-广州市天威电子科技有限公司 | 齐东汽车-提供抑尘车|洒水车|压缩垃圾车|餐厨垃圾车|垃圾转运车|清洗吸污车|扫路车价格,图片及视频 | 自动锁螺丝机_在线式拧螺丝机_自动化灌胶机_ab点胶机_品牌厂家 | 小鼠实验(试验)用臭氧发生器_进口臭氧发生器 - 北京同林臭氧实验网 | 立式加工中心_龙门加工中心_卧式加工中心-山东威达重工股份有限公司 | 潍坊亿宏重工机械有限公司,破碎机,高性能立磨机,颚式破碎机,锤式破碎机反击式破碎机,重锤式破碎机,高性能反击式破碎机,圆锥式破碎机,给料机系列,链板给料机系列,简易给料机系列,振动给料机 | 亚洲一区日韩一区欧美一区a,中文字幕乱妇无码AV在线,欧美日韩免费在线观看,国产精品一区二区三区免费,日韩精品免费一线在线观看,日韩一本在线,国产呦精品一区二区三区下载,国产日韩精品一区二区在线观看,欧美日韩高清一区二区三区,日韩在线免费观看视频,欧美日韩一区在线观看 | 内蒙古慧申设计顾问有限公司-官网| 四川迪瑞机电设备有限公司-容积式换热器|半容积式换热器|容积式换热机组|半容积式水加热器|换热器在线除垢防垢器|迪瑞机电 | 七台河市供排水有限责任公司| 永磁电机,防爆电机,调速电机,永磁同步电机_河南华信电机股份有限公司 | 呼吸家官网|肺功能检测仪生产厂家|国产肺功能仪知名品牌|肺功能检测仪|肺功能测试仪|婴幼儿肺功能仪|弥散残气肺功能仪|肺功能测试系统|广州红象医疗科技有限公司|便携式肺功能仪|大肺功能仪|呼吸康复一体机|儿童肺功能仪|肺活量计|医用简易肺功能仪|呼吸康复系统|肺功能仪|弥散肺功能仪(大肺)|便携式肺功能检测仪|肺康复|呼吸肌力测定肺功能仪|肺功能测定仪|呼吸神经肌肉刺激仪|便携式肺功能 | 衢州装饰公司_衢州装修公司_衢州创美装饰工程有限公司 - Powered by www.qzcmzs.com | 郑州空气能热水器,郑州空气能热水器经销商,郑州空气能热水器维修,郑州空气能热水器安装 | 压力容器锻件_升高法兰_管板_阀体_接管锻件 - 山西中重重工集团 压力机-压装机-黄油机-黄油泵-[广东品嘉灵]专业定制各种精密压装设备 | 上海搬运公司_上海工厂设备搬迁_大型设备吊装搬运_设备安装公司-桂星装卸搬运 | 专业提供医疗器械,医疗设备,进口国产医疗设备,医疗耗材采购,医疗设备厂家等医疗器械信息-上海聚慕医疗器械有限公司 | 河北高新技术企业认定,沧州商标注册,沧州9001质量管理体系认证,沧州高新技术企业认定,沧州体系认证,沧州商标续展,沧州版权登记,河北国瑞企业管理咨询有限公司 | 深圳市新纶超净科技有限公司,防静电/洁净室行业系统解决方案提供商 | 齐东汽车-提供抑尘车|洒水车|压缩垃圾车|餐厨垃圾车|垃圾转运车|清洗吸污车|扫路车价格,图片及视频 | 上海外资代理记账|上海软银财务咨询有限公司 | 长型材数控钻孔攻牙机-自动数控热熔钻孔机-东莞市利速数控机械有限公司 | 钱眼网-透过钱眼看商机 | 铜排,异型紫棒,紫铜棒,紫铜微孔管,异型黄管,黄铜管,异形紫管,紫铜管,焊接铜管,散热器铜管,电力铜管_河间市通海铜业有限公司 | 无锡防爆墙-无锡泄爆墙_江苏鑫立轩装饰工程有限公司 | 西安测试仪-西安电压测试仪-西安电流测试仪-西安热油汽水测试仪-西安阻抗测试仪-西安时间速度测试仪-西安电力设备厂家-西安中洲电力设备有限公司 | 学汽修,学汽修技术,汽修培训班,汽车美容培训,汽车新能源技术培训-广州万通汽车培训学校[官方网站] | 江苏成人高考网-江苏省成人高考报名| 远红外桑拿房-江苏侨达健康科技有限公司| 温州方圆仪器有限公司 工业自动化|自动化设备 - 温州方圆仪器有限公司 | 水上游乐设备 - 郑州亿浪水上乐园设备有限公司 | 温州冰顺制冷科技有限公司| 西安泰富西玛电机有限公司总部-电机-高压电机-西玛电机-西安西玛电机-泰富西玛电机-西安电机厂-西玛电机销售 | 益家304不锈钢水管厂家|批发代理|价格|薄壁|广东益家管业有限公司 |