2010-04-23 2 views
5

나는 .dll을 C++로 작성 했으므로 명명 된 파이프를 사용하여 C# 응용 프로그램과 통신하려고합니다.명명 된 파이프를 사용하여 C++ .dll과 C# 응용 프로그램간에 메시지를 보내는 방법은 무엇입니까?

이제 C# 응용 프로그램에 내장 된 System.IO.Pipe .net 클래스를 사용하고 있으며 C++의 일반 함수를 사용하고 있습니다.

나는 C++에서 많은 경험을하지 못했다. (읽기 : 이것은 나의 첫 번째 C++ 코드이다.), 나는 C#에서 경험했다.

서버와 클라이언트와의 연결이 작동하고있는 것 같지만 메시지가 전송되지 않는 유일한 문제입니다. .dll 서버, C# 응용 프로그램 서버, 파이프 방향 InOut (이중)을 만들려고했으나 아무 것도 작동하지 않는 것 같습니다.

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++ 코드가 유니 코드 용으로 컴파일 된 경우 유니 코드 인코딩을 사용하여 C# 측에서 파이프 스트림을 읽어야합니다. 다음과 같은 것이 있습니다.

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

업데이트는 : 나는 C#에서이 함께 일하지 않았다 때문에 나는 작은 테스트를 작성 거라 생각 했어요. 단지 threading이나 아무 것도 안되는 블로킹 파이프를 사용하여, 기본적인 작업을 확인하기 위해, 여기에 매우 거친 테스트 코드가 있습니다.

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

오 남자, 당신은 굵게 C++에서 pipenames의 차이와 C#을 넣어해야합니다. 나는 당신의 답을 찾을 때까지이 문제를 해결하는데 시간 낭비했다. –

+0

@BrianFairservice - 완료 :). 이 게시물이 도움이되어 기쁩니다! –

관련 문제