使用非托管代码使用扩展名获取文件图标时,在x86系统上获取异常 [英] Getting exceptions on x86 system when using unmanaged code to get file icon using extension

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

问题描述

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

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

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

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

当我删除下面的代码应用程序完美无瑕。我想使用下面的代码从扩展名获取文件图标。有什么办法可以让代码在x86系统上工作吗?我发现这个代码从如何获取常见的文件类型C#中的图标

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);
    } 


推荐答案

您的定义 SHFILEINFO 尚未完成。原来看起来像

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;

在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;
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆