Folder Selection API Function
If you read my last two API tips, you noticed that the dialogs which functions loaded, could be loaded using comctl32.ocx control. However not all common dialog windows can be loaded this way. "Browse For Folder" is one of them. In this tip, I'll show how to load this dialog using two API functions: SHBrowseForFolder and SHGetPathFromIDList
You need two functions because loading this dialog is a two step process. First, you call SHBrowseForFolder function to obtain a so called 'pointer to ID List'. Second, you call SHGetPathFromIDList to extract a folder path from this 'ID List'. First comes the declaration of SHBrowseForFolder (remember, this function is not present in API Viewer!!!)::
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
All this code above has to be declared in one line. As you can see, the function uses BROWSEINFO structure which looks like this:
Public Type BROWSEINFO
Copy BROWSEINFO to the module together with the functions. The only members of BROWSEINFO we are going to use are hOwner, pidlRoot, lpszTitle, and uFlags. Other stuff is not needed.
Next function, SHGetPathFromIDList, will extract the folder path together with the name from the 'ID list' returned by the SHBrowseForFolder function. The declaration (not present in API Viewer) looks like this (has to be in one line):
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
pidl is the return value of SHBrowseForFolder function, and pszPath is a string where the path of the folder will be saved.
Display Browse For Folder dialog:
Private Sub cmdBrowse_Click()
You should see a message box with the folder name you selected. To make this more interesting, experiment by changing uFlags to one of the following:
Public Const BIF_RETURNONLYFSDIRS = &H1 ' - file system folders only
Next week: Network Dialogs
Made By Laimonas Simutis. 2001. firstname.lastname@example.org