我们在Windows窗体应用程序中使用适当的文件图标使用以下代码显示文件的树视图。我的问题是,调用GetIcon()似乎损坏了我的内存,因为我开始发生各种程序崩溃,在此调用之后,我无法用调试器捕捉到这些崩溃。
当我将managedType.LPTStr
更改为managedType.ByValTStr
时,该程序正常工作。这是一个真正的解决方案还是只是掩盖了问题?可以使用UnmanagedMemory.LPTStr而不是.ByValTStr导致内存损坏?为什么?
此代码似乎在我们的上一个产品版本中工作,我看不到任何已更改。使用.NET 4.0。我只能在发布模式下看到问题。
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon=IntPtr.Zero;
iIcon=0;
dwAttributes=0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 260)]//works if .ByValTStr is used instead
public string szDisplayName;
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 80)]//works if .ByValTStr is used instead
public string szTypeName;
};
public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes;
SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
谢谢。一位同事在我读到你的答案之前刚刚过来解释这一点,我正准备自己回答。 – Kim 2013-03-01 20:16:01