一:枚举文件夹函数:
void SpecialFolder_Browse(HWND hwnd)
{
LPMALLOC g_pMalloc;
/* Gets the Shell's default allocator */
if (::SHGetMalloc(&g_pMalloc) == NOERROR)
{
BROWSEINFO bi;
LPSTR lpBuffer;
LPITEMIDLIST pidlSpecialFolder; // PIDL for Special Folder
LPITEMIDLIST pidlBrowse; // PIDL selected by user
// Allocate a buffer to receive browse information.
if ((lpBuffer = (LPSTR) g_pMalloc->Alloc(MAX_PATH)) != NULL)
{
// Get the PIDL for the Programs folder.
if (SUCCEEDED(SHGetSpecialFolderLocation(hwnd, CSIDL_DRIVES , &pidlSpecialFolder))) //第二个参数可选;
{
// Fill in the BROWSEINFO structure.
bi.hwndOwner = hwnd;
bi.pidlRoot = pidlSpecialFolder;
bi.pszDisplayName = lpBuffer;
bi.lpszTitle = "Choose a Program Group";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
// Browse for a folder and return its PIDL.
pidlBrowse = SHBrowseForFolder(&bi);
if (pidlBrowse != NULL)
{
// Show the display name, title, and file system path.
MessageBox(hwnd, lpBuffer, "Display name", MB_OK); //点击确定后显示选择的文件夹;
if (SHGetPathFromIDList(pidlBrowse, lpBuffer))
SetWindowText(hwnd, lpBuffer);
MessageBox(hwnd, lpBuffer, "Display full name", MB_OK); //显示全部路径;
// Free the PIDL returned by SHBrowseForFolder.
g_pMalloc->Free(pidlBrowse);
}
// Clean up.
g_pMalloc->Free(pidlSpecialFolder);
}
g_pMalloc->Free(lpBuffer);
}
// Release the shell's allocator.
g_pMalloc->Release();
}
}
二:枚举指定文件夹:
#include <shlobj.h>
#include <iostream>
using namespace std;
void EnumerateFolder(LPCTSTR path)
{
HRESULT hr; // COM result, you 'd better examine it in your code!
hr = CoInitialize(NULL); // initialize COM
// NOTE: usually COM would be initialized just once in your main()
LPMALLOC pMalloc = NULL; // memory manager, for freeing up PIDLs
hr = SHGetMalloc(&pMalloc);
LPSHELLFOLDER psfDesktop = NULL; // namespace root for parsing the path
hr = SHGetDesktopFolder(&psfDesktop);
// IShellFolder::ParseDisplayName requires the path name in Unicode.
OLECHAR olePath[MAX_PATH]; // wide-char version of path name
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, path, -1, olePath, MAX_PATH);
// parse path for absolute PIDL, and connect to target folder
LPITEMIDLIST pidl = NULL; // general purpose
hr = psfDesktop-> ParseDisplayName(NULL, NULL, olePath, NULL, &pidl, NULL);
LPSHELLFOLDER psfFolder = NULL;
hr = psfDesktop-> BindToObject(pidl, NULL, IID_IShellFolder, (void**)&psfFolder);
psfDesktop-> Release(); // no longer required
pMalloc-> Free(pidl);
LPENUMIDLIST penumIDL = NULL; // IEnumIDList interface for reading contents
hr = psfFolder-> EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &penumIDL);
while(1)
{
// retrieve a copy of next local item ID list
hr = penumIDL-> Next(1, &pidl, NULL);
if(hr == NOERROR)
{
WIN32_FIND_DATA ffd; // let 's cheat a bit :)
hr = SHGetDataFromIDList(psfFolder, pidl, SHGDFIL_FINDDATA, &ffd, sizeof(WIN32_FIND_DATA));
cout << "Name = " << ffd.cFileName<< endl;
cout << "Type = " << ( (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? "dir\n " : "file\n " );
cout << "Size = " << ffd.nFileSizeLow << endl;
pMalloc-> Free(pidl);
}
// the expected "error " is S_FALSE, when the list is finished
else break;
}
// release all remaining interface pointers
penumIDL-> Release();
psfFolder-> Release();
pMalloc-> Release();
CoUninitialize(); // shut down COM
}