C#C ++命名管道连接错误 [英] C# C++ named pipes connection error

查看:78
本文介绍了C#C ++命名管道连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用命名管道将c#项目与c ++项目连接,但c ++项目无法连接。



ps:.exe都是在同一个文件中



这是我的代码也许你可以发现错误



C#server:< br $>


Program.cs



I am trying to connect a c# project with a c++ project using named pipes but the c++ project doesn't connect.

ps: the .exe are both in the same file

Here's the my code maybe you can spot the error

C# server:

Program.cs

namespace csharptestpipes
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        [STAThread]
        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 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);
        }
        public void writePipe(string text)
        {
            wr.Write(text);
        }

        public string readPipe()
        {
                string s;
                s = re.ReadToEnd();
                return s;
        }

        public void closeConnection()
        {
            pipeServer.Close();
        }
    }
}





Form1.cs:





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

        private void CloseForm(object sender, EventArgs e)
        {
            pipePointer.closeConnection();
       }
    }







C ++客户端:






C++ client:

#define chrSize 16

int main()
{
	TCHAR chr[chrSize];
	DWORD bytesRead;

	HANDLE pipeHandler;
	LPTSTR pipeName = TEXT("\\\\.\\pipe\\myNamedPipe1");

	pipeHandler = CreateFile(
		pipeName,   // pipe name 
		GENERIC_READ |  // read and write access 
		GENERIC_WRITE,
		0,              // no sharing 
		NULL,           // default security attributes
		OPEN_EXISTING,  // opens existing pipe 
		0,              // default attributes 
		NULL);          // no template file 

	ReadFile(
		pipeHandler,    // pipe handle 
		chr,    // buffer to receive reply 
		chrSize * sizeof(TCHAR),  // size of buffer 
		&bytesRead,  // number of bytes read 
		NULL);    // not overlapped 

	cout << "\n"<<chr;

	LPTSTR pipeMessage = TEXT("message receive");
	DWORD bytesToWrite= (lstrlen(pipeMessage) + 1) * sizeof(TCHAR);
	DWORD cbWritten;

	WriteFile(
		pipeHandler,                  // pipe handle 
		pipeMessage,             // message 
		bytesToWrite,              // message length 
		&cbWritten,             // bytes written 
		NULL);                  // not overlapped 
	
	return 0;

}





运行程序只是在C#中给出了这个例外。



**************异常文本************** System.InvalidOperationException:管道尚未连接。 .... .... ....



我尝试了什么:



i查看了多个示例,但我无法弄清楚为什么它无法连接



Running the program just gives this exception in C#

************** Exception Text ************** System.InvalidOperationException: Pipe hasn't been connected yet. .... .... ....

What I have tried:

i looked over multiple examples but i can't figure out why it can't connect

推荐答案

首先我看到命名管道的不同之处名称。 C ++有一个路径,但C#没有。

第二,C#在C ++启动之前没有完全初始化。

第三:需要一些WaitForConnection。检查MSDN的异步版本。



看看来自Microsoft的示例代码。他们有一个非常不同的方法。
At first I see a difference in the named pipe name. The C++ has a path, but the C# doesnt.
At second the C# isnt fully initialized before the C++ starts.
At third: Some WaitForConnection is needed. Check the MSDN for async versions.

Take a look at the example code from Microsoft. They had a really different approach.


这篇关于C#C ++命名管道连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆