-
Notifications
You must be signed in to change notification settings - Fork 3
/
NamedPipes_SMB.cs
93 lines (76 loc) · 3.34 KB
/
NamedPipes_SMB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This example demonstrates how to use named pipes to route commands to a server and retrieve the corresponding responses.
using System.Diagnostics;
using System.IO.Pipes;
using System.Text;
class Program
{
public enum Command
{
ProcessList,
Exit,
}
const string pipeName = "NamedPipeExample";
public static void Main(string[] args)
{
Thread namedPipeServerThread = new(() =>
{
try
{
// https://learn.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeserverstream?view=net-7.0?WT_mc_id=SEC-MVP-5005282
using NamedPipeServerStream serverPipe = new(pipeName, PipeDirection.InOut);
serverPipe.WaitForConnection();
using StreamReader reader = new(serverPipe);
using StreamWriter writer = new(serverPipe) { AutoFlush = true };
///
while (true)
{
switch(Enum.Parse(typeof(Command), reader.ReadLine() ?? ""))
{
case Command.ProcessList:
{
StringBuilder sb = new();
foreach (Process process in Process.GetProcesses())
sb.AppendLine($"({process.Id.ToString().PadRight(5, ' ')}){process.ProcessName}");
// Encode as Base64 to send to whole list in one single `WriteLine`
writer.WriteLine(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sb.ToString())));
break;
}
default:
{
// Exit or unknown or empty string.
break;
}
}
}
}
catch { }
});
namedPipeServerThread.Start();
Thread namedPipeClientThread = new(() =>
{
try
{
// `.` means local machine, it can be replaced by the network computer name hosting a the named pipe server.
// https://learn.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeclientstream?view=net-7.0?WT_mc_id=SEC-MVP-5005282
using NamedPipeClientStream clientPipe = new(".", pipeName, PipeDirection.InOut);
clientPipe.Connect();
using StreamReader reader = new(clientPipe);
using StreamWriter writer = new(clientPipe) { AutoFlush = true };
///
// Ask server for running process
writer.WriteLine(Command.ProcessList);
// Receive response
string? response = reader.ReadLine();
if (response != null)
Console.WriteLine(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(response)));
// Tell server, we finished our job
writer.WriteLine(Command.Exit);
}
catch { }
});
namedPipeClientThread.Start();
///
namedPipeServerThread.Join();
namedPipeClientThread.Join();
}
}