如何使用命名管道在c ++ .dll和C#app之间发送消息?

Gal*_*Gal 5 c# c++ pipe

我正在用C++编写注入的.dll,我想使用命名管道与C#app进行通信.

现在,我在C#app中使用内置的System.IO.Pipe .net类,我正在使用C++中的常规函数​​.

我没有太多的C++经验(阅读:这是我的第一个C++代码..),我在C#中经验丰富.

似乎与服务器和客户端的连接正常,唯一的问题是messaged没有被发送.我尝试将.dll作为服务器,C#app作为服务器,使管道方向InOut(双工)但似乎没有工作.

当我尝试将.dll发送到C#应用程序发送消息的服务器时,我使用的代码是这样的:

DWORD ServerCreate() // function to create the server and wait till it successfully creates it to return.
{
    hPipe = CreateNamedPipe(pipename,//The unique pipe name. This string must have the following form:  \\.\pipe\pipename
    PIPE_ACCESS_DUPLEX, 
    PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT, //write and read and return right away
    PIPE_UNLIMITED_INSTANCES,//The maximum number of instances that can be created for this pipe
    4096 , // output time-out 
    4096 , // input time-out 
    0,//client time-out 
    NULL);

   if(hPipe== INVALID_HANDLE_VALUE)
   {
    return 1;//failed
   }
   else
    return 0;//success
}

void SendMsg(string msg)
{
    DWORD cbWritten;
    WriteFile(hPipe,msg.c_str(), msg.length()+1, &cbWritten,NULL);
}

void ProccesingPipeInstance()
{
    while(ServerCreate() == 1)//if failed
{
    Sleep(1000);    
}

//if created success, wait to connect
ConnectNamedPipe(hPipe, NULL);
for(;;)
{
    SendMsg("HI!");
    if( ConnectNamedPipe(hPipe, NULL)==0)
        if(GetLastError()==ERROR_NO_DATA)
        {
            DebugPrintA("previous closed,ERROR_NO_DATA");
            DisconnectNamedPipe(hPipe);
            ConnectNamedPipe(hPipe, NULL);
        }
    Sleep(1000);

}
Run Code Online (Sandbox Code Playgroud)

和C#cliend像这样:

static void Main(string[] args)
    {
        Console.WriteLine("Hello!");

        using (var pipe = new NamedPipeClientStream(".", "HyprPipe", PipeDirection.In))
        {
            Console.WriteLine("Created Client!");

            Console.Write("Connecting to pipe server in the .dll ...");
            pipe.Connect();

            Console.WriteLine("DONE!");

            using (var pr = new StreamReader(pipe))
            {
                string t;
                while ((t = pr.ReadLine()) != null)
                {
                    Console.WriteLine("Message: {0}",t);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我看到C#客户端连接到.dll,但它不会收到任何消息..我试着反过来,正如我之前所说,并试图让C#发送消息到.dll,会在消息框中显示它们..dll被注入并连接到C#服务器,但当它收到一条消息时,它只是崩溃了它被注入的应用程序.

请帮助我,或指导我如何在C++和C#app之间使用命名管道

Chr*_*lor 16

一些东西.

1-您使用相同的管道名称
C++:"\\.\ pipe\HyperPipe"
C#:"HyperPipe"

2-我认为在C#方面使用ReadToEnd()可能更好,我只在C++中使用了命名管道,但我认为ReadToEnd()会读取一条消息,因为你使用的是基于消息的管道.

3-在C++方面,您正在尝试使用非阻塞管道并轮询管道以获取连接和数据等.我建议使用以下三种方法之一.

  a - Use a blocking pipe on a separate thread or
  b - Use non blocking pipes using Overlapped IO
  c - Use non blocking pipes using IOCompletion ports

第一个选项是最简单的,而且听起来你正在做的事情,它会很好地扩展.这是一个简单示例的链接到(a)http://msdn.microsoft.com/en-us/library/aa365588 ( VS.85 ) .aspx

4-确保您的编码在两侧都匹配.例如,如果您的C++代码是为Unicode编译的,则必须使用Unicode编码在C#端读取管道流.有些事情如下.

using (StreamReader rdr = new StreamReader(pipe, System.Text.Encoding.Unicode))
{
  System.Console.WriteLine(rdr.ReadToEnd());
}
Run Code Online (Sandbox Code Playgroud)

更新:由于我没有在C#中使用它,我想我会写一个小测试.只是使用阻塞管道没有线程或任何东西,只是为了确认基础工作,这是非常粗略的测试代码.

C++服务器

#include <tchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
  HANDLE hPipe = ::CreateNamedPipe(_T("\\\\.\\pipe\\HyperPipe"),
    PIPE_ACCESS_DUPLEX,
    PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
    PIPE_UNLIMITED_INSTANCES,
    4096,
    4096,
    0,
    NULL);


  ConnectNamedPipe(hPipe, NULL);

  LPTSTR data = _T("Hello");
  DWORD bytesWritten = 0;
  WriteFile(hPipe, data, _tcslen(data) * sizeof(TCHAR), &bytesWritten, NULL);
  CloseHandle(hPipe);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

C#客户端

using System;
using System.Text;
using System.IO;
using System.IO.Pipes;

namespace CSPipe
{
  class Program
  {
    static void Main(string[] args)
    {
      NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
      pipe.Connect();
      using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
      {
        System.Console.WriteLine(rdr.ReadToEnd());
      }

      Console.ReadKey();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)