Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: From String Dir to IShellItem ?



Permlink Replies: 1 - Last Post: Jun 27, 2014 4:54 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Asger Joergensen

Posts: 370
Registered: 11/18/08
From String Dir to IShellItem ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2014 3:47 PM
Hi

I'm trying to set the initial dir on a IFileOpenDialog looking for folders
and I saw the msdn used IKnownFolder::GetShellItem, so I thought that I could
do the same with an IShellFolder, but that creature does not have a GetShellItem
function.

So basically I want to know how to get from a path in String form to an
IShellItem so I can use IFileOpenDialog::SetDefaultFolder

   // InitDir is a function parameter
   IShellItem *isiInitDir = 0;
   IShellFolder *isfDesktop = NULL;
 
   if( InitDir.IsEmpty() == false && SUCCEEDED( SHGetDesktopFolder( &isfDesktop ) ) )
   {
      LPITEMIDLIST pidlFolder = NULL;
 
      if( SUCCEEDED( isfDesktop->ParseDisplayName(0, 0, InitDir.c_str(), 0, &pidlFolder, 0)) )
      {
         IShellFolder *isfInitDir = 0;
         hr = isfDesktop->BindToObject(pidlFolder, 0, IID_IShellFolder, (LPVOID*)&isfInitDir );
 
         if( SUCCEEDED( hr ) )
         {
            hr = isfInitDir->GetShellItem( 0, IID_PPV_ARGS( &isiInitDir ) );
 
            if( FAILED( hr ) )
               isiInitDir = 0;
 
            isfInitDir->Release();
         }
         CoTaskMemFree( pidlFolder );
      }
      isfDesktop->Release();
   }
 
 
......
 
   if( isiInitDir )
      isiInitDir->Release();

Best regards
Asger-P
MacroMenu a must have for programmer, view the video:
http://asger-p.dk/software/macromenu.php#QuickTour

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: From String Dir to IShellItem ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2014 4:54 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

I'm trying to set the initial dir on a IFileOpenDialog looking for
folders and I saw the msdn used IKnownFolder::GetShellItem, so I
thought that I could do the same with an IShellFolder, but that
creature does not have a GetShellItem function.

That is because IShellFolder predates IShellItem. However, there are many
SHCreateShellItem...() functions for getting an IShellItem from various sources:

SHCreateItemFromIDList()
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762133.aspx

SHCreateItemFromParsingName()
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762134.aspx

SHCreateItemFromRelativeName()
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762135.aspx

SHCreateItemWithParent()
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762137.aspx

SHCreateShellItem()
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762143.aspx

For instance, you could query the IShellFolder for its PIDL and pass it to
SHCreateItemFromIDList(). To get the PIDL, you can either:

a. query the IShellFolder for IPersistFolder2. If successful, call its
GetCurFolder() method.

b. query the IShellFolder for IPersistIDList. If successful, call its
GetIDList() method.

c. query the IShellFolder for IDataObject. If successful, call its GetData()
method asking for the CFSTR_SHELLIDLIST format.

d. use SHGetIDListFromObject() (http://www.geoffchappell.com/studies/windows/shell/shell32/api/shapi/getidlistfromobject.htm),
which is a wrapper for the above steps.

Alternatively, you can retreive the IShellFolder's full path/ParsingName
and pass it to SHCreateItemFromParsingName(). Since you are starting with
a path string to begin with, you could just ignore IShellFolder altogether
and pass the path to SHCreateItemFromParsingName() directly:

IShellItem *isiInitDir = NULL;
if( !InitDir.IsEmpty() )
    SHCreateItemFromParsingName(InitDir.c_str(), NULL, IID_IShellItem, (void**)&isiInitDir);
...
if( isiInitDir )
    isiInitDir->Release();


So basically I want to know how to get from a path in String form to
an IShellItem so I can use IFileOpenDialog::SetDefaultFolder

If you look at the source code for the VCL's TOpenDialog component, it uses
SHCreateItemFromParsingName() to convert the TOpenDialog::InitialDir property
into an IShellItem.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02