2009-06-09 72 views
4

我试图编写一个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); 
     } 
    } 

回答

9

包装你到SHGetKnownFolderPath调用在try-catch块。抓住System.EntryPointNotFoundException,然后尝试你的替代解决方案:

public static string GetKnownFolderPath(Guid guid) 
{ 
    try 
    { 
    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); 
    } 
    catch(EntryPointNotFoundException ex) 
    { 
    DoAlternativeSolution(); 
    } 
} 
+0

好的,这个工作,但有一个小的变化。我不得不捕获一个EntryPointNotFoundException,而不是一个DllNotFoundException。从功能上来看,shell32.dll是不可能在功能性Windows平台上缺失的。 – MiffTheFox 2009-06-09 14:00:51

3

您可以使用Environment.OSVersion酒店办理入住手续的操作系统版本。我相信,如果你这样做

int osVersion = Environment.OSVersion.Version.Major 
在XP

,这将是5,而在Vista上,这将是6,所以从那里只是一个简单的检查。

if(osVersion == 5) 
{ 
    //do XP way 
} 
else if(osVersion == 6) 
{ 
    //P/Invoke it 
}