[英]Getting the version of an exe file without loading in .NET Compact Framework
[英]Getting icon from exe in .net compact framework
我正在為Windows Embedded Compact 7開發一些Windows窗體應用程序,它使用.net緊湊框架和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旗艦版上工作正常(當然使用shell32.dll而不是coredll.dll),但是當我嘗試在Windows Embedded或智能設備模擬器上運行此代碼時,我在這一行中Icon.FromHandle(shinfo.hIcon)
異常: Icon.FromHandle(shinfo.hIcon)
。 有人知道如何解決我的問題嗎?
什么文件是targetPath
? 它是否存在於Windows Embedded中? 它是空的還是空的? 我們不知道!
在那里放一些錯誤檢查代碼。
您的IntPtr hImgLarg
是專門設置的,因此您可以在繼續之前驗證返回值是否為錯誤編號。
另外,在編寫支票時,請查看shinfo
- 並特別查看SHGetFileInfo
填充了shinfo.hIcon
。
您可以將NULL
傳遞給Icon.FromHandle
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.