1
我想通過兩臺Windows pc之間的命名管道發送消息。 當地撥打CreateNamedPipe
時,一切正常。如果我將主機名從"\\\\.\\pipe\\testpipename"
更改爲"\\\\myHostname\\pipe\\testpipename"
,我從getLastError()
得到ERROR_INVALID_NAME(123)
。C++命名管道通過網絡無效名稱錯誤
這是我的代碼:
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;
我的猜測是,pipename是無效的,但我不知道爲什麼。有任何想法嗎?
您是否嘗試過使用IP地址而不是主機名? – fsp
@ user3549596是的,發生同樣的錯誤。 – Strizzi
可能聽起來很愚蠢,但管道名是否正確,並且是服務器上允許的端口445入站? – fsp