C ++命名管道网络无效名称错误

时间:2016-12-29 12:49:06

标签: c++ ipc named-pipes

我正在尝试通过两个Windows PC之间的命名管道发送消息。 在本地调用CreateNamedPipe时,一切正常。如果我将主机名从"\\\\.\\pipe\\testpipename"更改为"\\\\myHostname\\pipe\\testpipename",我会从ERROR_INVALID_NAME(123)获得getLastError()

这是我的代码:

    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无效,但我不知道为什么。任何想法?

1 个答案:

答案 0 :(得分:0)

问题解决了! 服务器和客户端的Pipenames是:

  

服务器:"\\\\.\\pipe\\testpipe"
  客户:"\\\\serverHostName\\pipe\\testpipe"

客户也做了一些小的改动。 完整代码可在my Github repo找到。