Advertisement
EddyCZ

Untitled

Oct 5th, 2023
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. class Program
  5. {
  6.     const uint PIPE_ACCESS_INBOUND = 0x00000001;
  7.     const uint PIPE_TYPE_BYTE = 0x00000000;
  8.     const uint PIPE_READMODE_BYTE = 0x00000000;
  9.     const uint PIPE_WAIT = 0x00000000;
  10.     const uint PIPE_UNLIMITED_INSTANCES = 255;
  11.  
  12.     [DllImport("kernel32.dll", SetLastError = true)]
  13.     public static extern IntPtr CreateNamedPipeA(
  14.         string lpName,
  15.         uint dwOpenMode,
  16.         uint dwPipeMode,
  17.         uint nMaxInstances,
  18.         uint nOutBufferSize,
  19.         uint nInBufferSize,
  20.         uint nDefaultTimeOut,
  21.         IntPtr lpSecurityAttributes);
  22.  
  23.     [DllImport("kernel32.dll", SetLastError = true)]
  24.     [return: MarshalAs(UnmanagedType.Bool)]
  25.     public static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);
  26.  
  27.     [DllImport("kernel32.dll", SetLastError = true)]
  28.     [return: MarshalAs(UnmanagedType.Bool)]
  29.     public static extern bool CloseHandle(IntPtr hObject);
  30.  
  31.     static void Main()
  32.     {
  33.         string pipeName = @"\\.\pipe\MyNamedPipe";
  34.         IntPtr pipeHandle = CreateNamedPipeA(
  35.             pipeName,
  36.             PIPE_ACCESS_INBOUND,
  37.             PIPE_TYPE_BYTE,
  38.             PIPE_UNLIMITED_INSTANCES,
  39.             0, // Buffer size for output data
  40.             0, // Buffer size for input data
  41.             0, // Default timeout (0 means blocking)
  42.             IntPtr.Zero);
  43.  
  44.         if (pipeHandle != IntPtr.Zero)
  45.         {
  46.             Console.WriteLine("Named pipe created successfully.");
  47.             // Wait for a client to connect (you can use ConnectNamedPipe here)
  48.             // Do some work with the named pipe
  49.             // Close the named pipe when done
  50.  
  51.             CloseHandle(pipeHandle);
  52.         }
  53.         else
  54.         {
  55.             Console.WriteLine("Failed to create named pipe. Error code: " + Marshal.GetLastWin32Error());
  56.         }
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement