共用方式為


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 位元組,並將其複製到呼叫端提供的緩衝區。 已讀取的位元組數目會傳回於 HTTPRead 參數中。 方法不會在到達檔案結尾時傳回錯誤碼,因此應用程式應該在方法傳回之後檢查 在 azureRead 中的值。

這個方法是同步方法。 它會封鎖讀取作業直到讀取作業完成為止。

實作注意事項:這個方法應該藉由將讀取的位元組數目新增至目前位置,以更新數據流中的目前位置。讀取的位元組數目,這是由 在 azureRead 參數中傳回的值所指定。 其他可更新目前位置的方法包括 ReadWriteBeginWriteSeekSetCurrentPosition

如果已安裝 Windows Media Format 11 SDK 可轉散發元件,此介面可在下列平臺上使用:

  • Windows XP with Service Pack 2 (SP2) 和更新版本。
  • 已安裝 Windows XP Media Center Edition 2005 KB900325 (Windows XP Media Center Edition 2005) 和 KB925766 (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