首页 > 代码库 > 枚举桌面应用程序lnk路径并得到对应程序的绝对路径(使用SHGetDesktopFolder函数枚举,然后使用COM查询IShellFolder,IShellLink和IPersistFile接口)
枚举桌面应用程序lnk路径并得到对应程序的绝对路径(使用SHGetDesktopFolder函数枚举,然后使用COM查询IShellFolder,IShellLink和IPersistFile接口)
[cpp] view plain copy
- // 枚举桌面上应用程序的lnk路径
- void EnumDesktopLnkPath()
- {
- WCHAR szPath[MAX_PATH] = {0};
- SHGetSpecialFolderPathW(NULL, szPath, CSIDL_DESKTOPDIRECTORY, 0);
- IShellFolder* pShell = NULL;
- if(FAILED(SHGetDesktopFolder(&pShell)))
- {
- return ;
- }
- IShellFolder* pFolder = NULL;
- ITEMIDLIST* pItem = NULL;
- if(FAILED(pShell->ParseDisplayName(NULL, NULL, szPath, NULL, &pItem, NULL)))
- {
- return ;
- }
- if(FAILED(pShell->BindToObject(pItem, NULL, IID_IShellFolder, (LPVOID*)&pFolder)))
- {
- return ;
- }
- pShell->Release();
- if(NULL == pFolder)
- {
- return ;
- }
- IEnumIDList* pEnum = NULL;
- if(FAILED(pFolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &pEnum)))
- {
- return ;
- }
- pItem = NULL;
- ULONG ulFetched = 0;
- WCHAR szLnkPath[MAX_PATH] = {0};
- WCHAR szLnkName[MAX_PATH] = {0};
- LPCWSTR pszLnkName(L".lnk");
- WIN32_FIND_DATAW fd = {0};
- while(S_OK == pEnum->Next(1, &pItem, &ulFetched))
- {
- SHGetDataFromIDListW(pFolder, pItem, SHGDFIL_FINDDATA, &fd, sizeof(fd));
- swprintf(szLnkName, L"%s", fd.cFileName);
- if(0 == wcscmp(wcsrchr(szLnkName, L‘.‘), pszLnkName))
- {
- swprintf(szLnkPath, L"%s//%s", szPath, szLnkName);
- AfxMessageBox(CString(szLnkPath));
- //ParseLnk2Path(szLnkPath);
- }
- }
- pEnum->Release();
- pFolder->Release();
- }
- // 由程序的Lnk路径得到对应的绝对路径
- BOOL ParseLnk2Path(LPCWSTR pszLnkName)
- {
- ::CoInitialize(NULL);
- IShellLink* pShell = NULL;
- IPersistFile* pFile = NULL;
- if(S_OK == ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pShell))
- {
- if(S_OK == pShell->QueryInterface(IID_IPersistFile, (LPVOID*)&pFile))
- {
- pFile->Load(pszLnkName, STGM_READWRITE);
- TCHAR szPath[MAX_PATH] = {0};
- pShell->GetPath(szPath, MAX_PATH, NULL, SLGP_UNCPRIORITY);
- AfxMessageBox(CString(szPath));
- }
- }
- pShell->Release();
- pFile->Release();
- ::CoUninitialize();
- return TRUE;
- }
http://blog.csdn.net/visualeleven/article/details/6433330
枚举桌面应用程序lnk路径并得到对应程序的绝对路径(使用SHGetDesktopFolder函数枚举,然后使用COM查询IShellFolder,IShellLink和IPersistFile接口)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。