c# - 使用非托管代码使用扩展名获取文件图标时在 x86 系统上获取异常

标签 c# exception x86 unmanaged

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

Fatal Execution Engine error

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

Attempted to read or write protected memory. This is often an indication that other memory is corrupt

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

最佳答案

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

关于c# - 使用非托管代码使用扩展名获取文件图标时在 x86 系统上获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034552/

相关文章:

c# - 当非双击时如何处理 WinForms 控件单击事件

c++ - COM 中的异常

从 Servlet 执行批量更新时发生 Java EXCEPTION_ACCESS_VIOLATION

c# - 使用 Xamarin 自动登录 Azure 移动服务

c# - WCF 中端点的基地址

c# - Json.Net 无法从动态对象反序列化为 nullable<short>

不调用 Thread.join() 时的 C++ 多线程异常

linux - 反汇编为 Intel 语法的 x86 ELF 反汇编程序

assembly - lodsd/stosd 汇编指令 : possible errata in reversing book

assembly - X86 编码近调用相对偏移量