How to get the length of IStream? C++
I'm creating an IStream as follow:
IStream* stream;
result = CreateStreamOnHGlobal(0, TRUE, &stream);
Then I have a CImage ob开发者_JAVA技巧ject 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!
IStream::Stat should do what you want.
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);
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, ¤t);
精彩评论