我试过了:
public struct SHFILEINFOW
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260 * 2)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80 * 2)]
public string szTypeName;
}
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfoW(IntPtr pIDL, uint dwFileAttributes, out SHFILEINFOW psfi, uint cbFileInfo, SHGFI uFlags);
String DisplayName = shInfoW.szDisplayName;
但DisplayName只包含第一个char
答案 0 :(得分:3)
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260 * 2)]
ByValTStr
表示“与类或结构相同”。但是您没有为结构指定CharSet属性。它默认为CharSet.Ansi,因此字符串被编组,好像它是一个8位字符串。由于真正的字符串是Unicode,因此只有获得第一个字符才能获得非常高的赔率。修正:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHFILEINFOW {
// etc...
}