Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get en exe default icon

How to get en exe default icon

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
26 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kharfax
    wrote on last edited by
    #1

    Hi people Im filling a CListCtrl with a lot of exe files, and I would like to show the .exe icon next to its name. I tried the following : hMod = LoadLibraryEx(path, NULL, LOAD_LIBRARY_AS_DATAFILE); if(hMod != NULL) { imageIndex = m_imageList.Add(LoadIcon(hMod, MAKEINTRESOURCE(1))); Well, that works just in some cases, but doesn't in a lot. Does anyone knows what can I do to load the default icon of each exe? Shouldnt be so hard because explorer.exe open the icon always... Thanks in advance :D

    D N M 4 Replies Last reply
    0
    • K Kharfax

      Hi people Im filling a CListCtrl with a lot of exe files, and I would like to show the .exe icon next to its name. I tried the following : hMod = LoadLibraryEx(path, NULL, LOAD_LIBRARY_AS_DATAFILE); if(hMod != NULL) { imageIndex = m_imageList.Add(LoadIcon(hMod, MAKEINTRESOURCE(1))); Well, that works just in some cases, but doesn't in a lot. Does anyone knows what can I do to load the default icon of each exe? Shouldnt be so hard because explorer.exe open the icon always... Thanks in advance :D

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Have you tried ExtractIcon()?


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      K 2 Replies Last reply
      0
      • D David Crow

        Have you tried ExtractIcon()?


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        K Offline
        K Offline
        Kharfax
        wrote on last edited by
        #3

        Awesome, just what I needed, thanks a lot David :D

        1 Reply Last reply
        0
        • D David Crow

          Have you tried ExtractIcon()?


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          K Offline
          K Offline
          Kharfax
          wrote on last edited by
          #4

          Hey, I tried with extractIcon, but I should still pass as parameter the resource number. I tried 1 and 0 but still fails to load an icon for a lot of apps, like firefox, winword etc. Or sometimes the icon is not the one that the .exe uses normally :S What can I do?? Thanks Sebastian

          D 1 Reply Last reply
          0
          • K Kharfax

            Hey, I tried with extractIcon, but I should still pass as parameter the resource number. I tried 1 and 0 but still fails to load an icon for a lot of apps, like firefox, winword etc. Or sometimes the icon is not the one that the .exe uses normally :S What can I do?? Thanks Sebastian

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Kharfax wrote:

            ...but I should still pass as parameter the resource number.

            What's wrong with that?

            Kharfax wrote:

            I tried 1 and 0 but still fails to load an icon for a lot of apps, like firefox, winword etc.

            Winword.exe does not have an icon 0. It does have an icon 1, however. Have you tried ExtractIconEx()?


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            K 1 Reply Last reply
            0
            • D David Crow

              Kharfax wrote:

              ...but I should still pass as parameter the resource number.

              What's wrong with that?

              Kharfax wrote:

              I tried 1 and 0 but still fails to load an icon for a lot of apps, like firefox, winword etc.

              Winword.exe does not have an icon 0. It does have an icon 1, however. Have you tried ExtractIconEx()?


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              K Offline
              K Offline
              Kharfax
              wrote on last edited by
              #6

              Whats wrong with that is that I should know wich one of all the icons in a file is the default one. The one showed by windows explorer when you see a file. Using ExtractIconEx I will get a list of all the icons in the file, but I still need to identify wich one is the default one. The value I should pass to ExtractIcon is a zero based index array of icons, thats why I tried with 0 and 1.

              J D 2 Replies Last reply
              0
              • K Kharfax

                Whats wrong with that is that I should know wich one of all the icons in a file is the default one. The one showed by windows explorer when you see a file. Using ExtractIconEx I will get a list of all the icons in the file, but I still need to identify wich one is the default one. The value I should pass to ExtractIcon is a zero based index array of icons, thats why I tried with 0 and 1.

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                FWIW, I believe that the "Default" icon is the first one found in the resources.  Barring any more elegant solution, you could always just loop over a few low values to try to find the first icon in the executable.  For example, you could always search for values "0" through "100", and stop with the first one found...    You could also try to load the resource information directly from the executable to locate the first one.    I do not suppose that using IDI_APPLICATION with LoadIcon(...) works for you?    Peace!

                -=- James


                If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                DeleteFXPFiles & CheckFavorites (Please rate this post!)

                T 1 Reply Last reply
                0
                • K Kharfax

                  Whats wrong with that is that I should know wich one of all the icons in a file is the default one. The one showed by windows explorer when you see a file. Using ExtractIconEx I will get a list of all the icons in the file, but I still need to identify wich one is the default one. The value I should pass to ExtractIcon is a zero based index array of icons, thats why I tried with 0 and 1.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Kharfax wrote:

                  Whats wrong with that is that I should know wich one of all the icons in a file is the default one.

                  There is no default per se.

                  Kharfax wrote:

                  The one showed by windows explorer when you see a file.

                  You can change Windows Explorer to show whatever icon you wish.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  K 1 Reply Last reply
                  0
                  • J James R Twine

                    FWIW, I believe that the "Default" icon is the first one found in the resources.  Barring any more elegant solution, you could always just loop over a few low values to try to find the first icon in the executable.  For example, you could always search for values "0" through "100", and stop with the first one found...    You could also try to load the resource information directly from the executable to locate the first one.    I do not suppose that using IDI_APPLICATION with LoadIcon(...) works for you?    Peace!

                    -=- James


                    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    DeleteFXPFiles & CheckFavorites (Please rate this post!)

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    i think it may also have to deal with the icon windows associates to a type (but i conceed, it doesn't concern the .exes).


                    TOXCCT >>> GEII power

                    [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

                    1 Reply Last reply
                    0
                    • D David Crow

                      Kharfax wrote:

                      Whats wrong with that is that I should know wich one of all the icons in a file is the default one.

                      There is no default per se.

                      Kharfax wrote:

                      The one showed by windows explorer when you see a file.

                      You can change Windows Explorer to show whatever icon you wish.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      K Offline
                      K Offline
                      Kharfax
                      wrote on last edited by
                      #10

                      Ok granted maybe you can change the icon the explorer choses. But is choosing and icon, that icon is the same in every machine. There must be a way to know wich one is the "default" icon of an app. Maybe ExtractIcon isn't the way. I either dont want to start fucking around with the PE for something as simple as this... I'll find it sooner or later...

                      K 1 Reply Last reply
                      0
                      • K Kharfax

                        Ok granted maybe you can change the icon the explorer choses. But is choosing and icon, that icon is the same in every machine. There must be a way to know wich one is the "default" icon of an app. Maybe ExtractIcon isn't the way. I either dont want to start fucking around with the PE for something as simple as this... I'll find it sooner or later...

                        K Offline
                        K Offline
                        Kharfax
                        wrote on last edited by
                        #11

                        ok, the idea is this. You get the system image list, asociate it with the control, and then call SHFILEINFO sfi; memset(&sfi, 0, sizeof(SHFILEINFO)); SHGetFileInfo(sFilename, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); return sfi.iIcon; The return the index of the icon you should use. But, doesn't work fine yet, I'm still getting changed icons. For example... Firefox.exe gets a folder icon...

                        D 1 Reply Last reply
                        0
                        • K Kharfax

                          ok, the idea is this. You get the system image list, asociate it with the control, and then call SHFILEINFO sfi; memset(&sfi, 0, sizeof(SHFILEINFO)); SHGetFileInfo(sFilename, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); return sfi.iIcon; The return the index of the icon you should use. But, doesn't work fine yet, I'm still getting changed icons. For example... Firefox.exe gets a folder icon...

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Kharfax wrote:

                          The return the index of the icon...

                          Are you sure? It should return the handle to an image list that contains the small icon images.


                          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                          "Judge not by the eye but by the heart." - Native American Proverb

                          K 1 Reply Last reply
                          0
                          • D David Crow

                            Kharfax wrote:

                            The return the index of the icon...

                            Are you sure? It should return the handle to an image list that contains the small icon images.


                            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                            "Judge not by the eye but by the heart." - Native American Proverb

                            K Offline
                            K Offline
                            Kharfax
                            wrote on last edited by
                            #13

                            SHFILEINFO sfi; memset(&sfi, 0, sizeof(SHFILEINFO)); SHGetFileInfo(sFilename, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); return sfi.iIcon; Look at the parameters: SHGFI_SYSICONINDEX, icon index in system image list SHGFI_SMALLICON, specifies that is a small icon the one I want http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shgetfileinfo.asp

                            H 1 Reply Last reply
                            0
                            • K Kharfax

                              Hi people Im filling a CListCtrl with a lot of exe files, and I would like to show the .exe icon next to its name. I tried the following : hMod = LoadLibraryEx(path, NULL, LOAD_LIBRARY_AS_DATAFILE); if(hMod != NULL) { imageIndex = m_imageList.Add(LoadIcon(hMod, MAKEINTRESOURCE(1))); Well, that works just in some cases, but doesn't in a lot. Does anyone knows what can I do to load the default icon of each exe? Shouldnt be so hard because explorer.exe open the icon always... Thanks in advance :D

                              N Offline
                              N Offline
                              Nisamudheen
                              wrote on last edited by
                              #14

                              Try http://codeproject.com/win32/IconExtraction.asp

                              K 1 Reply Last reply
                              0
                              • K Kharfax

                                SHFILEINFO sfi; memset(&sfi, 0, sizeof(SHFILEINFO)); SHGetFileInfo(sFilename, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); return sfi.iIcon; Look at the parameters: SHGFI_SYSICONINDEX, icon index in system image list SHGFI_SMALLICON, specifies that is a small icon the one I want http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shgetfileinfo.asp

                                H Offline
                                H Offline
                                Hamid Taebi
                                wrote on last edited by
                                #15

                                if you run this code what happens?


                                WhiteSky


                                K 1 Reply Last reply
                                0
                                • H Hamid Taebi

                                  if you run this code what happens?


                                  WhiteSky


                                  K Offline
                                  K Offline
                                  Kharfax
                                  wrote on last edited by
                                  #16

                                  I told what it does a few post up. It returns the index of the icon in the system image list. Anyway, that doesn't work well and that are just chunks of code.

                                  1 Reply Last reply
                                  0
                                  • N Nisamudheen

                                    Try http://codeproject.com/win32/IconExtraction.asp

                                    K Offline
                                    K Offline
                                    Kharfax
                                    wrote on last edited by
                                    #17

                                    Thanks dude, that makes just what I need, but saving the icon as a file :S I need to get a resource ID or a HICON so I can add it to the list. I tried to adapt it, but I cant make it work. Maybe I just lost my patience and focus :(

                                    1 Reply Last reply
                                    0
                                    • K Kharfax

                                      Hi people Im filling a CListCtrl with a lot of exe files, and I would like to show the .exe icon next to its name. I tried the following : hMod = LoadLibraryEx(path, NULL, LOAD_LIBRARY_AS_DATAFILE); if(hMod != NULL) { imageIndex = m_imageList.Add(LoadIcon(hMod, MAKEINTRESOURCE(1))); Well, that works just in some cases, but doesn't in a lot. Does anyone knows what can I do to load the default icon of each exe? Shouldnt be so hard because explorer.exe open the icon always... Thanks in advance :D

                                      M Offline
                                      M Offline
                                      Mark Salsbery
                                      wrote on last edited by
                                      #18

                                      I use this to extract an icon (from any file, not just EXEs). This will get the default icon from Windows if none is in the exe. HICON hIcon = 0; // Try to extract an icon from the file itself SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(pszPathname, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SMALLICON)) { hIcon = shfi.hIcon; } // If no icon yet, get icon associated with the file type from the shell if (hIcon == 0) { LPTSTR pszExtension = PathFindExtension(pszPathname); CString DummyFileName; DummyFileName = _T("dummyfile"); if (pszExtension[0] == _T('.')) DummyFileName += pszExtension; else DummyFileName += _T("."); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(DummyFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)) { hIcon = shfi.hIcon; *pRetImgFlags |= RECORDIMGDATAFLAG_ICONFROMSHELL; } } if (hIcon) { ... Will that work? Mark

                                      1 Reply Last reply
                                      0
                                      • K Kharfax

                                        Hi people Im filling a CListCtrl with a lot of exe files, and I would like to show the .exe icon next to its name. I tried the following : hMod = LoadLibraryEx(path, NULL, LOAD_LIBRARY_AS_DATAFILE); if(hMod != NULL) { imageIndex = m_imageList.Add(LoadIcon(hMod, MAKEINTRESOURCE(1))); Well, that works just in some cases, but doesn't in a lot. Does anyone knows what can I do to load the default icon of each exe? Shouldnt be so hard because explorer.exe open the icon always... Thanks in advance :D

                                        M Offline
                                        M Offline
                                        Mark Salsbery
                                        wrote on last edited by
                                        #19

                                        I use this to get an icon for any file. This will get the default exe icon if none found in the exe itself: HICON hIcon = 0; // Try to extract an icon from the file itself SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(pszPathname, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SMALLICON)) { hIcon = shfi.hIcon; } // If no icon yet, get icon associated with the file type from the shell if (hIcon == 0) { LPTSTR pszExtension = PathFindExtension(pszPathname); CString DummyFileName; DummyFileName = _T("dummyfile"); if (pszExtension[0] == _T('.')) DummyFileName += pszExtension; else DummyFileName += _T("."); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(DummyFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)) { hIcon = shfi.hIcon; *pRetImgFlags |= RECORDIMGDATAFLAG_ICONFROMSHELL; } } if (hIcon) { ... } Is that what you're looking for? Mark

                                        M 1 Reply Last reply
                                        0
                                        • M Mark Salsbery

                                          I use this to get an icon for any file. This will get the default exe icon if none found in the exe itself: HICON hIcon = 0; // Try to extract an icon from the file itself SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(pszPathname, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SMALLICON)) { hIcon = shfi.hIcon; } // If no icon yet, get icon associated with the file type from the shell if (hIcon == 0) { LPTSTR pszExtension = PathFindExtension(pszPathname); CString DummyFileName; DummyFileName = _T("dummyfile"); if (pszExtension[0] == _T('.')) DummyFileName += pszExtension; else DummyFileName += _T("."); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(DummyFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)) { hIcon = shfi.hIcon; *pRetImgFlags |= RECORDIMGDATAFLAG_ICONFROMSHELL; } } if (hIcon) { ... } Is that what you're looking for? Mark

                                          M Offline
                                          M Offline
                                          Mark Salsbery
                                          wrote on last edited by
                                          #20

                                          Sorry about the double post. I got an error the first time. I should have checked to see if it posted first :) Mark

                                          K 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups