Solved

Selecting a Directory with GetOpenFileName()

Posted on 1997-08-22
1
237 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now