gpt4 book ai didi

c++ - GLE 997 :Overlapped I/O operation is in progress

转载 作者:行者123 更新时间:2023-11-28 08:27:01 25 4
gpt4 key购买 nike

这篇文章与我之前的 Post 有关.在那里,我将 PipeServer 和 Client 创建为两个单独的程序,并尝试写入服务器并获得响应。程序将进入无限循环。

因此为了简单起见,我将客户端和服务器合并为一个程序并运行。现在我得到 System Error Code 997 即:重叠 I/O 操作正在进行中。尝试了不同的参数选项,无法找出主要原因。知道如何处理此错误代码。

代码片段如下:

#include "stdafx.h"
//#include "WindowService.h"
#include "iostream"
#include "fstream"
using namespace std;

#define BUFSIZE 512

SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);

BOOL InstallService();
BOOL DeleteService();

cWindowsService m_WindowsService;

int main()
{

HANDLE hPipe;
LPTSTR lpszPipename;

LPTSTR lpszWrite = TEXT("Default message from client");

TCHAR chReadBuf[BUFSIZE];


lpszPipename = TEXT("\\\\.\\pipe\\1stPipe");

BOOL fSuccess;
DWORD cbRead, dwMode;

OVERLAPPED m_OverLaped;
HANDLE hEvent;
HANDLE hPipeC;
//HANDLE hPipe;

hPipe=CreateNamedPipe(
lpszPipename,PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,
PIPE_UNLIMITED_INSTANCES,BUFSIZE,
BUFSIZE,0,NULL
);


//hEvent=CreateEvent(NULL,TRUE,TRUE,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;


if (hPipe != INVALID_HANDLE_VALUE)

if (GetLastError() != ERROR_PIPE_BUSY)
{
if(GetLastError()==0)
{
printf("Successfully Created %s\n",lpszPipename);
}
else
printf( TEXT("Can not Create NamedPipe\n[GLE=%d]\n"), GetLastError() );
//return -1;
}



ConnectNamedPipe(hPipe,&m_OverLaped);

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

printf("GLE=%d.\n",GetLastError());

if (!fSuccess && (GetLastError() != ERROR_MORE_DATA))
{
printf("TransactNamedPipe failed with GLE=%d.\n",GetLastError());
return 0;
}


while(1)
{
printf(TEXT("%s\n"), chReadBuf);

// Break if TransactNamedPipe or ReadFile is successful
if(fSuccess)
break;

DWORD nBytesTransfered=1000;
DWORD *lpBytesTransfered;
lpBytesTransfered=&nBytesTransfered;

WaitForSingleObject( m_OverLaped.hEvent,15000);
GetOverlappedResult(hPipe,&m_OverLaped,lpBytesTransfered,true);
// Read from the pipe if there is more data in the message.
fSuccess = ReadFile(
hPipe, // pipe handle
chReadBuf, // buffer to receive reply
BUFSIZE*sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
&m_OverLaped); // not overlapped

// Exit if an error other than ERROR_MORE_DATA occurs.
if( !fSuccess && (GetLastError() != ERROR_MORE_DATA))
break;
else printf( TEXT("%s\n"), chReadBuf);
}



}

最佳答案

MSDN 有一个示例可以完成您想要做的事情,我建议您检查一下:Named Pipe ServerNamed Pipe Client (从评论中添加)。

另一个建议是,您首先尝试不使用重叠 IO,因为它会使事情变得有点复杂,然后一旦您在没有重叠 IO 的情况下开始工作,您就可以再次引入它。

关于c++ - GLE 997 :Overlapped I/O operation is in progress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664816/

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