I've found some strange thing about this:
If I want to extract for example the default icon for '.xls' files there is
a registry key redirecting to the class:
HKEY_CLASSES_ROOT\.xls
The default of this key gives me the class name of this: xlsfile
In the registry under this cass name there is another key holdiing the
information about the default icon for this class:
HKEY_CLASSES_ROOT\xlsfile\DefaultIcon
Which contains the following data: officeres.dll,-15650
So was thinking this are the values to use for the ExtractIconEx filename
and index. The file officeres.dll exists and there are icons in this file
but with index -15650 no icon is extracted. There is another file called
officeres.96.dll. If I use this instead, the icon is extracted successfully.
Does anyone knows how I can figure out if the defult icon is 'redirected in
such way?
Here is the code i use:
public static bool ExtractAssocoiatedIcon(string Extension, ref Icon
IconBitmap, bool LargeIcon)
{
if (!string.IsNullOrEmpty(Extension))
{
try
{
// Replace *
Extension = Extension.Replace("*", "").Trim();
if (Registry.ClassesRoot.GetSubKeyNames().Contains(Extension))
{
String strHost =
Registry.ClassesRoot.OpenSubKey(Extension).GetValue("").ToString();
if (!string.IsNullOrEmpty(strHost))
{
string[] strValues =
Registry.ClassesRoot.OpenSubKey(String.Format(@"{0}\DefaultIcon",
strHost)).GetValue("").ToString().Split(',');
string strFilepath = string.Format(@"Windows\{0}",
strValues[0]);
// Check if File exists
if (File.Exists(strFilepath))
{
// Extract File
int readIconCount = 0;
IntPtr[] hDummy = new IntPtr[2] { IntPtr.Zero,
IntPtr.Zero };
IntPtr[] hIconEx = new IntPtr[2] { IntPtr.Zero,
IntPtr.Zero };
try
{
int intIndex = 0;
if (strValues.GetUpperBound(0) > 0)
{
intIndex = int.Parse(strValues[1]);
}
//// Get number of Icons
IntPtr HIcon = IntPtr.Zero;
IntPtr HApp = IntPtr.Zero;
int TotalIcons = 1;
HIcon = CWinAPI.ExtractIcon(HApp, strFilepath,
TotalIcons);
if (LargeIcon)
readIconCount = CWinAPI.ExtractIconEx(strFilepath, -2,
hIconEx, hDummy, 1);
else
readIconCount = CWinAPI.ExtractIconEx(strFilepath, -2,
hDummy, hIconEx, 1);
if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
{
IconBitmap = (Icon)Icon.FromHandle(hIconEx[0]).Clone();
return true;
}
}
catch (Exception ex)
{
/* EXTRACT ICON ERROR */
// BUBBLE UP
throw new ApplicationException("Could not extract icon",
ex);
}
finally
{
// RELEASE RESOURCES
foreach (IntPtr ptr in hIconEx)
if (ptr != IntPtr.Zero)
CWinAPI.DestroyIcon(ptr);
foreach (IntPtr ptr in hDummy)
if (ptr != IntPtr.Zero)
CWinAPI.DestroyIcon(ptr);
}
}
}
}
}
catch (Exception ex)
{
CLog.Log("IRFramework.CUtil", "0", ex.ToString(),
CLog.LogLevel.Error);
}
}
return false;
}
Thanks for help
Roman
Roman Mellenberger said:
Hello,
I'm trying to create a function which gets the associated Icon to a known
extension. I grab the path for the extension (eg.xls = officeres.dll) from
registry and to get the icon which works perfectly if I intend to get the
first icon. Can anybody tell me the parameters for ExtractIconEx when I
want to get the second icon?
I'm using Windows mobile 5.0 several devices...
Best regards
Roman