没有从命名管道服务器获得任何响应

时间:2010-09-06 10:53:46

标签: c++ windows named-pipes

我在Windows服务中创建了一个NamedPipe并手动启动服务或在系统启动时启动。

编辑:

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 个答案:

答案 0 :(得分:2)

m_OverLaped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
...
ConnectNamedPipe(hPipe, &m_OverLaped);

由于管道是使用FILE_FLAG_OVERLAPPED标志创建的,因此必须将LPOVERLAPPED参数传递给每个管道I / O调用(包括TransactNamedPipe)。如果函数返回FALSE并且GetLastError返回ERROR_IO_PENDING,则等待m_OverLaped.hEvent - 当它设置时,操作完成。

答案 1 :(得分:1)

首发

m_OverLaped.hEvent=hPipe;

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

WaitForSingleObject( oOverlap.hEvent, 

然后:

GetOverlappedResult()

您是否让管道在非重叠模式下工作?