SHGetFileInfo icon and dispose
-
I am using a dllimport of SHGetFileInfo to retrieve a file icon, and then add it to an image list. is there something i need to disposse ? this line
this.iconsListSmall.Images.Add( FileGetIcon( fi.FullName, true ) );
and this functionpublic static Icon FileGetIcon( string strPath, bool bSmall ) { SHFILEINFO info = new SHFILEINFO( true ); uint cbFileInfo = ( uint )Marshal.SizeOf( info ); SHGFI flags; if ( bSmall ) flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes; else flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes; SHGetFileInfo( strPath, 256, out info, cbFileInfo, flags ); return System.Drawing.Icon.FromHandle( info.hIcon ); }
-
I am using a dllimport of SHGetFileInfo to retrieve a file icon, and then add it to an image list. is there something i need to disposse ? this line
this.iconsListSmall.Images.Add( FileGetIcon( fi.FullName, true ) );
and this functionpublic static Icon FileGetIcon( string strPath, bool bSmall ) { SHFILEINFO info = new SHFILEINFO( true ); uint cbFileInfo = ( uint )Marshal.SizeOf( info ); SHGFI flags; if ( bSmall ) flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes; else flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes; SHGetFileInfo( strPath, 256, out info, cbFileInfo, flags ); return System.Drawing.Icon.FromHandle( info.hIcon ); }
Well if you search google: "SHGetFileInfo site:msdn.microsoft.com" You will see towards the bottom of the page: If SHGetFileInfo returns an icon handle in the hIcon member of the SHFILEINFO structure pointed to by psfi, you are responsible for freeing it with DestroyIcon when you no longer need it. Alex Korchemniy
-
Well if you search google: "SHGetFileInfo site:msdn.microsoft.com" You will see towards the bottom of the page: If SHGetFileInfo returns an icon handle in the hIcon member of the SHFILEINFO structure pointed to by psfi, you are responsible for freeing it with DestroyIcon when you no longer need it. Alex Korchemniy
so should i
icotmp = FileGetIcon( fi.FullName, true ); this.iconsListSmall.Images.Add( icotmp );
DestroyIcon( icotmp.Handle );
oricotmp.Dispose();
or should i free it in FileGetIcon() before returning the copied icon in a system.drawing.iconFileGetIcon() .. DestroyIcon( info.hIcon ); Icon ico = System.Drawing.Icon.FromHandle( info.hIcon ); return ico;