我是在c#代码中调用的winAPI来实现的命名管道 
  服务器端:
   static void Main(string[] args)
        {
            string pipename="\\\\.\\pipe\\mypipe";
            uint BufferSize=1024;
            IntPtr h_pipe=IntPtr.Zero;
            Process p1 = null;
            try
            {
                h_pipe = CreateNamedPipe(pipename, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, BufferSize, BufferSize, 0, IntPtr.Zero);
                if ((IntPtr)INVALID_HANDLE_OPERATION == h_pipe)
                {
                    Console.WriteLine("管道打开失败!");
                    return;
                }
                Process process = new Process();
                ProcessStartInfo ps = new ProcessStartInfo();
                ps.FileName = "E:\\MyProject\\PipeClient\\PipeClient\\bin\\Debug\\PipeClient.exe";
                ps.Arguments = pipename+" "+"来自服务端";
                ps.WindowStyle = ProcessWindowStyle.Normal;
                 p1= Process.Start(ps);                byte[] readbyte = new byte[1024];
                int readsize = 0;
                unsafe
                {
                    fixed (byte* preadbyte = readbyte)
                    {
                        System.Threading.Thread.Sleep(1000);
                        if (ReadFile(h_pipe, (void*)preadbyte, BufferSize, &readsize, 0))
                        {
                            Console.WriteLine(System.Text.Encoding.Default.GetString(readbyte));
                        }
                        else
                        {
                            Console.WriteLine("读取文件失败");
                        }
                    }
                }
            }
            finally
            {
                if(p1!=null)
                p1.Close();
            if (h_pipe != null)
                CloseHandle(h_pipe);
            }
        }
    }
客户端:
  static void Main(string[] args)
        {
            IntPtr h_pipe = IntPtr.Zero;
            try
            {
                if (args != null && args.Length > 0)
                {
                    h_pipe = CreateFile(args[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, System.IntPtr.Zero);
                    if (h_pipe == (IntPtr)INVALID_HANDLE_OPERATION)
                    {
                        Console.WriteLine("打开管道失败!");
                        return;
                    }
                    else
                    {
                        byte[] writebyte = new byte[1024];
                        writebyte=System.Text.Encoding.Default.GetBytes(args[1]+"(客户端返回)");
                        uint buffersize=1024;
                        uint outputbuffer=0;
                        unsafe
                        {
                            fixed (byte* writebytes = writebyte)
                            {
                                if (WriteFile(h_pipe, (void*)writebytes, buffersize, out outputbuffer, IntPtr.Zero))
                                {
                                    Console.WriteLine("客户端通过管道写入成功!");
                                }
                                else
                                {
                                    Console.WriteLine("客户端通过管道写入失败!");
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("参数为空");
                }
            }
            finally
            {
                if (h_pipe != null)
                    CloseHandle(h_pipe);
            }
        }   运行服务器端,客户端会提示“打开管道失败” ,请问我的createfile为什么会失败呢?

解决方案 »

  1.   

    http://topic.csdn.net/u/20110114/18/3a5adf98-8475-40d9-bb6d-4f37f2fddd38.html
      

  2.   

    在补充个小问题:
      在c#里面调用WinAPI的时候,接口的参数有些找不到它所代表的实际数值,那我在c#中怎么定义这些参数呢?
    比如:HANDLE CreateFile(
      LPCTSTR lpFileName,
      DWORD dwDesiredAccess,
      DWORD dwShareMode,
      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
      DWORD dwCreationDisposition,
      DWORD dwFlagsAndAttributes,
      HANDLE hTemplateFile
    );参数  dwShareMode 要求为 FILE_SHARE_DELETE 或 FILE_SHARE_READ 或 FILE_SHARE_WRITE
    msdn中没有给出这几个参数的实际值,那我在c#中调用方法的时候怎么传递这些参数呢?有些参数msdn中是给了实际值了,如  PIPE_ACCESS_DUPLEX=0x00000003  (CreateNamedPipe方法的参数)
      

  3.   


    很不幸的告诉这位仁兄啊,我兜里的分是 它“o”  ,这个40分是发帖的时候那一栏里默认的我也没有改,不知道我结贴发饷的时候csdn会怎么处理呢? 
      

  4.   

    找到问题了,我在服务端创建管道的时候定义的是PIPE_ACCESS_INBOUND类型的,所以客户端createfile连接时要定义为GENERIC_WRITE 而不能为GENERIC_WRITE|GENERIC_READ   
      

  5.   

    http://www.pinvoke.net/default.aspx/kernel32.createnamedpipehttp://www.switchonthecode.com/tutorials/interprocess-communication-using-named-pipes-in-csharphttp://www.codeproject.com/KB/threads/CodeFX_IPC.aspx?display=Print
      

  6.   

    .NET有封装管道,有现成的托管代码给你用...你多此一举不是自找麻烦吗...
      

  7.   

    .net封装好的到底层不还是用的这些东西吗?