我试图编写一个C#托管类来包装SHGetKnownFolderPath,到目前为止它在Vista上工作,但由于在shell32.dll中找不到正确的函数而导致XP崩溃,如预期的那样。如何处理失败的DllImport?
我想让它设置好,这样我就可以在使用System.Environment.GetFolderPath(如果使用XP)的情况下回退(肯定是hacky)解决方案。 (或者,甚至更好,如果它在shell32中找不到功能。)
有没有其他方法可以做到这一点,然后条件编译?
我当前的代码看起来像:
public abstract class KnownFolders
{
[DllImport("shell32.dll")]
private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
// Trim properties to get various Guids.
public static string GetKnownFolderPath(Guid guid)
{
IntPtr pPath;
int result = SHGetKnownFolderPath(guid, 0, IntPtr.Zero, out pPath);
if (result == 0)
{
string s = Marshal.PtrToStringUni(pPath);
Marshal.FreeCoTaskMem(pPath);
return s;
}
else
throw new System.ComponentModel.Win32Exception(result);
}
}
好的,这个工作,但有一个小的变化。我不得不捕获一个EntryPointNotFoundException,而不是一个DllNotFoundException。从功能上来看,shell32.dll是不可能在功能性Windows平台上缺失的。 – MiffTheFox 2009-06-09 14:00:51