. Home Feedback Contents Search

Directory Browse 

Up Next

Browse For A Directory

Here’s an example of how to use SHBrowseForFolder(...) to browse for a folder. This code will return whatever was returned from the browsing operation.

CString csGetFolder()
   {
   CString       csReturn;
   BROWSEINFO    bi;
   LPITEMIDLIST  pidl     = 0;
   LPMALLOC      pMalloc  = 0;

   do {
       // Instantiate the MALLOC structure.
       // If this fails then don't go any further.

       if (FAILED(SHGetMalloc(&pMalloc)))
           break;

       // Initialize the structure.

       ZeroMemory(&bi,sizeof(bi));
       bi.hwndOwner = NULL;
       bi.pszDisplayName = 0;
       bi.pidlRoot = 0;
       bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;

       // Turn control over to the browsing function.
       // If the user aborts out of the browse then
       // nothing further to be done.

       if (!(pidl = SHBrowseForFolder(&bi)))
           break;

       // The browse ran to a successful completion.
       // Extract the user's choice and put into the
       // CString that will be returned.

       SHGetPathFromIDList( pidl, csReturn.GetBuffer( _MAX_PATH ) );
       csReturn.ReleaseBuffer();

       } while (false);

   // Clean up allocated resources.

   if ( pidl )
       pMalloc->Free(pidl);

   if ( pMalloc )
       pMalloc->Release();

   return csReturn;
   }

Up Next

Hit Counter