Advertisement
EddyCZ

Untitled

Oct 6th, 2023
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     const uint PIPE_ACCESS_OUTBOUND = 0x00000002;
  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.     const uint FILE_FLAG_OVERLAPPED = 0x40000000;
  14.  
  15.     [StructLayout(LayoutKind.Sequential)]
  16.     public struct SECURITY_ATTRIBUTES
  17.     {
  18.         public int nLength;
  19.         public IntPtr lpSecurityDescriptor;
  20.         public int bInheritHandle;
  21.     }
  22.  
  23.     [DllImport("kernel32.dll", SetLastError = true)]
  24.     public static extern IntPtr CreateNamedPipe(
  25.         string lpName,
  26.         uint dwOpenMode,
  27.         uint dwPipeMode,
  28.         uint nMaxInstances,
  29.         uint nOutBufferSize,
  30.         uint nInBufferSize,
  31.         uint nDefaultTimeOut,
  32.         IntPtr lpSecurityAttributes);
  33.  
  34.     [DllImport("kernel32.dll", SetLastError = true)]
  35.     public static extern int ConnectNamedPipe(
  36.         IntPtr hNamedPipe,
  37.         IntPtr lpOverlapped);
  38.  
  39.     [DllImport("kernel32.dll", SetLastError = true)]
  40.     public static extern int WriteFile(
  41.         IntPtr hFile,
  42.         byte[] lpBuffer,
  43.         uint nNumberOfBytesToWrite,
  44.         out uint lpNumberOfBytesWritten,
  45.         IntPtr lpOverlapped);
  46.  
  47.     [DllImport("kernel32.dll", SetLastError = true)]
  48.     public static extern int CloseHandle(IntPtr hObject);
  49.  
  50.     [DllImport("kernel32.dll", SetLastError = true)]
  51.     public static extern IntPtr GetStdHandle(int nStdHandle);
  52.  
  53.     static void Main()
  54.     {
  55.         string pipeName = @"\\.\pipe\MyNamedPipe";
  56.         IntPtr hPipe;
  57.  
  58.         // Create the named pipe
  59.         hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
  60.             PIPE_UNLIMITED_INSTANCES, 0, 0, 0, IntPtr.Zero);
  61.  
  62.         if (hPipe == IntPtr.Zero || hPipe.ToInt32() == -1)
  63.         {
  64.             Console.WriteLine("Error creating named pipe: " + Marshal.GetLastWin32Error());
  65.             return;
  66.         }
  67.  
  68.         // Wait for a client to connect
  69.         Console.WriteLine("Waiting for client to connect...");
  70.         if (ConnectNamedPipe(hPipe, IntPtr.Zero) == 0)
  71.         {
  72.             Console.WriteLine("Error connecting to client: " + Marshal.GetLastWin32Error());
  73.             return;
  74.         }
  75.         Console.WriteLine("Client connected.");
  76.  
  77.         // Get the standard output handle
  78.         IntPtr hStdOut = GetStdHandle(-11); // STD_OUTPUT_HANDLE
  79.  
  80.         // Read user input from the console and send it to the named pipe
  81.         while (true)
  82.         {
  83.             Console.Write("Enter text to send to client (or 'exit' to quit): ");
  84.             string input = Console.ReadLine();
  85.  
  86.             if (input.ToLower() == "exit")
  87.                 break;
  88.  
  89.             byte[] buffer = Encoding.ASCII.GetBytes(input);
  90.             uint bytesWritten;
  91.  
  92.             if (WriteFile(hPipe, buffer, (uint)buffer.Length, out bytesWritten, IntPtr.Zero) == 0)
  93.             {
  94.                 Console.WriteLine("Error writing to pipe: " + Marshal.GetLastWin32Error());
  95.                 break;
  96.             }
  97.         }
  98.  
  99.         // Close the named pipe and exit
  100.         CloseHandle(hPipe);
  101.         Console.WriteLine("Named pipe closed.");
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement