2009-12-15 138 views
6

我想通过管道将数据从c#应用程序发送到C++应用程序。 这里是我做了什么:通过管道在C++和c#之间进行通信

这是C++客户端:

#include "stdafx.h" 
#include <windows.h> 
#include <stdio.h> 


int _tmain(int argc, _TCHAR* argv[]) { 

    HANDLE hFile; 
    BOOL flg; 
    DWORD dwWrite; 
    char szPipeUpdate[200]; 
    hFile = CreateFile(L"\\\\.\\pipe\\BvrPipe", GENERIC_WRITE, 
          0, NULL, OPEN_EXISTING, 
          0, NULL); 

    strcpy(szPipeUpdate,"Data from Named Pipe client for createnamedpipe"); 
    if(hFile == INVALID_HANDLE_VALUE) 
    { 
     DWORD dw = GetLastError(); 
     printf("CreateFile failed for Named Pipe client\n:"); 
    } 
    else 
    { 
     flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL); 
     if (FALSE == flg) 
     { 
     printf("WriteFile failed for Named Pipe client\n"); 
     } 
     else 
     { 
     printf("WriteFile succeeded for Named Pipe client\n"); 
     } 
     CloseHandle(hFile); 
    } 
return 0; 

}

,这里的C#服务器

using System; 
using System.IO; 
using System.IO.Pipes; 
using System.Threading; 
namespace PipeApplication1{ 

class ProgramPipeTest 
{ 

    public void ThreadStartServer() 
    { 
     // Create a name pipe 
     using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("\\\\.\\pipe\\BvrPipe")) 
     { 
      Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode()); 

      // Wait for a connection 
      pipeStream.WaitForConnection(); 
      Console.WriteLine("[Server] Pipe connection established"); 

      using (StreamReader sr = new StreamReader(pipeStream)) 
      { 
       string temp; 
       // We read a line from the pipe and print it together with the current time 
       while ((temp = sr.ReadLine()) != null) 
       { 
        Console.WriteLine("{0}: {1}", DateTime.Now, temp); 
       } 
      } 
     } 

     Console.WriteLine("Connection lost"); 
    } 

    static void Main(string[] args) 
    { 
     ProgramPipeTest Server = new ProgramPipeTest(); 

     Thread ServerThread = new Thread(Server.ThreadStartServer); 

     ServerThread.Start(); 

    } 
} 

}

当我启动服务器,然后来自客户端的客户端GetLastErrror返回2(系统可以没有找到指定的文件。)

对此有任何想法。 谢谢

回答

6

我猜,在服务器中创建管道时不需要“\。\ Pipe \”前缀。调用NamedPipeServerStream构造函数的examples我刚才看到传入管道名称。例如。

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("BvrPipe")) 

您可以使用SysInternals进程管理器列出打开的管道及其名称。这应该可以帮助您验证管道是否有正确的名称。有关详细信息,请参阅this问题。

+0

我可以证实这一点,我已经使用C#和C++之间的管道,我做了在创建NamedPipeServerStream时不在管道名称上使用Pipe \ prefix – 2016-10-05 10:52:03

+0

在上面的代码中,直到我们从C++代码调用管道句柄上的CloseHandle,数据不会传递到C#服务器。我也尝试过“FlushFileBuffers”函数,它的功能 – 2017-02-27 05:23:43

+0

@Raveendra你无法刷新套接字,请参阅http://stackoverflow.com/questions/12814835/flush-a-socket-in-c了解详情。 ://www.tangentsoft.net/wskfaq/intermediate.html#packetscheme。 – 2017-02-28 11:32:15

-1

请阅读this了解管道是如何实现的。为什么不使用CreateNamedPipes API调用?您将C++端的文件句柄视为普通文件而不是管道。因此,当您正在查找管道时,您的C++代码会失败,实际上它正试图从文件中读取数据。

+1

使用CreateFile()打开管道的_client_端是完全有效的。请参阅CreateFile函数的注释部分(http://msdn.microsoft.com/zh-cn/library/aa363858%28VS.85%29.aspx)。 – 2009-12-15 11:35:51

+0

@andyjohnson:我没有意识到你可以这样做......我的坏......对于误导性声明抱歉...... :( – t0mm13b 2009-12-15 11:58:22