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