IMFByteStream::Read 메서드(mfobjects.h)

스트림에서 데이터를 읽습니다.

구문

HRESULT Read(
  [in]  BYTE  *pb,
  [in]  ULONG cb,
  [out] ULONG *pcbRead
);

매개 변수

[in] pb

데이터를 수신하는 버퍼에 대한 포인터입니다. 호출자는 버퍼를 할당해야 합니다.

[in] cb

버퍼의 크기(바이트)입니다.

[out] pcbRead

버퍼에 복사되는 바이트 수를 받습니다. 이 매개 변수는 NULL일 수 없습니다.

반환 값

메서드가 성공하면 S_OK를 반환하고, 그러지 않으면 HRESULT 오류 코드를 반환합니다.

설명

이 메서드는 스트림의 현재 위치에서 최대 cb 바이트를 읽고 호출자가 제공하는 버퍼에 복사합니다. pcbRead 매개 변수에서 읽은 바이트 수가 반환됩니다. 메서드는 파일 끝에 도달할 때 오류 코드를 반환하지 않으므로 메서드가 반환된 후 애플리케이션이 pcbRead의 값을 검사 합니다.

이 메서드는 동기적이며, 읽기 작업이 완료될 때까지 차단됩니다.

구현 정보:이 메서드는 pcbRead 매개 변수에서 반환된 값으로 지정된 읽은 바이트 수를 현재 위치에 추가하여 스트림의 현재 위치를 업데이트해야 합니다. 현재 위치를 업데이트할 수 있는 다른 방법은 Read, Write, BeginWrite, SeekSetCurrentPosition입니다.

이 인터페이스는 Windows Media Format 11 SDK 재배포 가능 구성 요소가 설치된 경우 다음 플랫폼에서 사용할 수 있습니다.

  • Windows XP SP2(서비스 팩 2) 이상
  • KB900325(Windows XP Media Center Edition 2005) 및 KB925766(Windows XP Media Center Edition용 2006년 10월 업데이트 롤업)가 설치된 Windows XP Media Center Edition 2005.

예제

다음 예제에서는 바이트 스트림에서 호출자가 할당한 미디어 버퍼로 데이터를 읽습니다. 미디어 버퍼에 대한 자세한 내용은 미디어 버퍼를 참조하세요.

// Read data from a byte stream into a media buffer.
//
// This function reads a maximum of cbMax bytes, or up to the size of the 
// buffer, whichever is smaller. If the end of the byte stream is reached, the 
// actual amount of data read might be less than either of these values.
//
// To find out how much data was read, call IMFMediaBuffer::GetCurrentLength.

HRESULT ReadFromByteStream(
    IMFByteStream *pStream,     // Pointer to the byte stream.
    IMFMediaBuffer *pBuffer,    // Pointer to the media buffer.
    DWORD cbMax                 // Maximum amount to read.
    )
{
    DWORD cbBufferMax = 0;
    DWORD cbRead = 0;
    BYTE *pData= NULL;

    HRESULT hr = pBuffer->Lock(&pData, &cbBufferMax, NULL);

    // Do not exceed the maximum size of the buffer.
    if (SUCCEEDED(hr))
    {
        if (cbMax > cbBufferMax)
        {
            cbMax = cbBufferMax;
        }

        // Read up to cbMax bytes.
        hr = pStream->Read(pData, cbMax, &cbRead);
    }

    // Update the size of the valid data in the buffer.
    if (SUCCEEDED(hr))
    {
        hr = pBuffer->SetCurrentLength(cbRead);
    }
    if (pData)
    {
        pBuffer->Unlock();
    }
    return hr;
}

다음 예제는 비슷하지만 데이터를 저장할 새 미디어 버퍼를 할당합니다.

참고 이 예제에서는 SafeRelease 함수를 사용하여 인터페이스 포인터를 해제합니다.
 
//-------------------------------------------------------------------
// AllocReadFromByteStream
//
// Reads data from a byte stream and returns a media buffer that
// contains the data.
//-------------------------------------------------------------------

HRESULT AllocReadFromByteStream(
    IMFByteStream *pStream,         // Pointer to the byte stream.
    DWORD cbToRead,                 // Number of bytes to read.
    IMFMediaBuffer **ppBuffer       // Receives a pointer to the media buffer. 
    )
{
    HRESULT hr = S_OK;
    BYTE *pData = NULL;
    DWORD cbRead = 0;   // Actual amount of data read.

    IMFMediaBuffer *pBuffer = NULL;

    // Create the media buffer. 
    // This function allocates the memory for the buffer.
    hr = MFCreateMemoryBuffer(cbToRead, &pBuffer);

    // Get a pointer to the memory buffer.
    if (SUCCEEDED(hr))
    {
        hr = pBuffer->Lock(&pData, NULL, NULL);
    }

    // Read the data from the byte stream.
    if (SUCCEEDED(hr))
    {
        hr = pStream->Read(pData, cbToRead, &cbRead);
    }

    // Update the size of the valid data in the buffer.
    if (SUCCEEDED(hr))
    {
        hr = pBuffer->SetCurrentLength(cbRead);
    }

    // Return the pointer to the caller.
    if (SUCCEEDED(hr))
    {
        *ppBuffer = pBuffer;
        (*ppBuffer)->AddRef();
    }

    if (pData)
    {
        pBuffer->Unlock();
    }
    SafeRelease(&pBuffer);
    return hr;
}

요구 사항

요구 사항
지원되는 최소 클라이언트 Windows Vista [데스크톱 앱 | UWP 앱]
지원되는 최소 서버 Windows Server 2008 [데스크톱 앱 | UWP 앱]
대상 플랫폼 Windows
헤더 mfobjects.h(Mfidl.h 포함)
라이브러리 Mfuuid.lib

추가 정보

IMFByteStream