Solved

Selecting a Directory with GetOpenFileName()

Posted on 1997-08-22
1
239 Views
Last Modified: 2013-12-03
I need to know if there is a way to use GetOpenFileName() to select a directory (not a file).
When press ok to a directory windows open the directory so I cannot select it (say in order to copy files to that directory)
Is there any other function that uses the explorer style dialog to do this job ?
0
Comment
Question by:panos
1 Comment
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1404715
Yes, there is a Win32 function SHBrowseForFolder. You may see more information in Win32 documentation about Shell Name Space.

The following code is a part of my C++ program that may be helpful for you.

#include <objbase.h>
#include <shlobj.h>
                       // pointer to the shell's IMalloc interface.
                           LPMALLOC pMalloc;

                       // Get the shell's allocator.
                           if (SUCCEEDED(::SHGetMalloc(&pMalloc)))
                       {
                           // Allocate a buffer to receive browse information.
                       LPSTR lpBuffer;
                           if ((lpBuffer = (LPSTR)pMalloc->Alloc(MAX_PATH)) != NULL)
                       {
                       // Get the PIDL for the Programs folder.
                           LPITEMIDLIST pidlPrograms; // PIDL for Programs folder
                           if (SUCCEEDED(::SHGetSpecialFolderLocation(hwnd,
                       CSIDL_PROGRAMS,
                       &pidlPrograms)))
                       {
                           // Fill in the BROWSEINFO structure.
                           BROWSEINFO bi =
                       {
                       pWnd->GetSafeHwnd(),
                           pidlPrograms,
                           lpBuffer,
                           "Choose a Program Group",
                           0,
                           NULL,
                           0
                       };
                           
                           // Browse for a folder and return its PIDL selected by user.
                           LPITEMIDLIST pidlBrowse = ::SHBrowseForFolder(&bi);
                           if (pidlBrowse != NULL)
                       {
                           // Show the display name (SHGDN_NORMAL or SHGDN_INFOLDER),
                       // title, and file system path.
                           pWnd->MessageBox(lpBuffer, "Display name", MB_OK);
                           
                       if (::SHGetPathFromIDList(pidlBrowse, lpBuffer))
                           pWnd->MessageBox(lpBuffer, "Path name", MB_OK);
                         
                           // Free the PIDL returned by SHBrowseForFolder.
                           pMalloc->Free(pidlBrowse);
                       }
                         
                           // Free the PIDL returned by SHGetSpecialFolderLocation
                       pMalloc->Free(pidlPrograms);
                       }

                           // Clean up.
                           pMalloc->Free(lpBuffer);
                       }

                       // Release the shell's allocator.
                           pMalloc->Release();
                       }
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question