{"id":9,"date":"2007-06-08T14:16:05","date_gmt":"2007-06-08T18:16:05","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=9"},"modified":"2007-06-16T02:41:17","modified_gmt":"2007-06-16T06:41:17","slug":"creating-hicon-directly-from-memory","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=9","title":{"rendered":"Creating HICON Directly from Memory"},"content":{"rendered":"<p><strong><a href=\"http:\/\/www.ragestorm.net\/blogs\/?p=12\">[updated]<\/a><\/strong>\u00a0<\/p>\n<p>\u00a0How many times you had to do something trivial and you knew there ought to be a decent solution out there?<\/p>\n<p>That&#8217;s another common problem that I smashed my head against the wall and came up with a surprising simple solution. For example you might archive icons in a single file, or you download them from the Internet to a buffer in memory. And then you want to display them. That sounds an easy task ah? But most people will just write the memory block back to a temporary file and only then will use LoadImage to load the icon from the disk. That&#8217;s lame and hits performance big time. To spare you with the research I had done to solve it, here&#8217;s the code:<\/p>\n<pre>\r\nHICON createIcon(PBYTE iconData, int iconSize)     \r\n\r\n{     \r\n\r\n\u00a0  HICON hIcon = NULL;     \r\n\r\n\u00a0  \/\/ Ahhh, this is the magic API.     \r\n\r\n\u00a0  int offset = LookupIconIdFromDirectoryEx(iconData, TRUE, iconSize, iconSize, LR_DEFAULTCOLOR);     \r\n\r\n\u00a0  if (offset != 0) {     \r\n\r\n\u00a0     hIcon = CreateIconFromResourceEx(iconData + offset, 0, TRUE, 0x30000, iconSize, iconSize, LR_DEFAULTCOLOR);     \r\n\r\n\u00a0  }     \r\n\r\n\u00a0  return hIcon;     \r\n\r\n}<\/pre>\n<p>You should read the documenation about LookupIconIdFromDirectoryEx, it says the call should be wrapped with SEH&#8230;for malformed files. Probably you ask yourself why I treat the ID as an offset. But that&#8217;s because if you look at the structures of ICON in-file and in-memory you&#8217;ll notice that&#8217;s the same field. So it fits for our purpose here and gives us the offset to the single icon&#8217;s data rather than the whole directory as it is saved in file or resource data.<\/p>\n<p>Of course, you have to call DestroyIcon when you&#8217;re done messing with the icon&#8230;<\/p>\n<p>\u00a0For more information about icons format and Win32 implementation and relevant API,\u00a0 check <a target=\"_blank\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms997538.aspx\">this<\/a>\u00a0out. It really helped me in understanding how it all works. Funny that it even hints about that LookupIconIdFromDirectoryEx. ;) Too late.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[updated]\u00a0 \u00a0How many times you had to do something trivial and you knew there ought to be a decent solution out there? That&#8217;s another common problem that I smashed my head against the wall and came up with a surprising simple solution. For example you might archive icons in a single file, or you download [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[7],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-9","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/9"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}