Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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

C#
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:

C#
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++ client:

C++
#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;

}


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
Posted
Updated 4-Jul-16 1:09am
v2
Comments
[no name] 14-Jun-16 19:56pm    
Which examples have you looked at? Have you used the debugger? Why comment out WaitForConnection()? There are examples in the MSDN that DO work - why not use them?
Jochen Arndt 15-Jun-16 2:50am    
In addition to the above:

Check the return values of the IO functions in your C++ program. Upon failures call GetLastError() to get the error code and report it.

The ReadFile() function will block for synchronous IO (non-overlapped as used by your code) until a write operation completes at the write end of the pipe (your C# application) or an error occurs. So it will not return while you do not write anything to the pipe on the server side.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900