A demonstration of inter-process communication (IPC) between C# and Python using Windows named pipes.
This guide shows how to establish bidirectional communication between a C# application and a Python script using named pipes, a Windows mechanism for IPC using a server-client model.
- .NET SDK (for C# example)
- Python 3.x
- pywin32 library for Python:
pip install pywin32
- Run the C# application to start the named pipe server
- Run the Python script to connect and communicate
- Both applications will exchange messages:
- C# displays: "Hello Back"
- Python displays: "Hello Python"
- Server-client architecture — C# acts as server, Python as client
- Bidirectional messaging — Send and receive data
- Windows-native — Uses Windows named pipes for reliable IPC
- Simple interface — Straightforward message passing
- Error handling — Graceful connection management
Named pipes are a Windows IPC mechanism:
- Server listens on a named pipe
- Client connects to the named pipe
- Data flows bidirectionally through the pipe
- No network required; works on same machine or network
C# Application (Server)
↓
Named Pipe: "\\.\pipe\MyAppPipe"
↓
Python Script (Client)
↓
Bidirectional Message Exchange
// Create named pipe server
NamedPipeServerStream server =
new NamedPipeServerStream("MyAppPipe");
// Wait for client connection
server.WaitForConnection();
// Send/receive data
StreamWriter writer = new StreamWriter(server);
StreamReader reader = new StreamReader(server);# Connect to named pipe
with open(r'\\.\pipe\MyAppPipe', 'r+b', buffering=0) as pipe:
# Send message
pipe.write(b'Hello from Python')
# Receive message
message = pipe.read(1024)Messages are sent as UTF-8 encoded strings:
- C# sends:
StreamWriter.WriteLine() - Python sends:
pipe.write(message.encode()) - Null terminator: Optional but recommended for delimiting
- Server listens — Named pipe created and waiting
- Client connects — Establishes connection to pipe
- Handshake — Optional acknowledgment messages
- Exchange — Bidirectional communication
- Disconnect — Close streams and release resources
- Ensure C# server is running before starting Python client
- Check pipe name matches exactly (case-sensitive)
- Verify firewall isn't blocking
- Server may be blocking on read; implement threading
- Use non-blocking I/O for better responsiveness
- Named pipes require appropriate Windows permissions
- Run as administrator if needed