6

I have an app that needs to run on XP, but I want to be able to call ::SHCreateItemFromParsingName() which is only available from Vista onwards.

This: http://social.msdn.microsoft.com/Forums/vstudio/en-US/04eab1a9-5024-4199-b66c-8d20a83d4af4/shcreateitemfromparsingname-analog-on-xp?forum=vcgeneral

Said:

I believe you could achieve the same effect as follows: SHParseDisplayName to obtain item PIDL, then SHGetDesktopFolder, and finally IShellFolder::BindToObject

Which I've attempted:

IFileDialog *pfd;
....
PIDLIST_ABSOLUTE pidl;
HRESULT hresult = ::SHParseDisplayName(m_path, 0, &pidl, SFGAO_FOLDER, 0);
if (SUCCEEDED(hresult))
{
  IShellFolder *psf;
  hresult = ::SHGetDesktopFolder(&psf);

  if (SUCCEEDED(hresult))
  {
    IShellItem *child;
    hresult = psf->BindToObject(pidl, 0, IID_IShellFolder, (void**)&child);
    if (SUCCEEDED(hresult))
    {
      pfd->SetFolder(child);
    }
  }
}

I think I'm mainly struggling with the BindToObject (as I'm not a particularly skilled windows programmer!)

Is this even vaguely right?

4
  • 3
    The combination of functions to use is SHParseDisplayName then SHCreateShellItem. Commented Jan 2, 2014 at 15:26
  • I'll take a look at that and report back
    – steeveeet
    Commented Jan 2, 2014 at 15:53
  • Thanks, that worked a treat! If you want to put this code in an answer, I'll accept it :) PIDLIST_ABSOLUTE pidl; HRESULT hresult = ::SHParseDisplayName(m_path, 0, &pidl, SFGAO_FOLDER, 0); if (SUCCEEDED(hresult)) { IShellItem *psi; ::SHCreateShellItem(NULL, NULL, pidl, &psi); if (SUCCEEDED(hresult)) { pfd->SetFolder(psi); } }
    – steeveeet
    Commented Jan 2, 2014 at 16:04
  • 1
    Go ahead and write up the answer yourself and accept it, since you are the one who converted my suggestion into a full answer. Commented Jan 2, 2014 at 16:47

1 Answer 1

7

Based on @Raymond Chen comment, this seems to be a working replacement.

PIDLIST_ABSOLUTE pidl;
HRESULT hresult = ::SHParseDisplayName(m_path, 0, &pidl, SFGAO_FOLDER, 0);
if (SUCCEEDED(hresult))
{
  IShellItem *psi;
  hresult = ::SHCreateShellItem(NULL, NULL, pidl, &psi);
  if (SUCCEEDED(hresult))
  {
    pfd->SetFolder(psi);
  }
  ILFree(pidl);
}
1
  • 1
    Don't forget to free the memory for pidl after calling SHCreateShellItem(). Use ILFree() or CoTaskFreeMem() for that. Commented Jan 2, 2014 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.