I'm developing disk catalog application that requires me to get file icons using file extensions retrieved from the database. Code to get file icon using their extension works absolutely fine on my Windows 7 x64 machine with Any CPU debug configuration but when i switch to x86 in debug configuration i get following error.
Fatal Execution Engine error
When i tried to run the application in Windows XP x86 in Any CPU configuration i get following error.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt
When i remove the below code application works flawlessly. I want to use below code to get file icon from extension. is there any workaround to get the code to work on x86 system? i found this code from How do I get common file type icons in C#?.
/// <summary>
/// Contains information about a file object.
/// </summary>
struct SHFILEINFO
{
/// <summary>
/// Handle to the icon that represents the file. You are responsible for
/// destroying this handle with DestroyIcon when you no longer need it.
/// </summary>
public IntPtr HIcon;
};
[Flags]
enum FileInfoFlags
{
/// <summary>
/// Retrieve the handle to the icon that represents the file and the index
/// of the icon within the system image list. The handle is copied to the
/// hIcon member of the structure specified by psfi, and the index is copied
/// to the iIcon member.
/// </summary>
ShgfiIcon = 0x000000100,
/// <summary>
/// Indicates that the function should not attempt to access the file
/// specified by pszPath. Rather, it should act as if the file specified by
/// pszPath exists with the file attributes passed in dwFileAttributes.
/// </summary>
ShgfiUsefileattributes = 0x000000010
}
[DllImport("Shell32", CharSet = CharSet.Auto)]
extern static IntPtr SHGetFileInfo(
string pszPath,
int dwFileAttributes,
out SHFILEINFO psfi,
int cbFileInfo,
FileInfoFlags uFlags);
/// <summary>
/// Two constants extracted from the FileInfoFlags, the only that are
/// meaningfull for the user of this class.
/// </summary>
public enum IconSize
{
Large = 0x000000000,
Small = 0x000000001
}
/// <summary>
/// Get the icon associated with file Extension.
/// </summary>
/// <param name="fileExt">Search icon for this file extension</param>
/// <param name="size">Icon size</param>
/// <returns></returns>
public static Icon GetIcon(string fileExt ,IconSize size)
{
var fileInfo = new SHFILEINFO();
SHGetFileInfo(fileExt, 0, out fileInfo, Marshal.SizeOf(fileInfo),
FileInfoFlags.ShgfiIcon | FileInfoFlags.ShgfiUsefileattributes | (FileInfoFlags)size);
return Icon.FromHandle(fileInfo.HIcon);
}