使用NullReference异常调用IExtractImage.GetLocation()失败

时间:2012-10-18 23:47:38

标签: c# windows-shell

我有以下代码来导入IExtractImage接口。

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1")]
public interface IExtractImage
{
  [PreserveSig]
   Int32 GetLocation([MarshalAs(UnmanagedType.LPWStr)] out StringBuilder pszPathBuffer, 
     int cch, 
     ref int pdwPriority, 
     SIZE prgSize, 
     int dwRecClrDepth, 
     ref int pdwFlags);

  [PreserveSig]
    Int32 Extract(out IntPtr phBmpThumbnail);
}

我也导入了IShellFolder,为简洁起见,我在这里没有提及。我的目的是使用shell文件夹访问文件的缩略图。所以这是我检索缩略图的代码。但我对IExtractImage.GetLocation()的调用失败,出现NullReference异常,说明

“CCDash.exe中发生'System.NullReferenceException'类型的异常,但未在用户代码中处理

附加信息:对象引用未设置为对象的实例。“

有人可以帮我确定一下我错过了什么吗?

public Bitmap GetThumbNail(string mediaFileName)
{
  IShellFolder shellDesktop;
  SHGetDesktopFolder(out shellDesktop);

  IntPtr pidlRoot;
  uint attribute = 0;
  string mediaPath = "C:\\Users\\<user>\\Videos"; // I have hard-coded the path for now
  uint pchEaten = 0;
  // Get the pidl of the media folder
  shellDesktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, mediaPath, ref pchEaten, out pidlRoot, ref attribute);

  Guid mediaFolderGuid = new Guid("000214E6-0000-0000-C000-000000000046");
  shellDesktop.BindToObject(pidlRoot, IntPtr.Zero, mediaFolderGuid, out shellMediaFolder);


  IntPtr pidlMediaFolder;
  // Get the pidl of the media file
  shellMediaFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, mediaFileName, ref pchEaten, out pidlMediaFolder, ref attribute);

  Guid mediaFileImgGuid = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");      
  uint rfgRes = 0;
  IExtractImage extractImage;
  shellMediaFolder.GetUIObjectOf(IntPtr.Zero, 1, out pidlMediaFolder, mediaFileImgGuid, ref rfgRes, out extractImage);

  SIZE size = new SIZE
  {
    cx = 40,
    cy = 40
  };

  int flags = 0x40 | 0x40;
  StringBuilder location = new StringBuilder(260, 260);
  int priority = 0;
  int requestedColourDepth = 0x20;
  IntPtr hBmp = IntPtr.Zero;
  // Now get the image
  extractImage.GetLocation(out location, location.Capacity, ref priority, size, requestedColourDepth, ref flags);
  extractImage.Extract(out hBmp);

  Bitmap thumbnail = Image.FromHbitmap(hBmp);

  return thumbnail;
} 

编辑:

我现在修改了我的代码,如下所示。与第一个版本没有太大差别,但有一点错误处理和更好的文档和变量名称可以帮助我们更好地理解我的代码。这是修改后的代码:

public Bitmap GetThumbNail(string mediaFileName)
{
  Bitmap thumbnail = null;

  //Step 1: Use SHGetDesktopFolder to get the desktop folder.
  IShellFolder shellDesktop;
  SHGetDesktopFolder(out shellDesktop);

  if (shellDesktop != null)
  {
    IntPtr pidlMediaFolder;
    try
    {
      uint attribute = 0;
      string mediaPath = Path.GetDirectoryName(mediaFileName);
      uint pchEaten = 0;
      // Step 2: Using the desktop's IShellFolder, pass the file's parent folder path name into ParseDisplayName to get its PIDL.
      shellDesktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, mediaPath, ref pchEaten, out pidlMediaFolder, ref attribute);
    }
    catch (Exception)
    {
      Marshal.ReleaseComObject(shellDesktop);
      return null;
    }

    if (pidlMediaFolder != IntPtr.Zero)
    {
      Guid mediaFolderGuid = new Guid("000214E6-0000-0000-C000-000000000046");
      IShellFolder shellMediaFolder;
      // Step 3: Using the desktop's IShellFolder, pass the PIDL into the BindToObject method 
      // and get the IShellFolder interface of the file's parent folder.          
      try
      {
        shellDesktop.BindToObject(pidlMediaFolder, IntPtr.Zero, mediaFolderGuid, out shellMediaFolder);
      }
      catch (Exception)
      {
        Marshal.ReleaseComObject(shellDesktop);
        return null;
      }

      if (shellMediaFolder != null)
      {
        IntPtr pidlMediaFile;
        uint attribute = 0;
        uint pchEaten = 0;
        // Step 4: Using the parent folder's IShellFolder, pass the file name into ParseDisplayName to get its PIDL.            
        int ret = shellMediaFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, mediaFileName, ref pchEaten, out pidlMediaFile, ref attribute);

        Guid mediaFileImgGuid = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
        uint rfgRes = 0;
        IExtractImage extractImage;
        // Step 5: Using the parent folder's IShellFolder, pass the file's PIDL 
        // into the GetUIObjectOf. method to get the IExtractImage interface.            
        ret = shellMediaFolder.GetUIObjectOf(IntPtr.Zero, 1, out pidlMediaFile, mediaFileImgGuid, ref rfgRes, out extractImage);

        SIZE size = new SIZE
        {
          cx = 40,
          cy = 40
        };

        uint flags = 0x0200;
        StringBuilder location = new StringBuilder(260, 260);
        int priority = 0;
        int requestedColourDepth = 0x20;
        IntPtr hBmp = IntPtr.Zero;
        // Now get the image
        extractImage.GetLocation(out location, location.Capacity, ref priority, size, requestedColourDepth, ref flags);
        extractImage.Extract(out hBmp);

        thumbnail = Image.FromHbitmap(hBmp);
      }
    }
  }
  return thumbnail;
}

我在第4步看到pidlMediaFile未正确检索,并且在ParseDisplayName()调用后它的值仍为0。这是问题的开始。我不知道为什么没有检索到文件名的pidl,而成功检索文件的父文件夹。

1 个答案:

答案 0 :(得分:0)

来自extractImage的{​​{1}}似乎正在返回null。请尝试检查GetUIObjectOf()的{​​{1}}返回值,找出返回HRESULT值的原因。

编辑:

来自http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137%28v=vs.85%29.aspx

GetUIObjectOf()

根据documentation,第三个参数是null参数,而不是E_INVALIDARG One or more arguments are not valid 0x80070057 参数。