{"id":123,"date":"2010-02-25T15:03:30","date_gmt":"2010-02-25T09:33:30","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=123"},"modified":"2011-01-26T15:03:56","modified_gmt":"2011-01-26T09:33:56","slug":"extracting-icon-from-an-executable-name","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2010\/02\/25\/extracting-icon-from-an-executable-name\/","title":{"rendered":"Extracting Icon from an executable (name)"},"content":{"rendered":"<p style=\"text-align: justify;\">Remember the <a href=\"http:\/\/geekswithblogs.net\/TechTwaddle\/archive\/2009\/05\/26\/applications-creating-a-simple-ui-application-from-scratch-process-viewer-again.aspx\">Process Viewer<\/a> that we worked on a few months back? I was wondering how to display the applications icons, the way task manager does. A small icon beside the application name gives it a pleasant look. So how do you extract the icon from an executable? Well, there&#8217;s an API for that (pun). <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa922154.aspx\">ExtractIconEx()<\/a> is the function we use to get a handle to the applications icon. It&#8217;s a simple API, just give it the full path to the executable and it returns an HICON (icon handle). So I went back to my Process Viewer and added a static label to display the icon. Here is the code:<\/p>\n<p><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">HICON hIcon, hIc;<br \/>\nhIcon = ExtractIconEx(filename, 0, NULL, &amp;hIc, 1);<\/p>\n<p>if(!hIcon)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));<br \/>\n}<\/p>\n<p>SendMessage(g_hStaticIcon, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);<\/span><\/span><br \/>\n&nbsp;<\/p>\n<p>If the handle returned is NULL <em>(for applications without user interface)<\/em> then we display a default icon, the terminal. After getting the icon handle, we set it on the label using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa930092.aspx\"><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">STM_SETIMAGE<\/span><\/span><\/a> message.<\/p>\n<p style=\"text-align: justify;\">Now to the interesting part. An application can contain quite many icons as resources, so how does the system know which icon to return? The answer is that the system chooses the icon whose resource identifier value is the smallest and returns it as the application icon. So if I have three icons in my application whose identifiers are defined as: <em>(in Resourceppc.h)<\/em><\/p>\n<p><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">#define IDI_ICON1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 103<br \/>\n#define IDI_ICON2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 104<br \/>\n#define IDI_ICON3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 105<\/span><\/span><\/p>\n<p>then the resource <span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">IDI_ICON1<\/span><\/span> will be returned.<\/p>\n<p style=\"text-align: justify;\">Here is another interesting bit, the static label (in the video below) was created using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931018.aspx\"><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">CreateWindow()<\/span><\/span><\/a> as follows:<\/p>\n<p><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">g_hStaticIcon = CreateWindowEx(0, L&quot;STATIC&quot;, NULL, WS_CHILD | WS_VISIBLE | SS_ICON |<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">SS_REALSIZEIMAGE, x, y, 16, 16, hWndParent, 0, g_hInst, NULL);<\/span><\/span><br \/>\n&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now a static image label will expand itself if the image being displayed on it is bigger than the label size. This is what you wouldn&#8217;t want, imagine some application icons displayed in 16&#215;16 pixels while others in 32&#215;32. So to prevent this automatic scaling of the label we use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931018.aspx\"><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">SS_REALSIZEIMAGE<\/span><\/span><\/a> flag in the <span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">CreateWindow()<\/span><\/span> call. But <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931018.aspx\">according to MSDN<\/a>, <span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">SS_REALSIZEIMAGE<\/span><\/span> is listed under &quot;<span style=\"color: rgb(0, 51, 0);\"><span xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"><span id=\"src124\" class=\"srcSentence\">Unsupported static control styles<\/span><\/span><\/span><span xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"><span id=\"src124\" class=\"srcSentence\">&quot;.<br \/>\n<\/span><\/span><\/p>\n<p style=\"text-align: justify;\"><span xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"><span class=\"srcSentence\">If you have seen the video below already, then you&#8217;ll notice that the <strong>gwes.exe<\/strong> icon appears larger. There could be two reasons, either all other application&#8217;s icons are getting scaled down to 16&#215;16 and gwes.exe&#8217;s isn&#8217;t <em>(wonder why)<\/em>, or there is no scaling happening and all other app<\/span><\/span>lications are returning 16&#215;16 icons while gwes is returning a larger 32&#215;32 icon <em>(unlikely)<\/em>. Now if I remove the <span style=\"color: rgb(0, 0, 128);\"><span style=\"font-family: Verdana;\">SS_REALSIZEIMAGE<\/span><\/span> flag while creating the label then all the icons are displayed big (32&#215;32)! This means that scaling down is indeed happening and <span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\">SS_REALSIZEIMAGE<\/span><\/span>, although listed under &#8216;<span style=\"color: rgb(0, 51, 0);\">Unsupported static control styles<\/span>&#8216;, still works. I am a little confused as to why this is happening, if you got a clue then do let know.<\/p>\n<p>Here&#8217;s the video: &nbsp;<\/p>\n<p><object width=\"425\" height=\"344\"><param value=\"http:\/\/www.youtube.com\/v\/AN7nlylJwNo&amp;hl=en_US&amp;fs=1&amp;\" name=\"movie\" \/><param value=\"true\" name=\"allowFullScreen\" \/><param value=\"always\" name=\"allowscriptaccess\" \/><\/object><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Displaying the icons like I did above is just the proof. Next step would be to display the icons right beside the application name <em>(who needs serial number!)<\/em>, got to look at list views and images. And hopefully the image scaling problem should resolve itself with list_view images.<\/p>\n<p style=\"text-align: justify;\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Remember the Process Viewer that we worked on a few months back? I was wondering how to display the applications icons, the way task manager does. A small icon beside the application name gives it a pleasant look. So how do you extract the icon from an executable? Well, there&#8217;s an API for that (pun). &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2010\/02\/25\/extracting-icon-from-an-executable-name\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Extracting Icon from an executable (name)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-1Z","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/123\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}