windows 匿名管道

匿名管道

The CreatePipe function creates an anonymous pipe and returns two handles: a read handle to the pipe and a write handle to the pipe. The read handle has read-only access to the pipe, and the write handle has write-only access to the pipe. To communicate using the pipe, the pipe server must pass a pipe handle to another process. Usually, this is done through inheritance; that is, the process allows the handle to be inherited by a child process. The process can also duplicate a pipe handle using the DuplicateHandle function and send it to an unrelated process using some form of interprocess communication, such as DDE or shared memory.

A pipe server can send either the read handle or the write handle to the pipe client, depending on whether the client should use the anonymous pipe to send information or receive information. To read from the pipe, use the pipe's read handle in a call to the ReadFile function. The ReadFile call returns when another process has written to the pipe. The ReadFile call can also return if all write handles to the pipe have been closed or if an error occurs before the read operation has been completed.

To write to the pipe, use the pipe's write handle in a call to the WriteFile function. The WriteFile call does not return until it has written the specified number of bytes to the pipe or an error occurs. If the pipe buffer is full and there are more bytes to be written, WriteFile does not return until another process reads from the pipe, making more buffer space available. The pipe server specifies the buffer size for the pipe when it calls CreatePipe.

Server Write

using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeServer
{
    static void Main()
    {
        Process pipeClient = new Process();
        pipeClient.StartInfo.FileName = "PipeClient.exe";
        using (AnonymousPipeServerStream pipeServer =
            new AnonymousPipeServerStream(PipeDirection.Out,
            HandleInheritability.Inheritable))
        {
            pipeClient.StartInfo.Arguments =
                pipeServer.GetClientHandleAsString();
            pipeClient.StartInfo.UseShellExecute = false;
            pipeClient.Start();

            pipeServer.DisposeLocalCopyOfClientHandle();
            using (StreamWriter sw = new StreamWriter(pipeServer))
            {
                sw.AutoFlush = true;
                Console.Write("Enter text: ");
                sw.WriteLine(Console.ReadLine());
            }
        }
        pipeClient.WaitForExit();
        pipeClient.Close();
    }
}

Client Read

class PipeClient
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            using (PipeStream pipeClient =
                new AnonymousPipeClientStream(PipeDirection.In, args[0]))
            {
                using (StreamReader sr = new StreamReader(pipeClient))
                {
                    string temp;
                    while ((temp = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(temp);
                    }
                }
            }
        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}

参考:http://blog.csdn.net/MyRobert/archive/2010/08/03/5783961.aspx

http://wenku.baidu.com/view/542f4c2d7375a417866f8f1f.html

都是简化的操作

原文地址:https://www.cnblogs.com/Clingingboy/p/2097808.html