I've been trying to get string representation of HICON. I was able to get the HICON from here. I'm able to save it to a file using this answer. I know I can save to to a temp file and read and get the bytes, but I want to directly convert the stream into byte array. When I do a IStream::Read
or IStream_Read
, I consistently get Zero bytes. Can you tell me what I'm doing wrong or point me to some documentation on the same?
The following is the code that I've been working on:
std::wstring get_str(HICON hIcon) {
// Create the IPicture intrface
PICTDESC desc = {sizeof(PICTDESC)};
desc.picType = PICTYPE_ICON;
desc.icon.hicon = hIcon;
IPicture *pPicture = 0;
HRESULT hr = OleCreatePictureIndirect(&desc, IID_IPicture, FALSE, (void **)&pPicture);
if (FAILED(hr)) return L"Erorr creating empty pic";
// Create a stream and save the image
IStream *pStream = 0;
// Use SHCreateMemStream?
// https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-shcreatememstream
CreateStreamOnHGlobal(0, TRUE, &pStream);
LONG cbSize = 0;
hr = pPicture->SaveAsFile(pStream, TRUE, &cbSize);
// Write the stream content to the file
if (!FAILED(hr)) {
HGLOBAL hBuf = 0;
std::wstring ret;
char buffer[4096];
hr = IStream_Read(pStream, &buffer, cbSize);
if (FAILED(hr))
return L"Uneven reading from buffer"; // + std::to_wstring(cbSize) + L" " + std::to_wstring(read_size);
return L"Success"; // std::wstring(buffer);
}
// Cleanup
pStream->Release();
pPicture->Release();
return L"Fail fail fail";
}