• c#进程间通信(Inter-Process Communication)


    c#进程间通信(IPC, Inter-Process Communication)

    接收端:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;  //for  MarshalAs
    
    
    namespace receive
    {
        public partial class Receive : Form
        {
    public Receive()
            {
                InitializeComponent();
            }
            protected override void DefWndProc(ref System.Windows.Forms.Message m)          //重构此函数接收数据
            {
                switch (m.Msg)
                {
                    case Message.WM_TEST:  //确定类型后,处理数据
                         string  message = string.Format("收到消息!参数为:{0},{1},{2}", m.Msg, m.WParam, m.LParam);
                        label1.Text = message;
                        break;
                    default:
                        base.DefWndProc(ref m);
                        break;
                }
            }
        }
        class Message
        {
            public const int USER = 0X0400;
            public const int WM_TEST = USER + 101;
            public const int WM_MSG = USER + 102;
        }
    
    }

    发送端:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;  //for DllImport
    
    namespace send
    {
        public partial class Form1 : Form
        {
            [DllImport("User32.dll", EntryPoint = "SendMessage")]
            private static extern int SendMessage(int hWnd, int msg, int wParam, int lParam);      //发送消息
            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            private static extern int FindWindow(string lpClassName, string lpWindowName);      //获取另一个进程的窗口句柄
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int hWnd = FindWindow(null, @"Receive"); 
                SendMessage(hWnd, Message.WM_TEST, 99, 100);//参数1、窗口句柄。2 、消息类型。 3、数据1。 4、数据2
            }
        }
        class Message
        {
            public const int USER = 0X0400;
            public const int WM_TEST = USER + 101;      //简单的数据传输类型,传两个整数。
            public const int WM_MSG = USER + 102;
        }
    }

    比较复杂的字符串传输:

    发送端:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;  //for DllImport
    
    namespace send
    {
        public partial class Form1 : Form
        {
            [DllImport("User32.dll", EntryPoint = "SendMessage")]
           // private static extern int SendMessage(int hWnd, int msg, int wParam, int lParam);       //发送消息
            private static extern int SendMessage(int hWnd, int msg, int wParam, ref COPYDATASTRUCT lParam);   
            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            private static extern int FindWindow(string lpClassName, string lpWindowName);      //获取另一个进程的窗口句柄
    
            const int WM_COPYDATA = 0x004A;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int hWnd = FindWindow(null, @"Receive");
                string message = "你好消息成功发送!";
                int i = message.Length;
                byte[] sarr = System.Text.Encoding.Default.GetBytes(message);
                COPYDATASTRUCT cds;
                cds.dwData = (IntPtr)100;
                cds.lpData = message;
                cds.cbData = sarr.Length+1;         //此值错误会引发接收端崩溃
                SendMessage(hWnd, WM_COPYDATA, 0, ref cds);
            }
        }
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }
    }
    View Code

    接收端:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;  //for  MarshalAs
    
    
    namespace receive
    {
        public partial class Receive : Form
        {
            const int WM_COPYDATA = 0x004A;
            const int WM_MYSYMPLE = 0x005A;
    
            public Receive()
            {
                InitializeComponent();
            }
            protected override void DefWndProc(ref System.Windows.Forms.Message m)
            {
                switch (m.Msg)
                {
                    case WM_COPYDATA:
                        COPYDATASTRUCT myStr = new COPYDATASTRUCT();
                        Type myType = myStr.GetType();
                        myStr = (COPYDATASTRUCT)m.GetLParam(myType);    //m中获取LParam参数以myType类型的方式,让后转换问结构体。
                        label1.Text = myStr.lpData;
                        break;
                    default:
                        base.DefWndProc(ref m);
                        break;
                }
            }
    
        }
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }
    }
    View Code

    下载:链接:http://pan.baidu.com/s/1c0lWpt2 密码:yvzz

    其他方法参考:

    http://blog.csdn.net/feiren127/article/details/5459827

    附:在程序中使用代码启动程序

    using System.Diagnostics;   // for ProcessStartInfo
    
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:UsersdellDocumentsVisual Studio 2010Projects
    eceive
    eceiveinDebug
    eceive.exe";
    Process pro = new Process();
    pro.StartInfo = startInfo;
    pro.Start();        //开启
    IntPtr hWnd = pro.MainWindowHandle;    //进程的句柄
    pro.Kill();            //关闭

     参考: http://blog.csdn.net/u011000290/article/details/48108557

  • 相关阅读:
    通过PROFINET网络实现SINAMICS 120的PN IO OPC通讯,起动及调速控制
    Python datetime获取当前年月日时分秒
    计算机网络:套接字(Socket)| Python socket实现服务器端与客户端通信,使用TCP socket阿里云ECS服务器与本机通信
    Ubuntu16.04安装、卸载宝塔软件
    Ubuntu一键安装LAMP,LNMP
    STM32使用K型热电偶测温:运算放大器+内置ADC+K型热电偶分度表+中间温度定律 | K型热电偶的温度-热电势曲线
    盘点几种DIY加密狗的制作方法,适用于穿越机模拟器
    变频器通讯参数PKW和PZD的含义
    穿越机从0到起飞:选件
    西门子S7-1200PLC不让下载一直报“模块具有激活的测试和调试功能,防止下载到设备”解决方法
  • 原文地址:https://www.cnblogs.com/lwngreat/p/4786718.html
Copyright © 2020-2023  润新知