Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- class Program
- {
- const uint PIPE_ACCESS_INBOUND = 0x00000001;
- const uint PIPE_TYPE_BYTE = 0x00000000;
- const uint PIPE_READMODE_BYTE = 0x00000000;
- const uint PIPE_WAIT = 0x00000000;
- const uint PIPE_UNLIMITED_INSTANCES = 255;
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr CreateNamedPipeA(
- string lpName,
- uint dwOpenMode,
- uint dwPipeMode,
- uint nMaxInstances,
- uint nOutBufferSize,
- uint nInBufferSize,
- uint nDefaultTimeOut,
- IntPtr lpSecurityAttributes);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool CloseHandle(IntPtr hObject);
- static void Main()
- {
- string pipeName = @"\\.\pipe\MyNamedPipe";
- IntPtr pipeHandle = CreateNamedPipeA(
- pipeName,
- PIPE_ACCESS_INBOUND,
- PIPE_TYPE_BYTE,
- PIPE_UNLIMITED_INSTANCES,
- 0, // Buffer size for output data
- 0, // Buffer size for input data
- 0, // Default timeout (0 means blocking)
- IntPtr.Zero);
- if (pipeHandle != IntPtr.Zero)
- {
- Console.WriteLine("Named pipe created successfully.");
- // Wait for a client to connect (you can use ConnectNamedPipe here)
- // Do some work with the named pipe
- // Close the named pipe when done
- CloseHandle(pipeHandle);
- }
- else
- {
- Console.WriteLine("Failed to create named pipe. Error code: " + Marshal.GetLastWin32Error());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement