简体   繁体   English

从.net紧凑框架中的exe获取图标

[英]Getting icon from exe in .net compact framework

I am developing some windows forms application for Windows Embedded Compact 7 that imitates desktop shell using .net compact framework and c# Smart Device project type. 我正在为Windows Embedded Compact 7开发一些Windows窗体应用程序,它使用.net紧凑框架和c#智能设备项目类型来模仿桌面shell。 I use SHGetFileInfo WinAPI function to get associated icon from exe file, here is my code below: 我使用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);
 }

and then I call this function from here: 然后我从这里调用这个函数:

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

It's works fine on my Windows 7 Ultimate (using shell32.dll instead coredll.dll of course), but when I try to run this code on Windows Embedded or Smart Device emulator I have uninformative exception in this line: Icon.FromHandle(shinfo.hIcon) . 它在我的Windows 7旗舰版上工作正常(当然使用shell32.dll而不是coredll.dll),但是当我尝试在Windows Embedded或智能设备模拟器上运行此代码时,我在这一行中Icon.FromHandle(shinfo.hIcon)异常: Icon.FromHandle(shinfo.hIcon) Does anybody know how to solve my problem? 有人知道如何解决我的问题吗?

What file is targetPath ? 什么文件是targetPath Does it exist on Windows Embedded? 它是否存在于Windows Embedded中? Is it Empty or Null? 它是空的还是空的? We don't know! 我们不知道!

Put some error checking code in there. 在那里放一些错误检查代码。

Your IntPtr hImgLarg is set specifically so you can verify the return value is not an error number before you proceed. 您的IntPtr hImgLarg是专门设置的,因此您可以在继续之前验证返回值是否为错误编号。

Also, when writing your checks, look at shinfo - and specifically see if SHGetFileInfo populates shinfo.hIcon . 另外,在编写支票时,请查看shinfo - 并特别查看SHGetFileInfo填充了shinfo.hIcon

You could be passing a NULL to Icon.FromHandle . 您可以将NULL传递给Icon.FromHandle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM