Reputation: 110
I'm trying to implement the communication between two processes via namedpipes. To be more precise (don't think it effects the question), I want two Matlab instances to communicate with each other.
It works ok so far, but in 10% of all cases, it says that there is another process at the end of the pipe...
My code (shortened) so far (sender):
pipe = CreateNamedPipe(uniquePipeName, // name of the pipe
PIPE_ACCESS_DUPLEX, //
PIPE_TYPE_MESSAGE, // send data as a byte stream
1, // only allow 1 instance of this pipe
0, // no outbound buffer
0, // no inbound buffer
0, // use default wait time
NULL // use default security attributes
);
... some error handling...
result = ConnectNamedPipe(pipe, NULL);
if(!result)
{
printerror()
CloseHandle(pipe);
return;
}
numBytesWritten = 0;
printf("Sending %lg\n", *uPtrs[0]);
result = WriteFile(pipe, // handle to our outbound pipe
uPtrs[0], // data to send
sizeof(double),
&numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO
);
if (!result) {
printerror()
}
CloseHandle(pipe);
And the reciever:
while(true)
{
if (!WaitNamedPipe(uniquePipeName, NMPWAIT_USE_DEFAULT_WAIT))
continue; // timeout, try again
pipe = CreateFile(
uniquePipeName,
GENERIC_READ, // only need read access
0,//FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (pipe != INVALID_HANDLE_VALUE)
break;
else
{
{
if (GetLastError() == ERROR_PIPE_BUSY || GetLastError() == 2)
{
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
printf("%s\n",lpMsgBuf);
CloseHandle(pipe);
if (!WaitNamedPipe(uniquePipeName, NMPWAIT_USE_DEFAULT_WAIT))
continue; // timeout, try again
}
else
{
printf("Failed to connect pipe. Error %.i\n",GetLastError());
y[0] = 0;
return;
}
//system("pause");
}
}
}
printf("Connected!\n");
result = ReadFile(
pipe,
&buffer_in, // the data from the pipe will be put here
sizeof(double),
// 127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes actually read
NULL // not using overlapped IO
);
printf("Recieved: %lg\n",buffer_in);
printf("Recieved: %lg\n", buffer_out);
y[0] = buffer_in;
CloseHandle(pipe);
The error code in case of a failed submission is: GetLastError: 535 : There is a process on other end of the pipe.
As I'm totally new to the whole subjects, any other adivses to improve the core are appreciated.
Thanks a lot!
Upvotes: 2
Views: 2011
Reputation: 21
I know this question is from 2014, but I had the same issue and found the solution. In the Microsoft docs example (https://learn.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server) you can see that they check for the exact error message you get (535), in which case fConnected is set to true and the program continues. So I guess this error is actually a success?
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
Upvotes: 2