您的位置:首页 > 运维架构 > Shell

完整的通过ShellAPI枚举桌面目录的代码

2009-11-10 17:36 309 查看
完整的通过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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: