-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamOnHGlobal.cpp
More file actions
38 lines (31 loc) · 826 Bytes
/
StreamOnHGlobal.cpp
File metadata and controls
38 lines (31 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "StreamOnHGlobal.h"
CStreamOnHGlobal::CStreamOnHGlobal(HGLOBAL hGlobal) : stream(nullptr) {
if (FAILED(CreateStreamOnHGlobal(hGlobal, true, &stream))) {
stream = nullptr;
}
}
CStreamOnHGlobal::~CStreamOnHGlobal() {
if (stream) {
stream->Release();
stream = nullptr;
}
}
IStream* CStreamOnHGlobal::GetStream() const {
return stream;
}
std::optional<std::vector<uint8_t>> CStreamOnHGlobal::ReadAll() {
STATSTG stat = {};
if (FAILED(stream->Stat(&stat, STATFLAG_NONAME))) {
return std::nullopt;
}
if (FAILED(stream->Seek({}, STREAM_SEEK_SET, nullptr))) {
return std::nullopt;
}
ULONG bytesRead = 0;
std::vector<uint8_t> data(stat.cbSize.QuadPart);
if (FAILED(stream->Read(data.data(), stat.cbSize.QuadPart, &bytesRead))) {
return std::nullopt;
}
data.resize(bytesRead);
return data;
}