Solved

Selecting a Directory with GetOpenFileName()

Posted on 1997-08-22
1
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

632 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