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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 3
    [`FindResource`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-findresourcea) -> [`LoadResource`](https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadresource) -> [`LockResource`](https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-lockresource)/[`SizeOfResource`](https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-sizeofresource) -> [`SHCreateMemStream`](https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-shcreatememstream). – IInspectable Mar 27 '23 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](https://devblogs.microsoft.com/oldnewthing/20120720-00/?p=7083)). You'll have to reconstruct the icon file format in memory (say, in a `std::vector`), and then create a stream over that. Unfortunate. – IInspectable Mar 27 '23 at 18:28
  • @IInspectable A shame. Think I'll stick with external ICO files. All beyond me! – Andrew Truckle Mar 27 '23 at 19:06
  • 1
    It's not super complicated, just tedious (though I don't know whether I'll have to take that back once [PNG icons](https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473) are involved). I'll have a look how complicated this will be in a bit. – IInspectable Mar 27 '23 at 19:35
  • @IInspectable Thanks. The ICO files do have PNG images. – Andrew Truckle Mar 27 '23 at 19:54
  • 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. – IInspectable Mar 29 '23 at 14:22
  • @IInspectable I only use them for the context menu on my WebView2 browser control. So I don’t mind them being data resources. – Andrew Truckle Mar 29 '23 at 14:25
  • 1
    Or rather, a [user-defined resource](https://learn.microsoft.com/en-us/windows/win32/menurc/user-defined-resource), as that lets you specify a file to include. The `typeID` can be any unique string, such as `"RawIconData"`, preferable `#define`d in *resource.h* so that both the .rc file and source code agree on the same name. – IInspectable Mar 29 '23 at 14:39

0 Answers0