Share via


Metodo IMFByteStream::Read (mfobjects.h)

Legge i dati dal flusso.

Sintassi

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

Parametri

[in] pb

Puntatore a un buffer che riceve i dati. Il chiamante deve allocare il buffer.

[in] cb

Dimensione del buffer in byte.

[out] pcbRead

Riceve il numero di byte copiati nel buffer. Questo parametro non può essere NULL.

Valore restituito

Se questo metodo ha esito positivo, restituisce S_OK. In caso contrario, restituisce un codice di errore HRESULT .

Commenti

Questo metodo legge al massimo i byte cb dalla posizione corrente del flusso e li copia nel buffer fornito dal chiamante. Il numero di byte letti viene restituito nel parametro pcbRead . Il metodo non restituisce un codice di errore per raggiungere la fine del file, quindi l'applicazione deve controllare il valore in pcbRead dopo che il metodo restituisce.

Questo metodo è asincrono. Blocca fino al completamento dell'operazione di lettura.

Note sull'implementazione:Questo metodo deve aggiornare la posizione corrente nel flusso aggiungendo il numero di byte letti, specificato dal valore restituito nel parametro pcbRead , alla posizione corrente. Altri metodi che possono aggiornare la posizione corrente sono Read, Write, BeginWrite, Seek e SetCurrentPosition.

Questa interfaccia è disponibile nelle piattaforme seguenti se i componenti ridistribuibili di Windows Media Format 11 SDK sono installati:

  • Windows XP con Service Pack 2 (SP2) e versioni successive.
  • Windows XP Media Center Edition 2005 con KB900325 (Windows XP Media Center Edition 2005) e KB925766 (aggiornamento cumulativo di ottobre 2006 per Windows XP Media Center Edition) installato.

Esempio

Nell'esempio seguente vengono letti dati da un flusso di byte in un buffer multimediale allocato dal chiamante. Per altre informazioni sui buffer multimediali, vedere Buffer multimediali.

// 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;
}

L'esempio successivo è simile, ma alloca un nuovo buffer multimediale per contenere i dati.

Nota In questo esempio viene usata la funzione SafeRelease per rilasciare i puntatori all'interfaccia.
 
//-------------------------------------------------------------------
// 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;
}

Requisiti

Requisito Valore
Client minimo supportato Windows Vista [app desktop | App UWP]
Server minimo supportato Windows Server 2008 [app desktop | App UWP]
Piattaforma di destinazione Windows
Intestazione mfobjects.h (include Mfidl.h)
Libreria Mfuuid.lib

Vedi anche

FMByteStream