I want to send data from a c# application to a c++ application through a pipe. Here is what I've done:
this is the c++ client:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[]) {
HANDLE hFile;
BOOL flg;
DWORD dwWrite;
char szPipeUpdate[200];
hFile = CreateFile(L"\\\\.\\pipe\\BvrPipe", GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
0, NULL);
strcpy(szPipeUpdate,"Data from Named Pipe client for createnamedpipe");
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD dw = GetLastError();
printf("CreateFile failed for Named Pipe client\n:" );
}
else
{
flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
if (FALSE == flg)
{
printf("WriteFile failed for Named Pipe client\n");
}
else
{
printf("WriteFile succeeded for Named Pipe client\n");
}
CloseHandle(hFile);
}
return 0;
}
and here the c# server
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace PipeApplication1{
class ProgramPipeTest
{
public void ThreadStartServer()
{
// Create a name pipe
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("\\\\.\\pipe\\BvrPipe"))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
// We read a line from the pipe and print it together with the current time
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
Console.WriteLine("Connection lost");
}
static void Main(string[] args)
{
ProgramPipeTest Server = new ProgramPipeTest();
Thread ServerThread = new Thread(Server.ThreadStartServer);
ServerThread.Start();
}
}
}
when I start the server and then the client GetLastErrror from client returns 2 (The system cannot find the file specified.)
Any idea on this. Thanks
C++ is a subset of C as it is developed and takes most of its procedural constructs from the C language. Thus any C program will compile and run fine with the C++ compiler. However, C language does not support object-oriented features of C++ and hence it is not compatible with C++ programs.
Client 1 will tell the intermediate process when data is ready, and send the data to it. The intermediate process will then hold this data until client 2 tells it that it is ready for the data. If the intermediate process has not received new data from client 1, it will tell client 2 to wait.
C and C++ are two closely related programming languages. Therefore, it may not come as a surprise to you that you can actually mix C and C++ code in a single program. However, this doesn't come automatically when you write your code the normal way.
At a guess, I'd say you don't need the "\.\Pipe\" prefix when creating the pipe in the server. The examples of calling the NamedPipeServerStream constructor that I've seen just pass-in the pipe name. E.g.
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("BvrPipe"))
You can list the open pipes, and their names, using SysInternals process explorer. This should help you verify that the pipe has the correct name. See this question for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With