2016-06-14 30 views
0

名前付きパイプを使用しているC++プロジェクトにc#プロジェクトを接続しようとしていますが、C++プロジェクトが接続しません。C#名前付きパイプ接続

PS:私はの使用理解していない私のパイプ名の前に「\\を\パイプ\。」:.exeファイルの両方が同じファイルに

側の質問です。それは何をし、本当に必要なのですか?

Program.csの

static class Program 
{ 

    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     pipeHandler pipe = new pipeHandler(); 

     var proc = new Process(); 
     proc.StartInfo.FileName = "cplpltestpipes.exe"; 
     proc.Start(); 


     pipe.establishConnection(); 

     Application.Run(new Form1(pipe)); 
    } 
} 


public class pipeHandler 
{ 

    private StreamReader re; 
    private StreamWriter wr; 
    private NamedPipeServerStream pipeServer; 

    public void establishConnection() 
    { 
     pipeServer = new NamedPipeServerStream("myNamedPipe1"); 
     pipeServer.WaitForConnection(); 
     re = new StreamReader(pipeServer); 
     wr = new StreamWriter(pipeServer); 
    } 

    public void writePipe(string text) 
    { 
     wr.Write(text); 

    } 

    public string readPipe() 
    { 
     if(re.Peek()==-1) 
      System.Threading.Thread.Sleep(2000); 
     if (re.Peek() > -1) 
     { 
      string s; 
      s = re.ReadToEnd(); 
      return s; 
     } 
     else 
      return "fail"; 
    } 


} 

のForm1.cs:

public partial class Form1 : Form 
{ 
    pipeHandler pipePointer; 
    public Form1(pipeHandler pipe) 
    { 
     pipePointer=pipe; 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     pipePointer.writePipe(textBox1.Text); 
     textBox2.Text = pipePointer.readPipe(); 
    } 


} 

C++クライアント

ここに私のコードは、多分あなたは

C#のサーバーのエラーを見つけることができます プログラムを実行する

}

だけのC#

でこの例外を与える**************例外テキスト************ ** System.InvalidOperationException:パイプがまだ接続されていません。 .... .... ....

およびC++では、ちょうどあなたがCreateFileのは、オブジェクトが文字列によって表されることを知っているんかCreateFileを呼び出すと、コンソール

+0

を削除し、私は '表示されていない" \\。\パイプどこでもあなたのコードでは、 '\。 –

+0

私はそれが何を今ないので、それはありませんし、いずれにしても動作しません。 – Awesome

+0

パイプ名の説明については、https://msdn.microsoft.com/en-us/library/windows/desktop/aa365783(v=vs.85).aspxを参照してください。 –

答えて

1

に「しよう」印刷を続けるmyNamedPipe1パイプですか?名前の前に\\ServerName\pipe\という接頭辞が付いていたので、それは分かっています。

ServerNameは、.とすることができます。これは「This machine」のショートカットであるため、コードをLPTSTR pipeName = TEXT("\\\\.\\pipe\\myNamedPipe1");に切り替えると他の問題がなければ動作するはずです。

NamedPipeServerStreamクラスputs it there for youのように、C#コードにそのコードを配置する必要はありません。

EDIT:あなたのコードを見ている、あなたは正しい名前を持っていた場合でも、pipeHandlerのコンストラクタにpipeServer = new NamedPipeServerStream("myNamedPipe1");を移動するには、サーバーがこれまでに開始される前に、今すぐあなたのC++プログラムが起動する場合がありますあなたはまだ可能性がありますエラーを取得します。

EDIT2:ConnectNamedPipepipeServer.WaitForConnection();のC++と同等である、あなたがやってはならないC++プログラムがクライアントである場合に。ここは、C++アプリ

を開始する前に、サーバーを起動するためにC#のアプリを書き換えする方法の例です:あなたは CreateFile

EDIT3からとすぐに有効なハンドルを持っているように読み書き行くために良いことがあります

static class Program { static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //The server now gets created here. pipeHandler pipe = new pipeHandler(); var proc = new Process(); proc.StartInfo.FileName = "cplpltestpipes.exe"; proc.Start(); //The server used to be created here. pipe.EstablishConnection(); Application.Run(new Form1(pipe)); } } public class pipeHandler { private StreamReader re; private StreamWriter wr; private NamedPipeServerStream pipeServer; public pipeHandler() { //We now create the server in the constructor. pipeServer = new NamedPipeServerStream("myNamedPipe1"); } public void establishConnection() { pipeServer.WaitForConnection(); re = new StreamReader(pipeServer); wr = new StreamWriter(pipeServer); } ... } 

その後、あなたのC++のコードが

while (!flag) 
{ 
    flag = ConnectNamedPipe(pipeHandler, NULL); 
    cout << "trying"; 
} 
+0

それ以上の接続エラーはありません、ありがとう!しかし、今、C#アプリは応答を待ってフリーズし、C++アプリは読み込みと応答に失敗します。あなたが寛大な気持ちになれば、残りのコードを見てみることができますか?前もって感謝します! – Awesome

+0

'readPipe()'はsuper weridになります。すべてのピークを取り除き、データを読み取るだけです。それでは、デバッガを使用して、どの行が停止しているのか把握し、新しい質問をしてください。 –

関連する問題