完整的通过ShellAPI枚举桌面目录的代码
为什么要使用ShellAPI来枚举桌面,而不是通过FindFirstFile呢?只有自己尝试过的人才能明白。而ShellAPI的使用又相对晦涩,在一番研究后终于搞明白了。特写下试验代码,以供有类似需求的人参考。
int _tmain(int argc, _TCHAR* argv[])
{
LPMALLOC pMalloc;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfDeskTop = NULL;
LPENUMIDLIST ppenum = NULL;
ULONG celtFetched;
HRESULT hr;
STRRET strDispName;
TCHAR pszDisplayName[MAX_PATH];
CoInitialize( NULL );
hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
int idx = 0;
while( (hr = ppenum->Next(1,&pidlItems, &celtFetched)) == S_OK && (celtFetched) == 1)
{
//psfDeskTop->GetDisplayNameOf(pidlItems, SHGDN_NORMAL, &strDispName); //得到相对与Desktop的路径名
//psfDeskTop->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); //得到相对于folder from which the request was made.
psfDeskTop->GetDisplayNameOf(pidlItems, SHGDN_FORPARSING, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
cout << idx++ << " - " << pszDisplayName << '/n';
//在资源管理器中打开并选中
//SHOpenFolderAndSelectItems (pidlItems, 0, NULL, 0);
//通过SHFileOperation可以完成copy delete move rename操作
//通过ShellExecuteEx完成"打开"操作,即Execute
{
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = pszDisplayName;
ShExecInfo.lpParameters = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_MAXIMIZE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
}
pMalloc->Free(pidlItems);
}
cout << "/n/n";
ppenum->Release();
pMalloc->Release();
psfDeskTop->Release();
CoUninitialize();
return 0;
}
完整的通过ShellAPI枚举桌面目录的代码
最新推荐文章于 2013-10-30 15:48:00 发布