Here is an api version:
Code:
static int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData)
{
// If the BFFM_INITIALIZED message is received
// set the path to the start path.
switch (uMsg)
{
case BFFM_INITIALIZED:
{
if (NULL != lpData)
{
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
}
}
return 0; // The function should always return 0.
}
// HWND is the parent window.
// szCurrent is an optional start folder. Can be NULL.
// szPath receives the selected path on success. Must be MAX_PATH characters in length.
BOOL BrowseForFolder(HWND hwnd, LPCTSTR szCurrent, LPTSTR szPath)
{
BROWSEINFO bi = { 0 };
LPITEMIDLIST pidl;
TCHAR szDisplay[MAX_PATH];
BOOL retval;
CoInitialize();
bi.hwndOwner = hwnd;
bi.pszDisplayName = szDisplay;
bi.lpszTitle = TEXT("Please choose a folder.");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM) szCurrent;
pidl = SHBrowseForFolder(&bi);
if (NULL != pidl)
{
retval = SHGetPathFromIDList(pidl, szPath);
CoTaskMemFree(pidl);
}
else
{
retval = FALSE;
}
if (!retval)
{
szPath[0] = TEXT('\0');
}
CoUninitialize();
return retval;
}