Solved

Selecting a Directory with GetOpenFileName()

Posted on 1997-08-22
1
242 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 Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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