3

我正在开发磁盘目录应用程序,该应用程序需要我使用从数据库中检索到的文件扩展名来获取文件图标。使用其扩展名获取文件图标的代码在我的具有任何 CPU 调试配置的 Windows 7 x64 机器上工作得非常好,但是当我在调试配置中切换到 x86 时出现以下错误。

致命的执行引擎错误

当我尝试在 Windows XP x86 中以任何 CPU 配置运行应用程序时,出现以下错误。

尝试读取或写入受保护的内存。这通常表明其他内存已损坏

当我删除下面的代码应用程序完美无缺。我想使用下面的代码从扩展名中获取文件图标。是否有任何解决方法可以让代码在 x86 系统上运行?我从如何在 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);
    } 
4

1 回答 1

3

你的定义SHFILEINFO不完整。原来的样子

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

在 C# 中它应该看起来像

[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;
}
于 2012-08-20T08:52:15.817 回答