CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 1999
    Posts
    14

    Q: how to get SHBrowseForFolder to return ...

    ... 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
    [email protected]

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





  2. #2
    Join Date
    May 1999
    Posts
    14

    Re: Q: how to get SHBrowseForFolder to return ...

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





  3. #3
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Q: how to get SHBrowseForFolder to return ...

    you also need to free the pidl back to the shell

    Sally


  4. #4
    Guest

    Re: Q: how to get SHBrowseForFolder to return ...

    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


  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Q: how to get SHBrowseForFolder to return ...

    works fine for me.....

    can you post your code?

    Sally


  6. #6
    Join Date
    Apr 1999
    Location
    California, USA
    Posts
    9

    Re: Q: how to get SHBrowseForFolder to return ...

    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:oBrowse(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


  7. #7
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Q: how to get SHBrowseForFolder to return ...

    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
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured