简体   繁体   English

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

[英]Not getting any Response from Named Pipe Server

I have created a NamedPipe inside a Windows Service and starting the Service Manually or as the System Starts up. 我已经在Windows服务中创建了一个NamedPipe并手动或在系统启动时启动了该服务。

EDIT: 编辑:

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);

Now I want to Access the Named Pipe, Write some Message and Response back. 现在,我要访问命名管道,写一些消息和响应。

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 

I have ommited the Error Checking Codes to be make it readable here. 我已省略了错误检查代码,以使其在此处易于阅读。 I get stuck for long(infinite may be ) time while executing TransactNamedPipe . 我在执行TransactNamedPipe时卡住了很长时间(可能无限)。 I must be setting some parameters wrong , but I have tried the options as specified at MSDN. 我必须将某些参数设置为错误,但是我尝试了MSDN中指定的选项。

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

Since the pipe is created with FILE_FLAG_OVERLAPPED flag, you must pass LPOVERLAPPED parameter to every pipe I/O call (including TransactNamedPipe). 由于管道是使用FILE_FLAG_OVERLAPPED标志创建的,因此必须将LPOVERLAPPED参数传递给每个管道I / O调用(包括TransactNamedPipe)。 If function returns FALSE and GetLastError returns ERROR_IO_PENDING, wait for m_OverLaped.hEvent - when it is set, operation is completed. 如果函数返回FALSE,而GetLastError返回ERROR_IO_PENDING,则等待m_OverLaped.hEvent-设置后,操作完成。

For starters 对于初学者

m_OverLaped.hEvent=hPipe;

Is wrong, hEvent needs to be set to the event you've created, not the pipe. 是错误的,需要将hEvent设置为您创建的事件,而不是管道。 Before you do the read you need to call: 阅读之前,您需要致电:

WaitForSingleObject( oOverlap.hEvent, 

and then: 接着:

GetOverlappedResult()

Have you got the pipe working in non-overlapped mode? 您是否使管道在非重叠模式下工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM