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
{
[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()
{
pipeServer = new NamedPipeServerStream("myNamedPipe1");
}
public void establishConnection()
{
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:
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:
#define chrSize 16
int main()
{
TCHAR chr[chrSize];
DWORD bytesRead;
HANDLE pipeHandler;
LPTSTR pipeName = TEXT("\\\\.\\pipe\\myNamedPipe1");
pipeHandler = CreateFile(
pipeName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
ReadFile(
pipeHandler, chr, chrSize * sizeof(TCHAR), &bytesRead, NULL);
cout << "\n"<<chr;
LPTSTR pipeMessage = TEXT("message receive");
DWORD bytesToWrite= (lstrlen(pipeMessage) + 1) * sizeof(TCHAR);
DWORD cbWritten;
WriteFile(
pipeHandler, pipeMessage, bytesToWrite, &cbWritten, NULL);
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