How to obtain the name and path of the file containing the icon representing a specific file type or

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
C++ code.
Im trying to retrieve information of all files inside an arbitrary folder. I have a function object that is called for every file found and the full file name with absolute path is passed to the function. In the function, Im trying to retrieve,
among other stuff, the file type and the file that contains the icon used by Windows to display the file in Windows Explorer and similar explorer windows.
For example:

File: "C:file.zip"
File Type: "WinZip File"
Icon File: "C:Program Files (x86)WinZipWINZIP32.EXE"
Icon Number: 2
(this values are according to my Windows register)

- or -


File: "C:readme.txt"
File Type: "Text Document"
Icon File: "C:WindowsSystem32imageres.dll"
Icon Number: -102
(this values are according to my Windows register)


On this last one, the Icon Number is negative which is compatible with the ::ExtractIcon() API function, but I would like to know how to get the actual icon number inside the file.

Finally, I dont want to poke inside the registry. This is the code I have so far:



<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void KFFP::ProcessFile(<span style="color:Blue; const string &sFileName)

{

SHFILEINFO shfi;

string sfName = sFileName;

string sTypeDesc;

string sIconFile;

<span style="color:Blue; int iIcon;

<span style="color:Green; // retrieve the file information

<span style="color:Blue; if (SHGetFileInfo(sfName.c_str(), 0, &shfi, <span style="color:Blue; sizeof(shfi), SHGFI_TYPENAME | SHGFI_ICONLOCATION))

{

<span style="color:Green; // file type description

sTypeDesc = shfi.szTypeName;



<span style="color:Green; // file icon data

sIconFile = shfi.szDisplayName;

iIcon = shfi.iIcon;

} <span style="color:Green; // end if



<span style="color:Green; // work with the obtained data here

}
[/code]
<br/>



But what Im getting with this is, for example:
File: "C:sound.mp3"


SHFILEINFO::hIcon = NULL<span style="white-space:pre this result is expected
SHFILEINFO::iIcon = 14
SHFILEINFO::dwAttributes = 0<span style="white-space:pre this result is expected
SHFILEINFO::szDisplayName = ""
SHFILEINFO::szTypeName = "MPEG Layer 3 Audio File"


Im not getting the icon file, nor the correct icon number. Whats wrong my code?


Thanks for any help.

View the full article
 
Back
Top