如何检索 IShellItem 的文件大小?

发布于 2025-01-20 10:55:46 字数 442 浏览 2 评论 0 原文

给定一个 IShellItem*,我如何找出它的大小?

环顾四周时,我发现解决方案可以是:

  1. IShellItem2 绑定到给定的 IShellItem
  2. 检索 IShellItem 属性存储
  3. 使用 < a href="https://learn.microsoft.com/en-us/windows/win32/api/propvarutil/nf-propvarutil-propvarianttouint64" rel="nofollow noreferrer">这个函数(如页面中的示例所示),找到文件的大小

我不完全理解Win32 API,所以也许我把这一切都错了,但如果我是对的,我只是发现很难得到过了第一步 - 我怎样才能绑定这两个?

Given an IShellItem*, how can I find out its size?

When looking around, I've seen that a solution for this can be:

  1. bind IShellItem2 to the given IShellItem
  2. retrieve the IShellItem property store
  3. with this function (as seen in the example in the page), find the file's size

I don't fully understand the Win32 API, so maybe I got this all wrong, but if I am right I just find it difficult to get past the 1st step - How can I bind those two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼痛 2025-01-27 10:55:46

您无需使用如果您有参考,您可以直接使用。这是一些示例代码:

CoInitialize(NULL);

...

IShellItem2* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\myPath\\myFile.ext", NULL, IID_PPV_ARGS(&item))))
{
  ULONGLONG size;
  if (SUCCEEDED(item->GetUInt64(PKEY_Size, &size))) // include propkey.h
  {
    ... use size ...
  }
  item->Release();
}

...

CoUninitialize();

如果您已经有一个 ishellitem 参考(通常,您想直接获得 ishellitem2 ),并且需要 ishellitem2 ,您可以做到这一点:

IShellItem2* item2;
if (SUCCEEDED(item->QueryInterface(&item2)))
{
    ... use IShellItem2 ...
}

另一种方法,使用 ishellitem2 ,是:

IShellItem* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\myPath\\myFile.ext", NULL, IID_PPV_ARGS(&item))))
{
  IPropertyStore* ps;
  if (SUCCEEDED(item->BindToHandler(NULL, BHID_PropertyStore, IID_PPV_ARGS(&ps))))
  {
    PROPVARIANT pv;
    PropVariantInit(&pv);
    if (SUCCEEDED(ps->GetValue(PKEY_Size, &pv)))  // include propkey.h
    {
      ULONGLONG size;
      if (SUCCEEDED(PropVariantToUInt64(pv, &size))) // include propvarutil.h
      {
        ... use size ...
      }
      PropVariantClear(&pv);
    }
    ps->Release();
  }
  item->Release();
}

You don't need to use IPropertyStore if you have an IShellItem2 reference, you can directly use IShellItem2::GetUInt64 . Here is some sample code:

CoInitialize(NULL);

...

IShellItem2* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\myPath\\myFile.ext", NULL, IID_PPV_ARGS(&item))))
{
  ULONGLONG size;
  if (SUCCEEDED(item->GetUInt64(PKEY_Size, &size))) // include propkey.h
  {
    ... use size ...
  }
  item->Release();
}

...

CoUninitialize();

If you already have an IShellItem reference (in general you want to get an IShellItem2 directly) and want a IShellItem2, you can do this:

IShellItem2* item2;
if (SUCCEEDED(item->QueryInterface(&item2)))
{
    ... use IShellItem2 ...
}

Another way of doing it, w/o using IShellItem2, is this:

IShellItem* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\myPath\\myFile.ext", NULL, IID_PPV_ARGS(&item))))
{
  IPropertyStore* ps;
  if (SUCCEEDED(item->BindToHandler(NULL, BHID_PropertyStore, IID_PPV_ARGS(&ps))))
  {
    PROPVARIANT pv;
    PropVariantInit(&pv);
    if (SUCCEEDED(ps->GetValue(PKEY_Size, &pv)))  // include propkey.h
    {
      ULONGLONG size;
      if (SUCCEEDED(PropVariantToUInt64(pv, &size))) // include propvarutil.h
      {
        ... use size ...
      }
      PropVariantClear(&pv);
    }
    ps->Release();
  }
  item->Release();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文