本文介绍了未从命名管道服务器获取任何响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a NamedPipe inside a Windows Service and starting the Service Manually or as the System Starts up.

编辑:

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 



我忘了错误检查代码,在执行 TransactNamedPipe 时,我遇到长时间(无限可能)的时间。我必须设置一些参数错误,但我已经尝试了MSDN指定的选项。

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. I must be setting some parameters wrong , but I have tried the options as specified at MSDN.

推荐答案


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 - 设置时,操作完成。

Since the pipe is created with FILE_FLAG_OVERLAPPED flag, you must pass LPOVERLAPPED parameter to every pipe I/O call (including TransactNamedPipe). If function returns FALSE and GetLastError returns ERROR_IO_PENDING, wait for m_OverLaped.hEvent - when it is set, operation is completed.

这篇关于未从命名管道服务器获取任何响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:33