Click here to Skip to main content
15,881,204 members
Articles / Desktop Programming / MFC
Article

An ShGetFileInfo Wrapper Class

Rate me:
Please Sign up or sign in to vote.
4.50/5 (33 votes)
6 Feb 20031 min read 150.1K   3.5K   34   34
A class built around the use of ShGetFileInfo() function.

Sample Image

Introduction

The CUseShGetFileInfo class is built around the use of ShGetFileInfo() function. It's a very simple class, but I think it can be usefull. You will not have to worry anymore about setting flags and it also works for names of files that do not exist. You don't need to know the full path for a certain file in order to obtain its icon, file type or its icon's index in the system's image list (both large icons and small icons). It also provides a function for obtaining a handle to the system's image list.

User functions

Use the GetFileIconIndex function to obtain the index of the file's associated icon in the system's image list.

int CUseShGetFileInfo::GetFileIconIndex( CString strFileName , BOOL bSmallIcon )

Use the GetDirIconIndex function to obtain the index of the folder's associated icon in the system's image list.

int CUseShGetFileInfo::GetDirIconIndex( BOOL bSmallIcon )

Use the GetFileIconHandle function to obtain a handle to the file's associated icon in the system's image list.

HICON CUseShGetFileInfo::GetFileIconHandle( CString strFileName, BOOL bSmallIcon )

Use the GetFolderIconHandle function to obtain a handle to the folder's associated icon in the system's image list.

HICON CUseShGetFileInfo::GetFolderIconHandle( BOOL bSmallIcon )

Use the GetFileType function to obtain a file's type (e.g. Winamp media file).

CString CUseShGetFileInfo::GetFileType( CString strFileName)

Use the GetSystemImageListHandle function to obtain a handle to the system's image list (large icons or small icons). Use this function to create a CImageList object to attach to a list control. After this image list is attached to the list control, use GetFileIconIndex or GetDirIconIndex to obtain the value to set for the nImage field (e.g. CListCtrl::InsertItem() function)

HIMAGELIST CUseShGetFileInfo::GetSystemImageListHandle( BOOL bSmallIcon )

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Romania Romania
Still a student at a computer science related faculty in the University of Craiova, Romania :Frown | :(
Hope that one day I'll graduate! Smile | :) ). Programming for last 8 years (Visual C++ for the last 2 years).

Comments and Discussions

 
QuestionLicense Pin
Member 108669435-Jun-14 7:04
Member 108669435-Jun-14 7:04 
Question#pragma warning (disable : 4089) Pin
Rui Frazao18-May-12 6:42
Rui Frazao18-May-12 6:42 
GeneralMy vote of 5 Pin
User 74293383-Nov-11 9:07
professionalUser 74293383-Nov-11 9:07 
Questionhow to get the icon name in the system list? Pin
xylh20032-Aug-10 23:50
xylh20032-Aug-10 23:50 
QuestionHow to Find a larger icon than "large icon" on this program? Pin
Mehdi Ghiasi24-Jan-09 5:26
Mehdi Ghiasi24-Jan-09 5:26 
Question.exe Icon Pin
nns200929-Dec-08 1:57
nns200929-Dec-08 1:57 
GeneralThis is good! Pin
vbnmjkkhgffd2-Apr-07 19:59
vbnmjkkhgffd2-Apr-07 19:59 
AnswerFILE_ATTRIBUTE_NORMAL Pin
mikeT100018-Jun-07 23:36
mikeT100018-Jun-07 23:36 
GeneralRe: FILE_ATTRIBUTE_NORMAL Pin
vbnmjkkhgffd19-Jun-07 15:25
vbnmjkkhgffd19-Jun-07 15:25 
GeneralRe: FILE_ATTRIBUTE_NORMAL Pin
Tristan Burtenshaw15-Feb-10 18:13
Tristan Burtenshaw15-Feb-10 18:13 
Generali love it Pin
i_a_z28-Nov-06 2:32
i_a_z28-Nov-06 2:32 
GeneralLarge Icon problem on Win XP Pin
Dave_isit12124-Apr-04 22:16
Dave_isit12124-Apr-04 22:16 
GeneralSuggested improvements Pin
.:floyd:.12-Feb-04 0:57
.:floyd:.12-Feb-04 0:57 
UseShGetFileInfo.h:
...
#pragma warning( push )  // better yet to not change it at all
#pragma warning (disable : 4089)

class CUseShGetFileInfo{
private:
   CUseShGetFileInfo();   // prevent instantiation
public:
   // virtual ~CUseShGetFileInfo();   // not needed
   
   //get the system's image list
   static HIMAGELIST GetSystemImageListHandle( BOOL bSmallIcon );
   
   //get the image's index in the system's image list
   static int GetFileIconIndex( CString strFileName , BOOL bSmallIcon);
   static int GetDirIconIndex(BOOL bSmallIcon);

   //get a handle to the icon
   static HICON GetFileIconHandle(CString strFileName,BOOL bSmallIcon);
   static HICON GetFolderIconHandle(BOOL bSmallIcon );

   //get file type
   static CString GetFileType(CString strFileName);
};

#pragma warning( pop )   // see above

UseShGetFileInfo.cpp:
#include <shlwapi.h>   // path manipulation functions
#pragma comment( lib, "shlwapi" )   // link with respective library
#include <objbase.h>
#if defined( _MT )
  CoInitializeEx( NULL, COINIT_MULTITHREADED );
#else
  CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
#endif  // defined( _MT )
HIMAGELIST  CUseShGetFileInfo::GetSystemImageListHandle( BOOL bSmallIcon )
{
   TCHAR tcSystemRoot[MAX_PATH + 1];
   GetWindowsDirectory( tcSystemRoot, MAX_PATH );
   PathStripToRoot( tcSystemRoot );
   HIMAGELIST  hSystemImageList;
   SHFILEINFO    ssfi;

   if (bSmallIcon)
   {
      hSystemImageList = (HIMAGELIST)SHGetFileInfo(tcSystemRoot,
         0,
         &ssfi,
         sizeof(SHFILEINFO),
         SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_ICON);   // see MSDN
   }
   else
   {
   ...
}

int CUseShGetFileInfo::GetDirIconIndex(BOOL bSmallIcon )
{
   SHFILEINFO    sfi;
   if (bSmallIcon)
   {
       SHGetFileInfo(
       _T( "" ),   // make it UNICODE compatible -- empty string suffices
       FILE_ATTRIBUTE_DIRECTORY,
       &sfi, 
       sizeof(SHFILEINFO), 
       SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES | SHGFI_ICON);   // see MSDN
   }
   else
   {
   ...
}
Apply respective changes to all other locations. In particular, supply SHGFI_ICON whenever any SHGFI_XXXICON flag is used, supply UNICODE compatible strings, even if they aren't evaluated by content, don't use a hardcoded C:\ drive.

The class cannot be instantiated anymore, which is an intended effect. The methods are available through CUseShGetFileInfo::GetSystemImageListHandle( BOOL bSmallIcon ) calls, etc., without any instance available -- that's what static members are for.

I've probably overlooked some issues, but this is a start anyway. If you have any criticism concerning the changes let's hear them.

.f

p.s.: Is there a way to apply syntax highlighting in these comments?
QuestionHow to save as ico file Pin
Anonymous24-Jun-03 3:42
Anonymous24-Jun-03 3:42 
AnswerRe: How to save as ico file Pin
npg42720-Apr-04 18:46
sussnpg42720-Apr-04 18:46 
GeneralNetwork Adapter Icon Pin
sri7621-May-03 18:21
susssri7621-May-03 18:21 
GeneralI think the article is great Pin
SteveKing7-Feb-03 7:22
SteveKing7-Feb-03 7:22 
GeneralRe: I think the article is great Pin
Diarrhio17-Mar-03 18:01
Diarrhio17-Mar-03 18:01 
GeneralRe: I think the article is great Pin
jaramilr12-Jun-04 14:57
jaramilr12-Jun-04 14:57 
GeneralCome on guys!!! Pin
Mustafa Demirhan7-Feb-03 4:12
Mustafa Demirhan7-Feb-03 4:12 
GeneralRe: Come on guys!!! Pin
Michael P Butler7-Feb-03 4:20
Michael P Butler7-Feb-03 4:20 
GeneralRe: Come on guys!!! Pin
Jeff J9-Feb-03 9:28
Jeff J9-Feb-03 9:28 
Generalremove this crap article from Cp. Thanks. Pin
Stephane Rodriguez.7-Feb-03 2:11
Stephane Rodriguez.7-Feb-03 2:11 
GeneralRe: remove this crap article from Cp. Thanks. Pin
Jörgen Sigvardsson7-Feb-03 2:27
Jörgen Sigvardsson7-Feb-03 2:27 
GeneralRe: remove this crap article from Cp. Thanks. Pin
Stephane Rodriguez.7-Feb-03 2:44
Stephane Rodriguez.7-Feb-03 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.