Solved

Selecting a Directory with GetOpenFileName()

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now