【问题标题】:communication between c++ and c# through pipec++和c#之间通过管道进行通信
【发布时间】:2010-12-26 18:24:29
【问题描述】:

我想通过管道将数据从 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();

    }
}

}

当我启动服务器然后客户端GetLastError从客户端返回2(系统找不到指定的文件。)

对此有任何想法。 谢谢

【问题讨论】:

    标签: c# c++ ipc


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

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

    您可以使用 SysInternals 进程浏览器列出打开的管道及其名称。这应该可以帮助您验证管道是否具有正确的名称。详情见this问题。

    【讨论】:

    • 我可以确认一下,我在 C# 和 C++ 之间使用过管道,并且在创建 NamedPipeServerStream 时没有在管道名称上使用 Pipe\ 前缀。
    • 在上面的代码中,直到我们从 C++ 代码中调用管道句柄上的 CloseHandle,数据才会传递到 C# 服务器。有没有办法在不关闭手柄的情况下通过?我也尝试了“FlushFileBuffers”功能。它起作用了
    • @Raveendra 您无法刷新套接字。有关信息,请参阅stackoverflow.com/questions/12814835/flush-a-socket-in-c。另见tangentsoft.net/wskfaq/intermediate.html#packetscheme
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2011-02-09
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多