完整的通过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;
}
为什么要使用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;
}
相关文章推荐
- c#通过纯代码创建桌面快捷方式、创建程序菜单项、将网页添加到收藏夹
- 通过shell脚本生成C代码例程
- Eclipse中的File Explorer 打不开/data目录,通过adb shell出现error错误
- jQuery EasyUI 1.2.4 API 中文文档(完整)目录
- 一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码
- shell判断文件,目录是否存在或者具有权限的代码
- 编写驱动拦截NT的API实现隐藏文件目录(代码)
- [Shell和批处理]_[枚举指定目录下的文件并处理]
- jQuery EasyUI 1.2.4 API 中文文档(完整)目录
- 通过Java代码打开浏览器,本地文件目录以及ftp站点
- 301重定向在通过iis与通过代码写文件放在根目录的差别
- [代码审计]XiaoCms(后台任意文件上传至getshell,任意目录删除,会话固定漏洞)
- C#通过代码彻底结束桌面进程explorer,解决自动重启问题
- 通过shell脚本删除代码里面定义但未使用的变量
- 通过Git WebHooks+脚本实现自动更新发布代码之Shell脚本(二)
- 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子
- Java通过QQ群的API读取群组状态的代码
- jQuery EasyUI 1.2.4 API 中文文档(完整)目录
- JQuery手动触发事件API之:通过代码看清trigger与triggerHandler的差别
- Java代码通过API操作HBase的最佳实践