3

Win32 MFC

I can load an icon stream (to use with WebView2) from a physical ICO file on my hard drive. Eg:

wil::com_ptr<IStream> iconStreamPdfSettings;
CHECK_FAILURE(SHCreateStreamOnFileEx(
    L"d:\\Icons\\settings.ico", STGM_READ, FILE_ATTRIBUTE_NORMAL, FALSE,
    nullptr, &iconStreamPdfSettings));

I would like to add the ICO file as a resource in my executable. If I do that, how can I turn the embedded resource ICO into one of these wil::com_ptr<IStream> objects?

8
  • 3
    FindResource -> LoadResource -> LockResource/SizeOfResource -> SHCreateMemStream. Commented Mar 27, 2023 at 16:16
  • 1
    Uh, that could have been easy. As it turns out, it isn't quite that simple: The binary structure of icon group resources is ever so slightly different from the icon file format. Plus, the individual images are split out into individual icon resources (see The format of icon resources). You'll have to reconstruct the icon file format in memory (say, in a std::vector), and then create a stream over that. Unfortunate. Commented Mar 27, 2023 at 18:28
  • 1
    It's not super complicated, just tedious (though I don't know whether I'll have to take that back once PNG icons are involved). I'll have a look how complicated this will be in a bit. Commented Mar 27, 2023 at 19:35
  • 1
    In the meantime, here's another idea: If you add the icon files as raw binary (RT_RCDATA) resources rather than actual icons, the steps from my first comment will work. If you aren't otherwise using those icons in your application then that may be a convenient way to go about it. Commented Mar 29, 2023 at 14:22
  • 1
    Or rather, a user-defined resource, as that lets you specify a file to include. The typeID can be any unique string, such as "RawIconData", preferable #defined in resource.h so that both the .rc file and source code agree on the same name. Commented Mar 29, 2023 at 14:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.