Jump to Real's How-to Main page

Browse for a folder using the standard Win dialog

[External FUNCTION declaration]
FUNCTION long SHGetPathFromIDListA (long pidl, ref string pszPath) &
LIBRARY "shell32.dll"
FUNCTION long SHBrowseForFolderA ( BROWSEINFO lpBrowseInfo)  &
LIBRARY "shell32.dll"

[structure BROWSEINFO]
howner           long
pidlroot         long
pszdisplayname   string
lpsztitle        string
ulflags          long
lpfn             long
lparam           long
iimage           long

[Powerscript]
browseinfo lst_bi
long       ll_pidl
int        li_thispos
string     ls_path, ls_xs
long       ll_rc

ls_path=space(512)      // maximum path lenght
lst_bi.howner=handle(this)  // handle of the window
lst_bi.pidlRoot = 0
lst_bi.pszdisplayname = space(260)
lst_bi.lpszTitle = "Select a folder"
lst_bi.ulFlags = 1
ll_pidl = SHBrowseForFolderA(lst_bi)
ll_rc = SHGetPathFromIDListA(ll_pidl, ls_path)
IF NOT ll_rc > 0 THEN
   SetNull(ls_path)
END IF
MessageBox("Folder selected" , ls_path)
NOTE: This may cause some memory leaks. To fix them, use the following API.
[external function declaration]
SUBROUTINE CoTaskMemFree( long lpv ) LIBRARY "ole32.dll"

[powerscript]
SHBrowseForFolder( lst_bi )
CoTaskMemFree( lst_bi.pidlRoot )

If you find this article useful, consider making a small donation
to show your supportfor this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]