This is old code and comes with no warranty.
Code:
LONG WINAPI BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData) {
CHAR tempStr[MAX_PATH];
// If the BFFM_INITIALIZEDemp message is received
// set the path to the current setting
switch (uMsg) {
case BFFM_INITIALIZED:
GetDlgItemText( (HWND) lpData,CNTRL_FOLDER,tempStr,MAX_PATH);
// Send a message to the dialog box telling it to select this path.
SendMessage(hwnd, BFFM_SETSELECTIONA, TRUE, (LPARAM) tempStr);
}
return 0; // the function should always return 0
}
void BrowseForFolder(HWND hwnd) {
BROWSEINFO bi; // structure passed to the function
LPITEMIDLIST pidl; // PIDL to the user's selection
CHAR tempStr[MAX_PATH]; // string used to temporarily hold the physical path
LONG retval; // return value
// Initialize the structure to be passed to the function.
ZeroMemory(&bi, sizeof(bi) );
bi.hwndOwner = hwnd;
bi.pszDisplayName = tempStr;
bi.lpszTitle = "Please choose a folder."; // Message displayed to the user
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LONG) hwnd;
pidl = SHBrowseForFolder(&bi);
if (pidl != 0) {
//get folder location
retval = SHGetPathFromIDList(pidl, tempStr);
//Free the pidl
CoTaskMemFree(pidl);
if (retval == TRUE) {
SetDlgItemText(hwnd,CNTRL_FOLDER,tempStr);
return;
}
}
return;
}