本文介绍了在x86系统上获得异常使用非托管代码在使用扩展​​来获得文件图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发,需要我用得从数据库检索的文件扩展名的文件图标磁盘目录应用程序。代码中使用它们的扩展名来获得文件图标作品精绝我的Windows 7 64位机上与任何CPU的调试配置,但是当我切换到x86的调试配置,我收到以下错误。





当我试图运行在Windows XP x86的在任何CPU配置中的应用我得到以下错误。



When i tried to run the application in Windows XP x86 in Any CPU configuration i get following error.

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);
    } 
解决方案

Your definition of SHFILEINFO is not complete. Original looks like

typedef struct _SHFILEINFO {
  HICON hIcon;
  int   iIcon;
  DWORD dwAttributes;
  TCHAR szDisplayName[MAX_PATH];
  TCHAR szTypeName[80];
} SHFILEINFO;

In C# it should look like

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SHFILEINFO {
    public IntPtr hIcon;
    public int iIcon;
    public uint dwAttributes;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string szTypeName;
}

这篇关于在x86系统上获得异常使用非托管代码在使用扩展​​来获得文件图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:22