我正在開發一些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)
。有人知道如何解決我的問題嗎?