2013-03-22 62 views
3

我正在开发一些Windows Embedded Compact 7的Windows窗体应用程序,它使用.net紧凑框架和c#Smart Device项目类型模仿桌面外壳。我用SHGetFileInfo WinAPI的函数来获取从exe文件关联的图标,这里是我下面的代码:在.net紧凑框架中从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旗舰版(当然使用shell32.dll而不是coredll.dll),但是当我尝试在Windows Embedded或智能设备仿真器上运行此代码时,我在此行中有无意义的例外:Icon.FromHandle(shinfo.hIcon)。有人知道如何解决我的问题吗?

回答

0

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

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

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

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

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