Streams and the IStream Interface

If you have a stream object, you can manipulate it through the IStream interface. Streams are always located under a root storage or a substorage object. Streams grow automatically (in 512-byte increments) as you write to them. An MFC class for streams, COleStreamFile, makes a stream look like a CFile object. That class won't be of much use to us in this chapter, however.

Once you have a pointer to IStream, a number of functions are available to you for manipulating the stream. Here is a list of all the IStream functions:

HRESULT CopyTo(IStream** pStm, ULARGE_INTEGER cb, …);

Copies cb bytes from this stream to the named stream. ULARGE_INTEGER is a structure with two 32-bit members—HighPart and LowPart.

HRESULT Clone(IStream** ppStm);

Creates a new stream object with its own seek pointer that references the bytes in this stream. The bytes are not copied, so changes in one stream are visible in the other.

HRESULT Commit(…);

Transactions are not currently implemented for streams.

HRESULT Read(void const* pv, ULONG cb, ULONG* pcbRead);

Tries to read cb bytes from this stream into the buffer pointed to by pv. The variable pcbRead indicates how many bytes were actually read.

DWORD Release(void);

Closes this stream.

HRESULT Revert(void);

Has no effect for streams.

HRESULT Seek(LARGE_INTEGER dlibMove,
DWORD dwOrigin, ULARGE_INTEGER* NewPosition);

Seeks to the specified position in this stream. The dwOrigin parameter specifies the origin of the offset defined in the NewPosition parameter.

HRESULT SetSize(ULARGE_INTEGER libNewSize);

Extends or truncates a stream. Streams grow automatically as they are written, but calling SetSize can optimize performance.

HRESULT Stat(STATSTG* pStatstg, DWORD flag);

Fills in the STATSTG structure with useful information about the stream, including the stream name and size. The size is useful if you need to allocate memory for a read.

HRESULT Write(void const* pv, ULONG cb, ULONG* pcbWritten);

Tries to write cb bytes to this stream from the buffer pointed to by pv. The variable pcbWritten indicates how many bytes were actually written.

IStream Programming

Here is some sample code that creates a stream under a given storage object and writes some bytes from m_buffer to the stream:

 extern IStorage* pStg; IStream* pStream; ULONG nBytesWritten; if (pStg->CreateStream(L"MyStreamName",     STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,     0, 0, &pStream) == S_OK) {     ASSERT(pStream != NULL);     pStream->Write(m_buffer, m_nLength, &nBytesWritten);     pStream->Release(); } 


Programming Visual C++
Advanced 3ds max 5 Modeling & Animating
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 331
Authors: Boris Kulagin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net