How to get en exe default icon
-
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...
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
-
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
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
-
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 -
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
if you run this code what happens?
WhiteSky
-
if you run this code what happens?
WhiteSky
-
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 :DI 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 -
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 :DI 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 -
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? MarkSorry about the double post. I got an error the first time. I should have checked to see if it posted first :) Mark
-
Sorry about the double post. I got an error the first time. I should have checked to see if it posted first :) Mark
Thanks mate, and no problem about the double post. Yeap that works but I don't know why i have a lot of apps wich fail loading the icon :( I post my code, maybe someone sees what im doing wrong
do { imageIndex = 0; keyNameLenght = 256; res = RegEnumKeyEx(phkResult, dwIndex, keyName, &keyNameLenght, NULL, NULL, NULL, NULL); if(res == ERROR_SUCCESS) { // Tomo icono // Busco Path en registro sprintf(lpSubKey2,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s\\", keyName); result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpSubKey2,NULL,KEY_ALL_ACCESS,&phkResult2); if (ERROR_SUCCESS == result) { //Levanto Path if (ERROR_SUCCESS == RegQueryValue(phkResult2, NULL, path, &largoBuffer)) { CString tup = path; HICON temp = NULL; HMODULE hMod = NULL; int res = 0; WORD pis = 0; tup.Remove('"'); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(tup,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON | SHGFI_SMALLICON)) { temp = shfi.hIcon; imageIndex = m_imageList.Add(temp); } else { LPTSTR pszExtension = PathFindExtension(tup); CString DummyFileName; DummyFileName = _T("dummyfile"); if (pszExtension[0] == _T('.')) DummyFileName += pszExtension; else DummyFileName += _T("."); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(keyName,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)) { temp = shfi.hIcon; } if(temp != NULL) { imageIndex = m_imageList.Add(temp); } else { imageIndex = 0; } } } //Agregar Elemento lvi.mask = LVIF_TEXT | LVIF_IMAGE; lvi.iItem = 0; lvi.iSubItem = 0; lvi.pszText = keyName; lvi.iImage = imageIndex; m_listOrigin.InsertItem(&lvi); m_listOrigin.SetItem(&lvi); } } dwIndex++; }while(res != ERROR_NO_MORE_ITEMS);
-
Thanks mate, and no problem about the double post. Yeap that works but I don't know why i have a lot of apps wich fail loading the icon :( I post my code, maybe someone sees what im doing wrong
do { imageIndex = 0; keyNameLenght = 256; res = RegEnumKeyEx(phkResult, dwIndex, keyName, &keyNameLenght, NULL, NULL, NULL, NULL); if(res == ERROR_SUCCESS) { // Tomo icono // Busco Path en registro sprintf(lpSubKey2,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s\\", keyName); result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpSubKey2,NULL,KEY_ALL_ACCESS,&phkResult2); if (ERROR_SUCCESS == result) { //Levanto Path if (ERROR_SUCCESS == RegQueryValue(phkResult2, NULL, path, &largoBuffer)) { CString tup = path; HICON temp = NULL; HMODULE hMod = NULL; int res = 0; WORD pis = 0; tup.Remove('"'); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(tup,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON | SHGFI_SMALLICON)) { temp = shfi.hIcon; imageIndex = m_imageList.Add(temp); } else { LPTSTR pszExtension = PathFindExtension(tup); CString DummyFileName; DummyFileName = _T("dummyfile"); if (pszExtension[0] == _T('.')) DummyFileName += pszExtension; else DummyFileName += _T("."); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(keyName,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)) { temp = shfi.hIcon; } if(temp != NULL) { imageIndex = m_imageList.Add(temp); } else { imageIndex = 0; } } } //Agregar Elemento lvi.mask = LVIF_TEXT | LVIF_IMAGE; lvi.iItem = 0; lvi.iSubItem = 0; lvi.pszText = keyName; lvi.iImage = imageIndex; m_listOrigin.InsertItem(&lvi); m_listOrigin.SetItem(&lvi); } } dwIndex++; }while(res != ERROR_NO_MORE_ITEMS);
>>...but I don't know why i have a lot of apps wich fail loading the icon What do you mean? EXEs without icons? Are you just getting the default EXE icon or none at all? Mark
-
Thanks mate, and no problem about the double post. Yeap that works but I don't know why i have a lot of apps wich fail loading the icon :( I post my code, maybe someone sees what im doing wrong
do { imageIndex = 0; keyNameLenght = 256; res = RegEnumKeyEx(phkResult, dwIndex, keyName, &keyNameLenght, NULL, NULL, NULL, NULL); if(res == ERROR_SUCCESS) { // Tomo icono // Busco Path en registro sprintf(lpSubKey2,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s\\", keyName); result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpSubKey2,NULL,KEY_ALL_ACCESS,&phkResult2); if (ERROR_SUCCESS == result) { //Levanto Path if (ERROR_SUCCESS == RegQueryValue(phkResult2, NULL, path, &largoBuffer)) { CString tup = path; HICON temp = NULL; HMODULE hMod = NULL; int res = 0; WORD pis = 0; tup.Remove('"'); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(tup,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON | SHGFI_SMALLICON)) { temp = shfi.hIcon; imageIndex = m_imageList.Add(temp); } else { LPTSTR pszExtension = PathFindExtension(tup); CString DummyFileName; DummyFileName = _T("dummyfile"); if (pszExtension[0] == _T('.')) DummyFileName += pszExtension; else DummyFileName += _T("."); SHFILEINFO shfi; memset(&shfi, 0, sizeof(shfi)); if (SHGetFileInfo(keyName,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)) { temp = shfi.hIcon; } if(temp != NULL) { imageIndex = m_imageList.Add(temp); } else { imageIndex = 0; } } } //Agregar Elemento lvi.mask = LVIF_TEXT | LVIF_IMAGE; lvi.iItem = 0; lvi.iSubItem = 0; lvi.pszText = keyName; lvi.iImage = imageIndex; m_listOrigin.InsertItem(&lvi); m_listOrigin.SetItem(&lvi); } } dwIndex++; }while(res != ERROR_NO_MORE_ITEMS);
>>...but I don't know why i have a lot of apps wich fail loading the icon What do you mean? EXEs without icons? Are you just getting the default EXE icon or none at all? By the way, don't forget to use DestroyIcon() on the returned HICON after you add it to your image list :) Mark
-
>>...but I don't know why i have a lot of apps wich fail loading the icon What do you mean? EXEs without icons? Are you just getting the default EXE icon or none at all? By the way, don't forget to use DestroyIcon() on the returned HICON after you add it to your image list :) Mark
First thanks to all the people that helped me with that :D The problem wasn't the technique used to get the icon, the problem was that when I used RegQueryValue I didn't re initialized the variable with the buffer lenght :sigh: So a lot of entries were corrupt and that's why some exe didn't get an icon
-
First thanks to all the people that helped me with that :D The problem wasn't the technique used to get the icon, the problem was that when I used RegQueryValue I didn't re initialized the variable with the buffer lenght :sigh: So a lot of entries were corrupt and that's why some exe didn't get an icon
Cool! And remember, don't forget to use DestroyIcon() on the returned HICON after you add it to your image list :) Mark
-
Cool! And remember, don't forget to use DestroyIcon() on the returned HICON after you add it to your image list :) Mark