2010-04-23 41 views
5

我正在用C++编写注入的.dll文件,我想使用命名管道与C#应用程序进行通信。如何使用命名管道在C++ .dll和C#应用程序之间发送消息?

现在,我在C#应用程序中使用内置的System.IO.Pipe .net类,并且使用C++中的常规函数​​。我没有太多的C++经验(阅读:这是我的第一个C++代码..),我在C#中经验丰富。

看来,与服务器和客户端的连接正在工作,唯一的问题是messaged没有被发送。我试图让.dll服务器,C#应用程序服务器,使管道方向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); 

} 

和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); 
       } 
      } 
     } 
    } 

我看到连接到.dll的C#客户端,但它不会收到任何消息。 我尝试以相反的方式执行此操作,正如前面所述,并试图使C#将消息发送给.dll。 DLL,它会在消息框中显示它们。 .dll被注入并连接到C#服务器,但是当它收到一条消息时,它刚刚崩溃了它注入的应用程序。

请帮助我,或者指导我如何使用C++和C#应用程序

回答

15

有几件事情之间的命名管道。

1 - 您是否使用相同的管道名称
C++: “\\。\管\ 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#端读取Pipe流。 有些事情如下。

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

更新:由于我没有在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; 
} 

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(); 
    } 
    } 
} 
+0

哦,你应该把pipenames之间在C++和C#中大胆的差异。我浪费了时间解决这个问题,直到我找到了答案。 – 2017-10-10 16:46:37

+0

@BrianFairservice - 完成:)。我很高兴这篇文章有帮助! – 2017-10-12 03:59:05

相关问题