1. 事件订阅
1. 使用命名管道进行通信
服务端示例代码
csharp
using System;
using System.IO.Pipes;
using System.Text;
public class NamedPipeServer
{
public static void Main()
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut))
{
Console.WriteLine("Waiting for client connection...");
pipeServer.WaitForConnection();
using (var reader = new StreamReader(pipeServer))
using (var writer = new StreamWriter(pipeServer))
{
writer.WriteLine("Hello from server");
writer.Flush();
string message = reader.ReadLine();
Console.WriteLine("Received from client: " + message);
}
}
}
}
客户端示例代码
csharp
using System;
using System.IO.Pipes;
using System.Text;
public class NamedPipeClient
{
public static void Main()
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut))
{
Console.WriteLine("Connecting to server...");
pipeClient.Connect();
using (var reader = new StreamReader(pipeClient))
using (var writer = new StreamWriter(pipeClient))
{
string message = reader.ReadLine();
Console.WriteLine("Received from server: " + message);
writer.WriteLine("Hello from client");
writer.Flush();
}
}
}
}
2. 使用匿名管道进行通信
服务端示例代码
csharp
using System;
using System.IO.Pipes;
public class AnonymousPipeServer
{
public static void Main()
{
using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out))
{
Console.WriteLine("Writing to client...");
pipeServer.Write(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0, 4);
}
}
}
客户端示例代码
csharp
using System;
using System.IO.Pipes;
public class AnonymousPipeClient
{
public static void Main()
{
using (AnonymousPipeClientStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In))
{
AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, pipeClient);
pipeServer.DisposeLocalCopyOfClientHandle();
byte[] buffer = new byte[4];
int bytesRead = pipeClient.Read(buffer, 0, 4);
Console.WriteLine("Read {0} bytes from server.", bytesRead);
}
}
}
3. 使用 SendMessage 实现通信
发送端示例代码
csharp
using System;
using System.Runtime.InteropServices;
public class SendMessageSender
{
public const int WM_COPYDATA = 0x004A;
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
public static void Main()
{
string message = "Hello from Sender";
COPYDATASTRUCT cds;
cds.dwData = IntPtr.Zero;
cds.cbData = message.Length + 1;
cds.lpData = message;
int hwnd = FindWindow(null, "Receiver Window Title");
SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
}
}
接收端示例代码
csharp
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class SendMessageReceiver : Form
{
public const int WM_COPYDATA = 0x004A;
public SendMessageReceiver()
{
// Set up the main window...
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COPYDATA)
{
COPYDATASTRUCT cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
string text = Marshal.PtrToStringAnsi(cds.lpData, cds.cbData - 1);
MessageBox.Show(text);
}
base.WndProc(ref m);
}
public static void Main()
{
Application.Run(new SendMessageReceiver());
}
}
结论
往期精品推荐: