ExtractIconEx get multiple icons

R

Roman Mellenberger

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
 
C

Chris Tacke, eMVP

Well the docs for the API make it pretty obvious:
http://msdn.microsoft.com/en-us/library/aa453065.aspx

nIconIndex
[in] Specifies the zero-based index of the first icon to extract. For
example, if this value is zero, the function extracts the first icon in the
specified file.
For Windows CE 2.10 and later, the nIconIndex parameter must be zero or -N,
where N is a specified resource identifier.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
R

Roman Mellenberger

Yes - but i cannot figure out which value I should take as 'resource
identifier'. I tried with -1 or the value stored in the registry together
with the DeafultPath without success...

Thanks for help!


Chris Tacke said:
Well the docs for the API make it pretty obvious:
http://msdn.microsoft.com/en-us/library/aa453065.aspx

nIconIndex
[in] Specifies the zero-based index of the first icon to extract. For
example, if this value is zero, the function extracts the first icon in
the specified file.
For Windows CE 2.10 and later, the nIconIndex parameter must be zero
or -N, where N is a specified resource identifier.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com




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
 
R

Roman Mellenberger

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
 
S

Simon Hart [MVP]

I wouldn't want to maintain that code!

You're getting very specifc to the inner workings of how Microsoft ships its
office suite of applications. I'm guessing that this is probably not
documented or that there is no public API available to do this. If I were you
I'd take copies because otherwise you're setting yourself up for a
maintenance nightmare.

Now I'm just guessing but perhaps officeres.96.dll refers to QVGA devices ie
96x96 DPI and maybe officeres.dll refers to VGA - perhaps.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://www.simonrhart.com


Roman Mellenberger said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top