【问题标题】:Getting exceptions on x86 system when using unmanaged code to get file icon using extension使用非托管代码使用扩展名获取文件图标时在 x86 系统上出现异常
【发布时间】:2012-08-20 08:38:54
【问题描述】:

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

致命的执行引擎错误

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

试图读取或写入受保护的内存。这通常是一个 指示其他内存已损坏

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

【问题讨论】:

    标签: c# exception x86 unmanaged


    【解决方案1】:

    您对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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 2016-05-27
      • 2013-10-24
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多