Home > front end >  How to retrieve an IShellItem's file size?
How to retrieve an IShellItem's file size?

Time:04-10

Given an IShellItem*, how can I find out its size? When looking around ive seen that a solution for this can be:

  1. bind IShellItem2 to the given IShellItem
  2. retrieve the IShellItem2 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 winapi 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?

CodePudding user response:

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();
}
  • Related