代码之家  ›  专栏  ›  技术社区  ›  Simsons

GLE 997:正在进行重叠I/O操作

  •  0
  • Simsons  · 技术社区  · 15 年前

    这篇文章与我以前的文章有关 Post . 在那里,我创建了PipeServer和Client作为两个独立的程序,并试图写入服务器并得到响应回来。然后呢这个节目将无限循环。

    所以为了简单起见,我将客户机和服务器结合到一个程序中并运行。现在我开始 System Error Code 997即: Overlapped I/O operation is in progress

    代码段如下:

    #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); 
           }
    
    
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Andreas Magnusson    15 年前

    MSDN有一个例子,它正是你想做的,我建议你去看看: Named Pipe Server Named Pipe Client