EDN Admin
Well-known member
Hi all!
So I have made a program that allows plugins to be loaded. <br/>
However their is 1 plugin application that loads icons from shell32.dll using p/invoke.<br/>
This works but afther I opened the application for 3 times it gives me this error:<br/>
Win32 Handle that was passed to Icon is not valid or is the wrong type
So I debugged the application and I noticed that when the application restarts the 3-4th time. The Icon.FromHandle() becuase somehow the IntPtr handle turned into IntPtr.Zero.
Here is the code:<br/>
<pre class="prettyprint public static class getIcon
{
[DllImport("shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);
[DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
private static extern int DestroyIcon(IntPtr hIcon);
//this is for shell32.dll on XP
private static int numIcons = 238;
private static IntPtr[] largeIcon = new IntPtr[numIcons - 1];
private static IntPtr[] smallIcon = new IntPtr[numIcons - 1];
public enum FolderIconType : int
{
Normal = 3,
Open = 4,
FloppyDrive = 6,
HardDrive = 8,
NetworkDrive = 9,
CDRomDrive = 11,
MyDocuments = 126
}
public enum IconSize
{
Small,
Large
}
//======getdifferentfolderIcon=====
public static Icon getSystemFolderIcon(FolderIconType folderType, IconSize icoSize)
{
try
{
largeIcon = new IntPtr[numIcons - 1];
smallIcon = new IntPtr[numIcons - 1];
ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, numIcons);
Icon getIco = null;
if (icoSize == IconSize.Large)
{
getIco = Icon.FromHandle(largeIcon[(int)folderType]);
return getIco;
}
else
{
getIco = Icon.FromHandle(smallIcon[(int)folderType]);
return getIco;
}
}
finally
{
//foreach (IntPtr ptr in largeIcon)
// if (ptr != IntPtr.Zero)
// DestroyIcon(ptr);
//foreach (IntPtr ptr in smallIcon)
// if (ptr != IntPtr.Zero)
// DestroyIcon(ptr);
}
}
}[/code]
<br/>
At the start of every plugin application it would get the icons using this code:
<pre class="prettyprint I_HDD = getIcon.getSystemFolderIcon(getIcon.FolderIconType.HardDrive, getIcon.IconSize.Large);
I_FOLDER_BIG = getIcon.getSystemFolderIcon(getIcon.FolderIconType.Open, getIcon.IconSize.Large);
I_MYDOCS_BIG = getIcon.getSystemFolderIcon(getIcon.FolderIconType.MyDocuments, getIcon.IconSize.Large);[/code]
<br/>
But for some reason afther I (re)started the application 3-4 times or more the IntPtrs in
largeIcon & smallIcon will all be IntPtr.Zero.<br/>
Even afther the ExtractIconEx() call they remain zero. But the first couple of times it contains valid IntPtrs.
If you need more info just ask!
Thanks alot!
<hr class="sig Hope This Helps! <br/>
<br/>
If this post answers your question, please click <b>"Mark As Answer"</b>.<br/>
If this post is helpful please click <b>"Mark as Helpful"</b>.<br/>
Else you got to wait until a MVP or MCC replies they are the <b>"PROs"
</b><br/>
View the full article
So I have made a program that allows plugins to be loaded. <br/>
However their is 1 plugin application that loads icons from shell32.dll using p/invoke.<br/>
This works but afther I opened the application for 3 times it gives me this error:<br/>
Win32 Handle that was passed to Icon is not valid or is the wrong type
So I debugged the application and I noticed that when the application restarts the 3-4th time. The Icon.FromHandle() becuase somehow the IntPtr handle turned into IntPtr.Zero.
Here is the code:<br/>
<pre class="prettyprint public static class getIcon
{
[DllImport("shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);
[DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
private static extern int DestroyIcon(IntPtr hIcon);
//this is for shell32.dll on XP
private static int numIcons = 238;
private static IntPtr[] largeIcon = new IntPtr[numIcons - 1];
private static IntPtr[] smallIcon = new IntPtr[numIcons - 1];
public enum FolderIconType : int
{
Normal = 3,
Open = 4,
FloppyDrive = 6,
HardDrive = 8,
NetworkDrive = 9,
CDRomDrive = 11,
MyDocuments = 126
}
public enum IconSize
{
Small,
Large
}
//======getdifferentfolderIcon=====
public static Icon getSystemFolderIcon(FolderIconType folderType, IconSize icoSize)
{
try
{
largeIcon = new IntPtr[numIcons - 1];
smallIcon = new IntPtr[numIcons - 1];
ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, numIcons);
Icon getIco = null;
if (icoSize == IconSize.Large)
{
getIco = Icon.FromHandle(largeIcon[(int)folderType]);
return getIco;
}
else
{
getIco = Icon.FromHandle(smallIcon[(int)folderType]);
return getIco;
}
}
finally
{
//foreach (IntPtr ptr in largeIcon)
// if (ptr != IntPtr.Zero)
// DestroyIcon(ptr);
//foreach (IntPtr ptr in smallIcon)
// if (ptr != IntPtr.Zero)
// DestroyIcon(ptr);
}
}
}[/code]
<br/>
At the start of every plugin application it would get the icons using this code:
<pre class="prettyprint I_HDD = getIcon.getSystemFolderIcon(getIcon.FolderIconType.HardDrive, getIcon.IconSize.Large);
I_FOLDER_BIG = getIcon.getSystemFolderIcon(getIcon.FolderIconType.Open, getIcon.IconSize.Large);
I_MYDOCS_BIG = getIcon.getSystemFolderIcon(getIcon.FolderIconType.MyDocuments, getIcon.IconSize.Large);[/code]
<br/>
But for some reason afther I (re)started the application 3-4 times or more the IntPtrs in
largeIcon & smallIcon will all be IntPtr.Zero.<br/>
Even afther the ExtractIconEx() call they remain zero. But the first couple of times it contains valid IntPtrs.
If you need more info just ask!
Thanks alot!
<hr class="sig Hope This Helps! <br/>
<br/>
If this post answers your question, please click <b>"Mark As Answer"</b>.<br/>
If this post is helpful please click <b>"Mark as Helpful"</b>.<br/>
Else you got to wait until a MVP or MCC replies they are the <b>"PROs"
</b><br/>
View the full article