Can anyone give an example code of SHBrowseForFolder function???

closed account (3hMz8vqX)
Hi all,
Can anyone give an example code of SHBrowseForFolder function???

Thankyou everyone in advance:)
The first Google search result is the MSDN site, which has a full example written years ago specifically for you:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762115(v=vs.85).aspx


See below post.
Last edited on
Perversely, the code samples in the MSDN entry for SHBrowseForFolder do not show the basic usage (or any other usage) of the function.

But this page does:

How is SHBrowseForFolder() used?
http://vcfaq.mvps.org/sdk/20.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    BROWSEINFO bi = { 0 };
    bi.lpszTitle = _T("Pick a Directory");
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
    if ( pidl != 0 )
    {
        // get the name of the folder
        TCHAR path[MAX_PATH];
        if ( SHGetPathFromIDList ( pidl, path ) )
        {
            _tprintf ( _T("Selected Folder: %s\n"), path );
        }

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


(Note that this code is backward compatible with Windows 95/Windows NT 4.0. If you only need to support Windows 2000 and newer, then pidl can be freed using CoTaskMemFree.)

Andy

PS And as SHBrowseForFolder is a COM based function, you need to call CoInitializeEx (with COINIT_APARTMENTTHREADED), CoInitialize, or OleInitialize before you use it. And CoUninititialize/OleUninititialize after you're done with it.
Last edited on
Topic archived. No new replies allowed.