How to get the length of IStream? C++
Asked Answered
S

3

5

I'm creating an IStream as follow:

IStream* stream;
result = CreateStreamOnHGlobal(0, TRUE, &stream);

Then I have a CImage object that I save to this stream:

image->Save(stream, Gdiplus::ImageFormatBMP);

I need to get the size of bytes written to this IStream.

How can I do this?

There is no Length or something like this in the IStream...

thanks!

Shedevil answered 13/10, 2009 at 16:26 Comment(0)
E
7

IStream::Stat should do what you want.

Encroachment answered 13/10, 2009 at 16:29 Comment(0)
S
5

Or you can use:

    ULARGE_INTEGER liSize;
    IStream_Size(pStream, &liSize);

other functions you might find useful in this context:

    IStream_Reset(pStream);         // reset seek position to beginning
    IStream_Read(pStream, mem, size);
Selfpollination answered 17/9, 2010 at 11:5 Comment(0)
A
-1

Both IStream_Size as well as IStream::Stat can be used to request the size. IStream_Size appears to be a convenience wrapper around IStream::Stat (that's oddly only available as a C COM macro). If that is indeed the case then there's a lot of data queried: An entire STATSTG, optionally without the pwcsName member.

In that case, a less costly way to get the same information would be IStream::Seek:

HRESULT get_size(IStream* stream, ULARGE_INTEGER& size) {
    return IStream->Seek({}, STREAM_SEEK_END, &size);
}

This changes the stream's current read or write pointer. If you need to save the current position you can use the following:

ULARGE_INTEGER current{};
stream->Seek({}, STREAM_SEEK_CUR, &current);
Apophyge answered 1/4, 2022 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.