【问题标题】:Getting icon from exe in .net compact framework从.net紧凑框架中的exe获取图标
【发布时间】:2013-03-12 05:59:56
【问题描述】:

我正在为 Windows Embedded Compact 7 开发一些 Windows 窗体应用程序,该应用程序使用 .net compact 框架和 c# Smart Device 项目类型模拟桌面外壳。我使用SHGetFileInfo WinAPI 函数从 exe 文件中获取相关图标,下面是我的代码:

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

    public SHFILEINFO(bool setDefaults)
    {
        hIcon = IntPtr.Zero;
        iIcon = IntPtr.Zero;
        dwAttributes = 0;
        szDisplayName = "";
        szTypeName = "";
    }
}
public class Win32
{
    public const uint SHGFI_ICON = 0x000000100; 
    public const uint SHGFI_LARGEICON = 0x00000000; 
    public const uint SHGFI_SMALLICON = 0x00000001; 

    [DllImport("coredll.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath,
                                              int dwFileAttributes,
                                              ref SHFILEINFO psfi,
                                              uint cbSizeFileInfo,
                                              uint uFlags);
 }

然后我从这里调用这个函数:

private static Icon ExtractIconFromExe(string targetPath)
{
IntPtr hImgLarge; 
var shinfo = new SHFILEINFO();
hImgLarge = Win32.SHGetFileInfo(targetPath,
                                0,
                                ref shinfo,
                                (uint)Marshal.SizeOf(shinfo),
                                Win32.SHGFI_ICON);
var icon = Icon.FromHandle(shinfo.hIcon);
return icon;
}

它在我的 Windows 7 Ultimate 上运行良好(当然使用 shell32.dll 而不是 coredll.dll),但是当我尝试在 Windows Embedded 或 Smart Device 模拟器上运行此代码时,我在这一行中出现了无信息异常:Icon.FromHandle(shinfo.hIcon) .有人知道如何解决我的问题吗?

【问题讨论】:

    标签: .net icons compact-framework exe


    【解决方案1】:

    targetPath 是什么文件?它存在于 Windows Embedded 上吗?它是空的还是空的?我们不知道!

    在里面放一些错误检查代码。

    您的IntPtr hImgLarg 是专门设置的,因此您可以在继续之前验证返回值不是错误号。

    另外,在编写支票时,请查看shinfo - 并特别查看SHGetFileInfo 是否填充shinfo.hIcon

    您可以将NULL 传递给Icon.FromHandle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多