Reputation: 2234
I have a Namedpipeserver written largely based on:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx
When I try to connect to it from a .NET client using this code:
NamedPipeClientStream clientPipe = new NamedPipeClientStream(".",
"\\\\.\\pipe\\TTCUIHELPER_SEND_TTC_RECEIVE",PipeDirection.Out);
try
{
if (clientPipe != null)
{
clientPipe.Connect(5000);
if (clientPipe.IsConnected == true)
{
byte[] bytes = pm.GetMessageData();
clientPipe.Write(bytes, 0, bytes.Length);
clientPipe.Flush();
clientPipe.Dispose();
clientPipe.Close();
}
}
}
catch (Exception Ex)
{
System.Windows.MessageBox.Show(Ex.Message);
}
Then the connect always times out. The weird thing is if I try to write a test client using CPP as so:
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\TTCUIHELPER_SEND_TTC_RECEIVE");
const TCHAR* lpvMessage=L"QQQQ";
HANDLE hPipe = CreateFile(
lpszPipename, // 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
// Break if the pipe handle is valid.
DWORD cbWritten;
DWORD cbToWrite = (lstrlen(lpvMessage)+1)*sizeof(TCHAR);
_tprintf( TEXT("Sending %d byte message: \"%s\"\n"), cbToWrite, lpvMessage);
BOOL fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped
if ( ! fSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}
Then that works flawlessly. What could I be doing wrong that it works from CPP but not .NET?
Upvotes: 3
Views: 1823
Reputation: 12135
Just lose the pipe prefix from your pipe name i.e. use just the name itself:
"TTCUIHELPER_SEND_TTC_RECEIVE"
The .NET NamedPipeClientStream
class adds the prefix for you when it calls the Windows WriteFile API internally.
Upvotes: 2