I just had a really tough time making Named Pipes work between c++ and .NET. I had no problems creating Named Pipes that worked between 2 c++ apps, or between 2 .NET apps.
Asked
Active
Viewed 3,161 times
3
-
More details please. Where does it fail? – Seva Alekseyev Feb 10 '11 at 18:35
-
Do you see a particular error? – Stephen Cleary Feb 10 '11 at 18:36
-
Wow, fast responses. Sorry guys I planning to post the answer to the problem right after asking it (to help other people), but got distracted and it took longer than expected to answer it. Thanks for your concern. – Solx Feb 10 '11 at 18:54
-
Welcome to Stack Overflow! The average time between asking a question and getting an answer that turns out to be correct, is about six minutes. – Greg Hewgill Feb 10 '11 at 18:59
2 Answers
2
I dont have problem with this communication, i use this scenario in some project.
C++ side:
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\pipename");
CHAR chReadBuf[1024];
DWORD cbRead;
BOOL fResult;
fResult = CallNamedPipe(
lpszPipename, // pipe name
_Message, // message to server
strlen(_Message), // message length
chReadBuf, // buffer to receive reply
sizeof(chReadBuf), // size of read buffer
&cbRead, // number of bytes read
NMPWAIT_WAIT_FOREVER); // wait;-)
At C# side:
public string GetMessageFromPipe()
{
int _lenght = 0;
/*
* Pipe Control Block
*/
_pipeserver.WaitForConnection();
do
{
_lenght += _pipeserver.Read(_buffer, _lenght, _buffer.Length);
}
while (!_pipeserver.IsMessageComplete);
_pipeserver.Disconnect();
/*
* End of Pipe Control Block
*/
if (_lenght == 0)
{
throw new ArgumentException("Message is empty ;-(");
}
return Encoding.UTF8.GetString(_buffer, 0, _lenght);
}
Pipe Creation:
_pipeserver = new NamedPipeServerStream("pipename",
PipeDirection.InOut, 254, PipeTransmissionMode.Message,
PipeOptions.Asynchronous, 262144, 262144);

Svisstack
- 16,203
- 6
- 66
- 100
-
This works, and please see my answer as well. The issue was that the pipe name string is different in .NET and c++. – Solx Feb 10 '11 at 18:50
-
1
I found that I could use ProcessExplorer to see the names of the pipes that I was opening. Even though I used the exact same name in both c++ and .NET, ProcessExplorer showed that they were different.
I ended up using these names: In .NET: "\\.\pipe\XXXDebug"
In c++: "\\.\pipe\pipe\XXXDebug"
What I saw in ProcessExplorer for both of these was: \Device\NamedPipe\pipe\XXXDebug
One more thing, I opened the pipe in .NET via:
NamedPipeServerStream pipe = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte);
And I opened the pipe in c++ via:
g_hPipe = CreateFile(
_pipeName,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

Solx
- 4,611
- 5
- 26
- 29
-
All this time, all it was...was an extra pipe prefix...SERIOUSLY M$?! SERIOUSLY?! – Alexandru Jan 13 '14 at 20:19
-