T
Terry
There are a number of things about using unmanaged resources in Windows
Forms programming that is unclear to me.
In C++, if you loaded an icon resource using "ExtractIcon()", the resource
was valid until you called "DestroyIcon()". If you didn't, you had a leak.
In C#, I need to load icons from unmanaged programs (.exe, dll) at run time
and I want to make sure I'm not leaking resources, but it's not clear to me
how to insure that I'm not.
I've imported "ExtractIcon" and "DestroyIcon" like so:
[DllImport("Shell32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr ExtractIcon(IntPtr hInstance, string
strFileName, uint uiIconIndex);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern bool DestroyIcon(IntPtr hIcon);
I load a number of icons from the file system and stuff them into an "Icon"
object and then into an ImageList.
public static Icon extractIcon(string strPath, int nIndex)
{
Icon icon = null;
IntPtr hIcon = WindowsAPI.ExtractIcon(IntPtr.Zero, strPath,
(uint)nIndex);
if (IntPtr.Zero != hIcon)
{
icon = Icon.FromHandle(hIcon);
}
return icon;
}
At this point, I've got an unmanaged recource being held by that "Icon"
object. What exactly is going on here? Does the "FromHandle()" call make a
copy of the resource into a *managed* resource? i.e. can I call
"DestroyIcon" on the unmanaged hIcon at this point? Or would that destroy
something that particular Icon object needs yet?
Let's say I never call "DestroyIcon" - will the unmanaged Icon resource get
cleaned up when the Icon's "Dispose()" method is called? (I know not to
rely on the Dispose for resource cleanup, I'm just asking out of curiosity).
Could someone please clear this up? I want to do this right.
Thanks,
Terry
Forms programming that is unclear to me.
In C++, if you loaded an icon resource using "ExtractIcon()", the resource
was valid until you called "DestroyIcon()". If you didn't, you had a leak.
In C#, I need to load icons from unmanaged programs (.exe, dll) at run time
and I want to make sure I'm not leaking resources, but it's not clear to me
how to insure that I'm not.
I've imported "ExtractIcon" and "DestroyIcon" like so:
[DllImport("Shell32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr ExtractIcon(IntPtr hInstance, string
strFileName, uint uiIconIndex);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern bool DestroyIcon(IntPtr hIcon);
I load a number of icons from the file system and stuff them into an "Icon"
object and then into an ImageList.
public static Icon extractIcon(string strPath, int nIndex)
{
Icon icon = null;
IntPtr hIcon = WindowsAPI.ExtractIcon(IntPtr.Zero, strPath,
(uint)nIndex);
if (IntPtr.Zero != hIcon)
{
icon = Icon.FromHandle(hIcon);
}
return icon;
}
At this point, I've got an unmanaged recource being held by that "Icon"
object. What exactly is going on here? Does the "FromHandle()" call make a
copy of the resource into a *managed* resource? i.e. can I call
"DestroyIcon" on the unmanaged hIcon at this point? Or would that destroy
something that particular Icon object needs yet?
Let's say I never call "DestroyIcon" - will the unmanaged Icon resource get
cleaned up when the Icon's "Dispose()" method is called? (I know not to
rely on the Dispose for resource cleanup, I'm just asking out of curiosity).
Could someone please clear this up? I want to do this right.
Thanks,
Terry