Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SHBrowseForFolder crash

Status
Not open for further replies.

lasombra

Programmer
Mar 22, 2002
26
CH
I'd like to use a dialog box to browse for folder. For that I use SHBrowseForFolder. But after eselcting a folder and clicking on "OK" the application crashes. Why that? Thanks for help.

Code:
void DlgExportSettings::OnButtonExportBrowse() 
{
	BROWSEINFO m_bi;
	
	m_bi.hwndOwner	= AfxGetMainWnd()->GetSafeHwnd();
	m_bi.pidlRoot	= NULL;	
	m_bi.lpszTitle	= "TITLE";
	m_bi.ulFlags	= BIF_VALIDATE;
	m_bi.lpfn		= NULL;
//crashes here:
	LPITEMIDLIST pItemIDList = SHBrowseForFolder( &m_bi );
    if ( pItemIDList != 0 )
    {
        // get the name of the folder
        TCHAR path[MAX_PATH];
        if ( SHGetPathFromIDList ( pItemIDList, path ) )
        {
            printf ( "Selected Folder: %s\n", path );
        }

        // free memory used
        IMalloc * imalloc = 0;
        if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
        {
            imalloc->Free ( pItemIDList );
            imalloc->Release ( );
        }
    }

}
 
Have you read these remarks which list some important pre-conditions to calling this function?

> the application crashes
This is way too vague - actual error messages are much better.

--
 
here's a function that I made called GetFolder() that I use to return the path of the folder in a CString:

Code:
CString GetFolder()
{
            LPMALLOC pMalloc,pMalloc2;
            CString strDirectory;
			BROWSEINFO bi;
			/* Gets the Shell's default allocator */
			char pszBuffer[MAX_PATH];
			LPITEMIDLIST pidl;
			// Get help on BROWSEINFO struct - it's got all the bit settings.
			bi.hwndOwner = GetDesktopWindow();
			bi.pidlRoot = NULL;
			bi.pszDisplayName = pszBuffer;
			bi.lpszTitle = _T("Select A Directory");
			bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
			bi.lpfn = NULL;
			bi.lParam = 0;

			
			if (::SHGetMalloc(&pMalloc) == NOERROR)
			{
				if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
				{
					if (::SHGetPathFromIDList(pidl, pszBuffer))
					{ 
						strDirectory = pszBuffer;
					}
					pMalloc->Free(pidl);
				}
				pMalloc->Release();
			}
                                                return strDirectory;
}

BlackDice

 
Thanks for the help. I have to use the BROWSEINFO as follow:
Code:
BROWSEINFO m_bi = {0};
and then initialize it:
Code:
m_bi.lpszTitle	 = strTitle;
m_bi.ulFlags	 = BIF_VALIDATE | 64;

Then it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top