从 .net compact framework 中的 exe 文件中获取图标

3

我正在开发一些Windows Embedded Compact 7的窗体应用程序,使用.net compact framework和c#智能设备项目类型来模拟桌面Shell。我使用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嵌入式或智能设备模拟器上运行此代码时,我会在这一行遇到无法解释的异常:Icon.FromHandle(shinfo.hIcon)。有人知道如何解决我的问题吗?

1个回答

0

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

在那里加入一些错误检查代码。

您的 IntPtr hImgLarg 被设置得很具体,这样您可以在继续之前验证返回值不是错误编号。

此外,在编写检查时,请查看 shinfo - 特别是查看 SHGetFileInfo 是否填充了 shinfo.hIcon

您可能会将一个 NULL 传递给 Icon.FromHandle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接