Click here to Skip to main content
15,907,001 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.8K   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

 
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 
GeneralRe: remove this crap article from Cp. Thanks. Pin
KarstenK7-Feb-03 3:10
mveKarstenK7-Feb-03 3:10 
GeneralRe: remove this crap article from Cp. Thanks. Pin
.:floyd:.11-Feb-04 1:45
.:floyd:.11-Feb-04 1:45 
KarstenK wrote:
1. class instead of struct

Why? What's the difference between
class C {
public:
  ...
};
and
struct S {
  ...
};
? Other than the fact that the latter is shorter I fail to see a difference.

Although I wouldn't rate this article as crap the provided code has some poor spots -- hardcoding drive C:\ into code that is used by clients is simply a no-no. What happens if it runs on a machine that doesn't have a drive C:\ ? Using GetWindowsDirectory would certainly be wise here.

.f

p.s.: A #pragma warning( disable : nnn ) in a header file is ok, as long as the previous compiler state is restored at the end of the respective header. Disabling warning 4089 is severe as it is, invalidating most of C++'s strong typing. Failing to restore the warning state, especially in this case, is pretty much as bad as it gets. If the original author feels the need that (s)he can't leave this warning untouched (aren't there static_cast<...> and reinterpret_cast<...> in C++?) (s)he should at least embrace the modified warning settings in #pragma warning( push ) and #pragma warning( pop ) compiler directives.

Other bad things: strings being cast to LPCTSTR, yet failing to make sure that it also works with UNICODE defined, by applying a simple _T( "..." ) macro.

All class members could be made static and both c'tor and d'tor hidden by making them private to disable instantiating this class -- after all, there is nothing to be gained by creating an instance to be able to call it's members.

The bottom line: This wrapper class isn't poor because it doesn't do much, but simply for the reason of being crafted in the most sloppy way possible. So I would have to agree with .S.Rod. who started this thread, although I would have like to have seen this stated in a more constructive criticism.
GeneralRe: remove this crap article from Cp. Thanks. Pin
KarstenK11-Feb-04 2:59
mveKarstenK11-Feb-04 2:59 
GeneralRe: remove this crap article from Cp. Thanks. Pin
.:floyd:.11-Feb-04 3:15
.:floyd:.11-Feb-04 3:15 
GeneralRe: remove this crap article from Cp. Thanks. Pin
Jörgen Sigvardsson7-Feb-03 3:46
Jörgen Sigvardsson7-Feb-03 3:46 
GeneralRe: remove this crap article from Cp. Thanks. Pin
Anna-Jayne Metcalfe7-Feb-03 2:30
Anna-Jayne Metcalfe7-Feb-03 2:30 
GeneralRe: remove this crap article from Cp. Thanks. Pin
Michael P Butler7-Feb-03 2:49
Michael P Butler7-Feb-03 2:49 
GeneralRe: remove this crap article from Cp. Thanks. Pin
#realJSOP7-Feb-03 3:07
professional#realJSOP7-Feb-03 3:07 
GeneralRe: remove this crap article from Cp. Thanks. Pin
Paul Vickery7-Feb-03 3:09
professionalPaul Vickery7-Feb-03 3:09 

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.