我使用SHGetFileInfo()
orGetDisplayNameOf()
来获取特殊文件夹的名称。
如果本地化操作系统更改设置“非 Unicode 程序的当前语言”,这些函数将返回一个值“????????????”。
用户遇到的这种设置组合。
shell32 不完全兼容 unicode?
Shell32.STRRET STRRET;
STRRET.uType = (uint)Shell32.STRRET_TYPE.STRRET_WSTR;
if (Windows.S_OK != ishellfolder_parent.GetDisplayNameOf(ptr_pidllast, (uint)Shell32.SHGNO.SHGDN_NORMAL | (uint)Shell32.SHGNO.SHGDN_INFOLDER, out STRRET))
return null;
StringBuilder sbuilder = new StringBuilder(260);
Shell32.StrRetToBuf(ref STRRET, ptr_pidllast, sbuilder, (uint)sbuilder.Capacity);
怎么了?
***稍后添加另一个示例来演示我的问题:
public static partial class Program
{
const Int32 CSIDL_DESKTOP = (0x0000);
const uint SHGFI_DISPLAYNAME = 0x000000200; // get display name
const uint SHGFI_PIDL = 0x000000008; // pszPath is a pidl
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public static int NAMESIZE = 80;
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll")]
static extern IntPtr SHGetFileInfo(IntPtr pidl, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
[DllImport("shell32.dll")]
public static extern IntPtr SHCloneSpecialIDList(IntPtr hwnd, Int32 CSIDL, bool create);
[STAThread]
static void Main(string[] args)
{
IntPtr pidl = SHCloneSpecialIDList(IntPtr.Zero, CSIDL_DESKTOP, false);
SHFILEINFO shfi = new SHFILEINFO();
if (IntPtr.Zero != SHGetFileInfo(
pidl,
0,
ref shfi,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI_PIDL | SHGFI_DISPLAYNAME))
{
System.Windows.Forms.MessageBox.Show(shfi.szDisplayName);
}
此代码不正确。某些情况下返回值错误,如上所述。谁能帮我举一个正确代码的例子,完全兼容 Unicode 并使用非默认系统设置?
谢谢你们!经过一番实验,找到了解决办法。我的错误在这里:
Shell32.StrRetToBuf(ref STRRET, ptr_pidllast, sbuilder, (uint)sbuilder.Capacity);
签名应该是:
[DllImport("shlwapi.dll", CharSet=CharSet.Unicode, EntryPoint="StrRetToBufW")]
public static extern Int32 StrRetToBufW( ...