自訂匿名管道類2

來源:互聯網
上載者:User

//---------------------------------------------------------------------------
/*
Module : ANONYMOUSPIPE.H
Purpose: Defines the interface for an MFC wrapper classe for Win32 Anonymous Pipes
Created: LICQ / 10-26-2007
History: None
Copyright (c) 2007 by LICQ.
All rights reserved.
*/
//---------------------------------------------------------------------------

#ifndef AnonyPipeUnitH
#define AnonyPipeUnitH

#include <assert.h>
#include <windows.h>
//---------------------------------------------------------------------------
class CAnonymousPipe
{
public:
      //Constructors / Destructors
      __fastcall CAnonymousPipe()
      {
          m_hWrite = INVALID_HANDLE_VALUE;
          m_hRead = INVALID_HANDLE_VALUE;
      }
      virtual __fastcall ~CAnonymousPipe()
      {
          Close();
      }

      //Creation & Opening
      BOOL __fastcall Create(LPSECURITY_ATTRIBUTES lpPipeAttributes = NULL, DWORD dwSize = 0)
      {
          assert(!IsOpen());

          BOOL bSuccess = ::CreatePipe(&m_hRead, &m_hWrite, lpPipeAttributes, dwSize);
          return bSuccess;
      }

      //General functions
      BOOL __fastcall Write(LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite, DWORD &dwNumberOfBytesWritten)
      {
          assert(!IsOpen());

          if (!WriteFile(m_hWrite, lpBuffer, dwNumberOfBytesToWrite, &dwNumberOfBytesWritten, NULL))
              return FALSE;
          return TRUE;
      }

      BOOL __fastcall Read(LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, DWORD &dwNumberOfBytesRead)
      {
          assert(!IsOpen());

          if (!ReadFile(m_hRead, lpBuffer, dwNumberOfBytesToRead, &dwNumberOfBytesRead, NULL))
              return FALSE;
          return TRUE;
      }

      BOOL __fastcall Close()
      {
          BOOL bSuccess = TRUE;
          if (IsOpen())
          {
              BOOL bSuccess1 = CloseHandle(m_hWrite);
              BOOL bSuccess2 = CloseHandle(m_hRead);
              bSuccess = bSuccess1 && bSuccess2;
          }
          return bSuccess;
      }

      //State accessors
      BOOL __fastcall IsOpen() const
      {
          return (m_hWrite != INVALID_HANDLE_VALUE);
      }

public:
      HANDLE m_hWrite;
      HANDLE m_hRead;
};
//---------------------------------------------------------------------------
#endif
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.