коммуникация между 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
задан itchy 15 December 2009 в 11:10
поделиться

2 ответа

At a guess, I'd say you don't need the "\.\Pipe\" prefix when creating the pipe in the server. The examples of calling the NamedPipeServerStream constructor that I've seen just pass-in the pipe name. E.g.

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

You can list the open pipes, and their names, using SysInternals process explorer. This should help you verify that the pipe has the correct name. See this question for details.

6
ответ дан 17 December 2019 в 04:47
поделиться

Please read this to see how the pipes are implemented. Why are you not using CreateNamedPipes API call? You are treating the file handle on the C++ side as an ordinary file not a pipe. Hence your C++ code fails when it is looking for a pipe when in fact it was attempting to read from a file.

-1
ответ дан 17 December 2019 в 04:47
поделиться
Другие вопросы по тегам:

Похожие вопросы: