Advertisement
EddyCZ

Untitled

Oct 5th, 2023
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. class Program
  7. {
  8.     const uint PIPE_ACCESS_INBOUND = 0x00000001;
  9.     const uint PIPE_TYPE_BYTE = 0x00000000;
  10.     const uint PIPE_READMODE_BYTE = 0x00000000;
  11.     const uint PIPE_WAIT = 0x00000000;
  12.     const uint PIPE_UNLIMITED_INSTANCES = 255;
  13.  
  14.     [DllImport("kernel32.dll", SetLastError = true)]
  15.     public static extern IntPtr CreateNamedPipeA(
  16.         string lpName,
  17.         uint dwOpenMode,
  18.         uint dwPipeMode,
  19.         uint nMaxInstances,
  20.         uint nOutBufferSize,
  21.         uint nInBufferSize,
  22.         uint nDefaultTimeOut,
  23.         IntPtr lpSecurityAttributes);
  24.  
  25.     [DllImport("kernel32.dll", SetLastError = true)]
  26.     [return: MarshalAs(UnmanagedType.Bool)]
  27.     public static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);
  28.  
  29.     [DllImport("kernel32.dll", SetLastError = true)]
  30.     [return: MarshalAs(UnmanagedType.Bool)]
  31.     public static extern bool CloseHandle(IntPtr hObject);
  32.  
  33.     [DllImport("kernel32.dll", SetLastError = true)]
  34.     [return: MarshalAs(UnmanagedType.Bool)]
  35.     public static extern bool ReadFile(
  36.         IntPtr hFile,
  37.         byte[] lpBuffer,
  38.         uint nNumberOfBytesToRead,
  39.         out uint lpNumberOfBytesRead,
  40.         IntPtr lpOverlapped);
  41.  
  42.     static void Main()
  43.     {
  44.         string pipeName = @"\\.\pipe\MyNamedPipe";
  45.         IntPtr pipeHandle = CreateNamedPipeA(
  46.             pipeName,
  47.             PIPE_ACCESS_INBOUND,
  48.             PIPE_TYPE_BYTE,
  49.             PIPE_UNLIMITED_INSTANCES,
  50.             0, // Buffer size for output data
  51.             0, // Buffer size for input data
  52.             0, // Default timeout (0 means blocking)
  53.             IntPtr.Zero);
  54.  
  55.         if (pipeHandle != IntPtr.Zero)
  56.         {
  57.             Console.WriteLine("Named pipe created successfully. Waiting for a client to connect...");
  58.  
  59.             // Wait for a client to connect
  60.             bool connected = ConnectNamedPipe(pipeHandle, IntPtr.Zero);
  61.             if (connected)
  62.             {
  63.                 Console.WriteLine("Client connected.");
  64.  
  65.                 // Read data from the named pipe
  66.                 byte[] buffer = new byte[1024]; // Adjust the buffer size as needed
  67.                 uint bytesRead;
  68.                 bool success = ReadFile(pipeHandle, buffer, (uint)buffer.Length, out bytesRead, IntPtr.Zero);
  69.                 if (success)
  70.                 {
  71.                     string data = Encoding.ASCII.GetString(buffer, 0, (int)bytesRead);
  72.                     Console.WriteLine("Received data from the client: " + data);
  73.                 }
  74.                 else
  75.                 {
  76.                     Console.WriteLine("Failed to read data. Error code: " + Marshal.GetLastWin32Error());
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 Console.WriteLine("Failed to connect to client. Error code: " + Marshal.GetLastWin32Error());
  82.             }
  83.  
  84.             // Close the named pipe when done
  85.             CloseHandle(pipeHandle);
  86.         }
  87.         else
  88.         {
  89.             Console.WriteLine("Failed to create named pipe. Error code: " + Marshal.GetLastWin32Error());
  90.         }
  91.  
  92.         Console.WriteLine("Press Enter to exit...");
  93.         Console.ReadLine();
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement