代码之家  ›  专栏  ›  技术社区  ›  Simsons

未从命名管道服务器获取任何响应

  •  1
  • Simsons  · 技术社区  · 14 年前

    编辑:

    lpszPipename = TEXT("\\\\.\\pipe\\1stPipe"); 
    OVERLAPPED m_OverLaped;
    HANDLE hEvent;
    
    hPipe=CreateNamedPipe (lpszPipename,
                           PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
                           PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,
                           PIPE_UNLIMITED_INSTANCES,BUFSIZE,
                           BUFSIZE,0,NULL);   
    
    m_OverLaped.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
    m_OverLaped.Internal=0;
    m_OverLaped.InternalHigh=0;
    m_OverLaped.Offset=0;
    m_OverLaped.OffsetHigh=0;
    
    ConnectNamedPipe(hPipe,&m_OverLaped);
    

    现在我想访问命名管道,编写一些消息并返回响应。

    LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\1stPipe"); 
    
    OVERLAPPED m_OverLaped;
    m_OverLaped.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
    m_OverLaped.Internal=0;
    m_OverLaped.InternalHigh=0;
    m_OverLaped.Offset=0;
    m_OverLaped.OffsetHigh=0;
    
    hPipe=CreateFile (lpszPipename,            // Gets the Pipename
                      GENERIC_READ | GENERIC_WRITE,// Client only writes to this pipe.
                      0,                       // Do not share this pipe with others.
                      NULL,                    // Do not inherit security.
                      OPEN_EXISTING,           // Pipe must exist.
                      FILE_ATTRIBUTE_NORMAL,   // I have no special requirements on
                                               //file attributes
                      NULL);  
    
    dwMode = PIPE_READMODE_MESSAGE;
    
    fSuccess = SetNamedPipeHandleState (hPipe,    // pipe handle 
                                        &dwMode,  // new pipe mode 
                                        NULL,     // don't set maximum bytes 
                                        NULL);    // don't set maximum time 
    fSuccess = TransactNamedPipe (hPipe,                  // pipe handle 
                                  lpszWrite,              // message to server
                                  (lstrlen(lpszWrite)+1)*sizeof(TCHAR),//message length 
                                  chReadBuf,              // buffer to receive reply
                                  BUFSIZE*sizeof(TCHAR),  // size of read buffer
                                  &cbRead,                // bytes read
                                  &m_OverLaped); 
    fSuccess = ReadFile (hPipe,                 // pipe handle 
                         chReadBuf,             // buffer to receive reply 
                         BUFSIZE*sizeof(TCHAR), // size of buffer 
                         &cbRead,               // number of bytes read 
                         &m_OverLaped);         // overlapped 
    

    我已经推荐了错误检查代码,使它在这里可读。我在执行时被卡住了很长一段时间 TransactNamedPipe . 我一定是设置了一些参数错误,但我已经尝试了MSDN指定的选项。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Alex F    14 年前
    m_OverLaped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
    ...
    ConnectNamedPipe(hPipe, &m_OverLaped);
    

    由于管道是使用FILE\u FLAG\u OVERLAPPED FLAG创建的,因此必须将LPOVERLAPPED参数传递给每个管道I/O调用(包括transact-namedpipe)。如果函数返回FALSE,GetLastError返回ERROR\u IO\u PENDING,请等待m_重叠.hEvent-设置后,操作完成。

        2
  •  2
  •   Patrick    14 年前

    对于初学者

    m_OverLaped.hEvent=hPipe;
    

    如果错误,则需要将hEvent设置为您创建的事件,而不是管道。在阅读之前,您需要致电:

    WaitForSingleObject( oOverlap.hEvent, 
    

    然后:

    GetOverlappedResult()
    

    你有没有让管道在非重叠模式下工作?