Click to See Complete Forum and Search --> : Q: how to get SHBrowseForFolder to return ...


craig knutson
May 10th, 1999, 09:20 AM
... the full path that is selected. it seems to return only the last node of the path. for example:

C:\program files\devstudio\bin

if i use the dialog that it displays to work my way down to the "bin" directory, and select it, the returned value, (i.e. the pszDisplayName), is only "bin". i kind of need to know the whole thing?

maybe SHBrowseForFolder is not the routine that i should be using.

thanks for any help you can provide.

craig
cknutson@flash.net

code snipit of how i'm using it follows:


char path[MAX_PATH];
memset(path,sizeof(MAX_PATH),0x00);
CString cszTitle = _T("Please select a path");
BROWSEINFO browseinfo;
browseinfo.hwndOwner = this->GetSafeHwnd(); // HWND
browseinfo.pidlRoot = NULL; // LPCITEMIDLIST
browseinfo.pszDisplayName = path; // LPSTR
browseinfo.lpszTitle = cszTitle; // LPCSTR
browseinfo.ulFlags = 0; // UINT
browseinfo.lpfn = NULL; // BFFCALLBACK
browseinfo.lParam = NULL; // LPARAM
browseinfo.iImage = NULL; // int
SHBrowseForFolder(&browseinfo);
if (strlen(path)>0){
m_myeditControl.SetWindowText(path);
}

craig knutson
May 10th, 1999, 09:32 AM
i found the answer out here on codeguru. (after i posted my question, i found it. sorry, it wasn't in the other order, then i would not have posted this question).

here is the answer, in case you are interested:


LPITEMIDLIST pidl = SHBrowseForFolder(&browseinfo);
char path2[MAX_PATH];
BOOL bSuccess = SHGetPathFromIDList(pidl, path2);
m_myeditControl.SetWindowText(path2);

sally
May 10th, 1999, 08:42 PM
you also need to free the pidl back to the shell

Sally

Sally
May 10th, 1999, 08:42 PM
you also need to free the pidl back to the shell

Sally

May 10th, 1999, 08:56 PM
Hi,
the method is good only for local disk folder, I use this method in network enviroment, when i select a network subfolder , it will error, Why

sally
May 10th, 1999, 10:27 PM
works fine for me.....

can you post your code?

Sally

Sally
May 10th, 1999, 10:27 PM
works fine for me.....

can you post your code?

Sally

dscott
May 25th, 1999, 11:50 AM
I am seeing a little quirky behavior when I am using a UNC path and was wondering if you had seen this. When I specify an initial path such as "C:\Some Directory" this works great but if I use a UNC path it does not set the initial directory. The really wierd part is that if I select a path (UNC) and save it away and then go back into my SHBrowseForFolder() routine it will set the initial directory the second time around. It's as if I am failing to initialize something. Here is the callback routine I am using:

int __stdcall CDirDialog::BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CDirDialog* pDirDialogObj = (CDirDialog*)lpData;
CWnd* pWnd = CWnd::FromHandle(hwnd);

if (pWnd)
{
if (uMsg == BFFM_INITIALIZED )
{
if(!pDirDialogObj->m_strSelDir.IsEmpty())
{
pWnd->SendMessage(BFFM_SETSELECTION, TRUE, (LPARAM)(LPCSTR)pDirDialogObj->m_strSelDir);
}

if(!pDirDialogObj->m_strWindowTitle.IsEmpty())
{
pWnd->SetWindowText((LPCTSTR)pDirDialogObj->m_strWindowTitle);
}
}
else if(uMsg == BFFM_SELCHANGED)
{
LPITEMIDLIST pidl = (LPITEMIDLIST) lParam;
char selection[MAX_PATH];
CString strStatusText, strNewStatus;
BOOL bOk = FALSE;

if(!::SHGetPathFromIDList(pidl, selection))
selection[0] = '\0';

bOk = pDirDialogObj->SelChanged(selection, strStatusText);

strStatusText = selection;

strNewStatus = pDirDialogObj->FormatLongPath(strStatusText);

if( pDirDialogObj->m_bStatus )
pWnd->SendMessage(BFFM_SETSTATUSTEXT , 0, (LPARAM)(LPCSTR)strNewStatus);

pWnd->SendMessage(BFFM_ENABLEOK, 0, bOk);
}
}

return 0;
}

And the code that I call to initially call the routine is as follows:

BOOL CDirDialog::DoBrowse(CWnd *pwndParent)
{
LPMALLOC pMalloc = NULL;
BROWSEINFO bInfo;
LPITEMIDLIST pidl;

// Where m_strSelDir is set to the initial directory.
if(!m_strSelDir.IsEmpty())
{
m_strSelDir.TrimRight();

if( m_strSelDir.Right(1) == "\\" || m_strSelDir.Right(1) == "//" )
m_strSelDir = m_strSelDir.Left(m_strSelDir.GetLength() - 1);
}

if (SHGetMalloc (&pMalloc)!= NOERROR)
{
m_hwnd = NULL;

return FALSE;
}

ZeroMemory((PVOID)&bInfo,sizeof (BROWSEINFO));


bInfo.hwndOwner = pwndParent == NULL ? NULL : pwndParent->GetSafeHwnd();

bInfo.pszDisplayName = m_strPath.GetBuffer (MAX_PATH);

bInfo.lpszTitle = (m_strTitle.IsEmpty()) ? "Open" : m_strTitle;

bInfo.ulFlags = BIF_RETURNFSANCESTORS | (m_bStatus ? BIF_STATUSTEXT : 0);

// address of callback function
bInfo.lpfn = BrowseCtrlCallback;

// pass address of object to callback function
bInfo.lParam = (LPARAM)this;

if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
{
m_hwnd = NULL;

return FALSE;
}

m_strPath.ReleaseBuffer();

m_iImageIndex = bInfo.iImage;

if (::SHGetPathFromIDList(pidl, m_strPath.GetBuffer(MAX_PATH)) == FALSE)
{
pMalloc ->Free(pidl);

pMalloc ->Release();

m_hwnd = NULL;

return FALSE;
}

m_strPath.ReleaseBuffer();

pMalloc ->Free(pidl);

pMalloc ->Release();

m_hwnd = NULL;

return TRUE;
}

Thanks in advance for any help...
Dan

Roger Allen
May 25th, 1999, 12:01 PM
LPMALLOC pIMalloc ;
CString folder ;

if (!SUCCEEDED(::SHGetMalloc(&pIMalloc)))
{
// fail
return ;
}
char szBuff[MAX_PATH] ;
BROWSEINFO bi ;

::FillMemory(&bi, sizeof(BROWSEINFO), 0) ;

bi.hwndOwner = AfxGetMainWnd()->GetSafeHwnd() ;
bi.pidlRoot = NULL ;
bi.pszDisplayName = szBuff ;
bi.lpszTitle = "Browse for upload destination folder" ;

// only want folders
bi.ulFlags = BIF_RETURNONLYFSDIRS ;

char szPath[MAX_PATH] ;
CString upload ;

lstrcpy(szPath, "Start path you want to use") ;
bi.lParam = (LPARAM)szPath ;
bi.lpfn = BrowseCallBack ;


ITEMIDLIST *browseList = NULL ;
// returns NULL if user cancels
browseList = ::SHBrowseForFolder(&bi) ;
if (browseList != NULL)
{
::SHGetPathFromIDList(browseList, szBuff) ;
folder = szBuff ;
pIMalloc->Free(browseList) ;
}
pIMalloc->Release() ;

folder should contain the selected path


Roger Allen