[英]C++ Named Pipes over network invalid name error
I am trying to send messages via named pipes between two Windows pc´s. 我试图通过命名管道在两个Windows PC之间发送消息。 When calling
CreateNamedPipe
locally everything works fine. 在本地调用
CreateNamedPipe
,一切正常。 If I change the hostname from "\\\\\\\\.\\\\pipe\\\\testpipename"
to "\\\\\\\\myHostname\\\\pipe\\\\testpipename"
I get an ERROR_INVALID_NAME(123)
from getLastError()
. 如果我将主机名从
"\\\\\\\\.\\\\pipe\\\\testpipename"
更改为"\\\\\\\\myHostname\\\\pipe\\\\testpipename"
则会从getLastError()
获取ERROR_INVALID_NAME(123)
getLastError()
。
This is my code: 这是我的代码:
BOOL fConnected = FALSE;
DWORD dwThreadId = 0;
HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
LPTSTR pipeName = /*nullptr*/ TEXT("\\\\myHostname\\pipe\\testpipename");
SECURITY_ATTRIBUTES sa = { 0 };
SECURITY_DESCRIPTOR sd = { 0 };
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
sa.bInheritHandle = false;
sa.lpSecurityDescriptor = &sd;
sa.nLength = sizeof(sa);
hPipe = CreateNamedPipe(
pipeName, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
255, // output buffer size
255, // input buffer size
0, // client time-out
&sa); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
cout << GetLastError();
return -2;
}
cout << "Waiting for client to connect!" << endl;
//waiting for client to connect
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
cout << "Client connected! YEAH" << endl;
My guess is that the pipename is invalid but I don´t know why. 我的猜测是,pipename无效,但我不知道为什么。 Any Ideas?
有任何想法吗?
Problem solved! 问题解决了! Pipenames for Server and Client are:
服务器和客户端的Pipenames是:
Server:
"\\\\\\\\.\\\\pipe\\\\testpipe"
服务器:
"\\\\\\\\.\\\\pipe\\\\testpipe"
Client:"\\\\\\\\serverHostName\\\\pipe\\\\testpipe"
客户端:
"\\\\\\\\serverHostName\\\\pipe\\\\testpipe"
Some minor changes at the client were also made. 客户也做了一些小的改动。 Full code can be found at my Github repo .
完整的代码可以在我的Github回购中找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.