两个C#的exe程序执行相互通信
两个exe通信有多种方式:1、直接通信;2、文件或粘贴板通信;3、协议通信(socket)。以下是采用直接通信的方法代码
发射端界面:
<Window x:Class="SendMsg.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox x:Name="txtImpinj" HorizontalAlignment="Left" Height="23" Margin="61,25,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> <Button Content="Button" HorizontalAlignment="Left" Margin="199,24,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> </Grid> </Window>
发射端后台代码:
1 public partial class MainWindow : Window 2 { 3 public MainWindow() 4 { 5 InitializeComponent(); 6 } 7 8 public const int WM_COPYDATA = 0x004A; 9 10 //通过窗口的标题来查找窗口的句柄 11 [DllImport("User32.dll", EntryPoint = "FindWindow")] 12 private static extern int FindWindow(string lpClassName, string lpWindowName); 13 14 //在DLL库中的发送消息函数 15 [DllImport("User32.dll", EntryPoint = "SendMessage")] 16 private static extern int SendMessage 17 ( 18 int hWnd, // 目标窗口的句柄 19 int Msg, // 在这里是WM_COPYDATA 20 int wParam, // 第一个消息参数 21 ref CopyDataStruct lParam // 第二个消息参数 22 ); 23 24 private void Button_Click(object sender, EventArgs e) 25 { 26 //将文本框中的值, 发送给接收端 27 string strURL = txtImpinj.Text; 28 CopyDataStruct cds; 29 cds.dwData = (IntPtr)1; //这里可以传入一些自定义的数据,但只能是4字节整数 30 cds.lpData = strURL; //消息字符串 31 cds.cbData = System.Text.Encoding.Default.GetBytes(strURL).Length + 1; //注意,这里的长度是按字节来算的 32 int hwnd = FindWindow(null, "接收端"); 33 SendMessage(hwnd, WM_COPYDATA, 0, ref cds); // 这里要修改成接收窗口的标题“接收端” 34 //this.Close(); 35 } 36 37 38 } 39 //WM_COPYDATA消息所要求的数据结构 40 public struct CopyDataStruct 41 { 42 public IntPtr dwData; 43 public int cbData; 44 45 [MarshalAs(UnmanagedType.LPStr)] 46 47 public string lpData; 48 }
接收端界面:
1 <Window x:Class="ReciveMsg.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="接收端" Height="350" Width="525"> 5 <Grid> 6 <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="78,70,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> 7 8 </Grid> 9 </Window>
接收端后台代码:
1 public partial class MainWindow : Window 2 { 3 public MainWindow() 4 { 5 InitializeComponent(); 6 7 SourceInitialized += HandleInitialized; 8 } 9 10 private void HandleInitialized(object sender, EventArgs e) 11 { 12 IntPtr wptr = new WindowInteropHelper(this).Handle; 13 HwndSource hs = HwndSource.FromHwnd(wptr); 14 hs.AddHook(new HwndSourceHook(WindowProc)); 15 } 16 private const int WM_COPYDATA = 0x004A; 17 private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 18 { 19 if (msg == WM_COPYDATA) 20 { 21 CopyDataStruct cds = (CopyDataStruct)Marshal.PtrToStructure(lParam, typeof(CopyDataStruct)); 22 textBox1.Text = cds.lpData.ToString(); //将文本信息显示到文本框 23 return IntPtr.Zero; 24 } 25 return hwnd; 26 } 27 } 28 //WM_COPYDATA消息所要求的数据结构 29 public struct CopyDataStruct 30 { 31 public IntPtr dwData; 32 public int cbData; 33 34 [MarshalAs(UnmanagedType.LPStr)] 35 public string lpData; 36 }
敲击键盘,创造价值