未从命名管道服务器收到任何响应

发布于 2024-09-17 20:25:31 字数 2764 浏览 4 评论 0原文

我在 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 上指定的选项。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甲如呢乙后呢 2024-09-24 20:25:31
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 - 当它被设置时,操作完成。

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). If function returns FALSE and GetLastError returns ERROR_IO_PENDING, wait for m_OverLaped.hEvent - when it is set, operation is completed.

(り薆情海 2024-09-24 20:25:31

对于初学者来说

m_OverLaped.hEvent=hPipe;

这是错误的,hEvent 需要设置为您创建的事件,而不是管道。在进行读取之前,您需要调用:

WaitForSingleObject( oOverlap.hEvent, 

然后:

GetOverlappedResult()

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

For starters

m_OverLaped.hEvent=hPipe;

Is wrong, hEvent needs to be set to the event you've created, not the pipe. 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文