How to get default icon for a file type when the file doesn't actually exist ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello everyone,

Im trying to extract the default icon for a file type. Ive looked at lot of examples, and SHGetFileInfo seems to be the way to go when the files actually exist on disk. However, I just want to get the icon that would be associated with
a file name if that file were real. I think the following code fails because of the USEFILEATTRIBUTES flag, but perhaps Im wrong.
This code is going into a namespace extension, and the file names dont reflect files that actually exist on disk . Getting the icons for the folders works perfectly, however when using the USEFILEATTRIBUTES flag, it sets the icon to a folder as well.


<div style="color:Black;background-color:White; <pre>
STDMETHODIMP CExtractIcon::Extract(LPCTSTR <span style="color:Green; /* pszFile */, UINT nIconIndex,
HICON* phiconLarge, HICON* phiconSmall, UINT <span style="color:Green; /* nIconSize */)
{
PWSTR fileName = m_pPidlMgr->GetData(m_pidl);
SHFILEINFO sfi;
SecureZeroMemory(&sfi, <span style="color:Blue; sizeof sfi);
BOOL isFolder = m_pPidlMgr->IsFolder(m_pidl);

<span style="color:Blue; if(isFolder)
{
<span style="color:Green; // Get the small icon
SHGetFileInfo(
fileName,
FILE_ATTRIBUTE_DIRECTORY,
&sfi, <span style="color:Blue; sizeof sfi,
SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
nIconIndex = sfi.iIcon;
*phiconSmall = sfi.hIcon;

<span style="color:Green; // Get the large icon
SHGetFileInfo(
fileName,
FILE_ATTRIBUTE_DIRECTORY,
&sfi, <span style="color:Blue; sizeof sfi,
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES);
nIconIndex = sfi.iIcon;
*phiconLarge = sfi.hIcon;

<span style="color:Blue; return S_OK;

}
<span style="color:Blue; else
{
<span style="color:Green; // http://msdn.microsoft.com/en-us/magazine/cc301425.aspx
<span style="color:Green; // Get the small icon
SHGetFileInfo(fileName,
FILE_ATTRIBUTE_NORMAL,
&sfi, <span style="color:Blue; sizeof(sfi),
SHGFI_ICON |SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
nIconIndex = sfi.iIcon;
*phiconSmall = sfi.hIcon;

SHGetFileInfo(fileName,
FILE_ATTRIBUTE_NORMAL,
&sfi, <span style="color:Blue; sizeof(sfi),
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES ); <span style="color:Green; // | SHGFI_USEFILEATTRIBUTES
nIconIndex = sfi.iIcon;
*phiconLarge = sfi.hIcon;


<span style="color:Blue; return S_OK;
}
<span style=<span style="color:#A31515; "white-space:pre"> <span style="color:Green; // if we extraced icons : return S_OK;
<span style="color:Green; // else if the calling application should extract the icon
<span style="color:Green; // http://www.codeproject.com/KB/shell/shellextguide9.aspx
<span style="color:Green; // We want explorer to return the icon itself
<span style="color:Green; // return S_FALSE;
}
[/code]

Ive seen other ways to do this using the Regsitry, but also have seen advice stating not to do that. Does anybody have any suggestions 1) as to whether Ive screwed something up above, and 2) if there is a better/easier way to do this ?

Many thanks in advance,

JT.

View the full article
 
Back
Top