[CPP]View PlainCopy
- Enumerate the LNK paths of applications on the desktop
- 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 ();
- }
- The corresponding absolute path is obtained by the program's LNK path
- 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
Enumerates the desktop application LNK paths and obtains the corresponding program's absolute path (using the Shgetdesktopfolder function enumeration, and then using COM to query the Ishellfolder,ishelllink and IPersistFile interfaces)