gpt4 book ai didi

c++ - 未从命名管道服务器获得任何响应

转载 作者:可可西里 更新时间:2023-11-01 11:39:39 25 4
gpt4 key购买 nike

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

最佳答案

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

关于c++ - 未从命名管道服务器获得任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3650876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com