Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用IExtractMage.GetLocation()失败,出现NullReference异常_C#_Windows Shell - Fatal编程技术网

C# 调用IExtractMage.GetLocation()失败,出现NullReference异常

C# 调用IExtractMage.GetLocation()失败,出现NullReference异常,c#,windows-shell,C#,Windows Shell,我有以下代码来导入IExtractMage接口 [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1")] public interface IExtractImage { [PreserveSig] Int32 GetLocation([MarshalAs(UnmanagedType.LPWStr)] out Strin

我有以下代码来导入IExtractMage接口

[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,为了简洁起见,这里不提它。我的目的是使用shellfolder访问文件的缩略图。这是我检索缩略图的代码。但是我对IExtractMage.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;
} 

我看到在步骤4中,没有正确检索到pidlMediaFile,在ParseDisplayName()调用之后,它的值仍然是0。这就是问题的开始。我不确定为什么没有检索文件名的pidl,而成功地检索了文件父文件夹的pidl。

看起来
extractImage
正在从
GetUIObjectOf()
返回
null
。尝试检查
GetUIObjectOf()
中的
HRESULT
返回值,以找出返回
null
值的原因

编辑:

发件人:


根据,第三个参数是
[in]
参数,而不是
[out]
参数。

似乎
extractImage
是从
GetUIObjectOf()
返回的
null
?我同意。基于API调用,您是否觉得extractImage为null的原因很明显?
GetUiObjectOf()
返回一个
HRESULT
错误代码。你可以检查一下。我得到这个错误值:-2147024809(十六进制等于FFFFFFFF80070057)。但是我在任何地方都找不到这个错误代码的定义。现在,我不确定我是否使用正确的参数以正确的顺序进行shell API调用。下面是我正在做的:现在,我不确定是否使用正确的参数以正确的顺序进行shell API调用。以下是我正在做的:1。获取桌面文件夹。2.使用桌面文件夹获取媒体文件夹的idl。3.使用刚刚检索到的idl获取媒体文件夹的外壳对象。4.使用媒体文件夹的shell对象调用GetUIObjectOf()并传递刚检索到的idl。我已删除此步骤列表中的步骤shellMediaFolder.ParseDisplayName(),因为在整个步骤序列中,这似乎是一个错误的步骤。但我还是得到了同样的错误。谷歌“hresult”,你会找到答案:
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;
}
E_INVALIDARG    One or more arguments are not valid 0x80070057