2009-12-15 144 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