NS_IMETHODIMP nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) { // IUniformResourceLocator isn't supported by VC5 (bless its little heart) #if _MSC_VER < 1200 || defined (WINCE) return NS_ERROR_NOT_AVAILABLE; #else nsAutoString path; nsresult rv = aFile->GetPath(path); if (NS_FAILED(rv)) return rv; if (path.Length() < 4) return NS_ERROR_NOT_AVAILABLE; if (!StringTail(path, 4).LowerCaseEqualsLiteral(".url")) return NS_ERROR_NOT_AVAILABLE; HRESULT result; rv = NS_ERROR_NOT_AVAILABLE; IUniformResourceLocator* urlLink = nsnull; result = ::CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (void**)&urlLink); if (SUCCEEDED(result) && urlLink) { IPersistFile* urlFile = nsnull; result = urlLink->QueryInterface(IID_IPersistFile, (void**)&urlFile); if (SUCCEEDED(result) && urlFile) { result = urlFile->Load(path.get(), STGM_READ); if (SUCCEEDED(result) ) { LPSTR lpTemp = nsnull; // The URL this method will give us back seems to be already // escaped. Hence, do not do escaping of our own. result = urlLink->GetURL(&lpTemp); if (SUCCEEDED(result) && lpTemp) { rv = NS_NewURI(aURI, lpTemp); // free the string that GetURL alloc'd CoTaskMemFree(lpTemp); } } urlFile->Release(); } urlLink->Release(); } return rv; #endif //_MSC_VER < 1200 || defined (WINCE) }
long GetURLShortCut( char *linkpath, char *newpath ) { HRESULT hres; IUniformResourceLocator *pHook; CoInitialize(NULL); hres = CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator,(void**)&pHook); if (SUCCEEDED(hres)) { IPersistFile *ppf; IShellLink *psl; hres = pHook->QueryInterface (IID_IPersistFile, (void **)&ppf); //IID_IPersistFile hres = pHook->QueryInterface (IID_IShellLink, (void **)&psl); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; char *lpsz; MultiByteToWideChar(CP_ACP, 0, linkpath, -1, wsz, MAX_PATH); // Load the shortcut. hres = ppf->Load (wsz, TRUE); if (SUCCEEDED(hres)) { pHook->GetURL(&lpsz); if ( lpsz ) strcpy( newpath, lpsz ); } psl->Release(); ppf->Release(); if(!SUCCEEDED(hres)) return false; } else return false; pHook->Release (); } else return false; CoUninitialize(); return true; }
void CFavPanel::GetAllFavUrl(LPCTSTR szDir, HTREEITEM hParentItem /* = NULL */) { TCHAR szFinderDir[MAX_PATH] = {0}; _stprintf(szFinderDir, TEXT("%s*.*"), szDir); WIN32_FIND_DATA wfd = {0}; HANDLE hFinder = FindFirstFile(szFinderDir, &wfd); if (hFinder == INVALID_HANDLE_VALUE) return ; BOOL bRes = TRUE; map<CString, CString> lstFiles; while (bRes) { if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (wfd.cFileName[0] != TEXT('.')) { //HTREEITEM hItem = m_treCtrl.InsertItem(wfd.cFileName, TVI_ROOT); HTREEITEM hItem = m_treCtrl.InsertItem(wfd.cFileName, 0, 0, TVI_ROOT); TCHAR szNewDir[MAX_PATH] = {0}; //先插入目录 _stprintf(szNewDir, TEXT("%s%s\\"), szDir, wfd.cFileName); m_treCtrl.SetItemData(hItem, 1); GetAllFavUrl(szNewDir, hItem); } } else { CString str(szDir); str += wfd.cFileName; lstFiles.insert(make_pair(str, CString(wfd.cFileName))); } bRes = FindNextFile(hFinder, &wfd); } //后插入文件 CoInitialize(NULL); for (map<CString, CString>::iterator it=lstFiles.begin(); it != lstFiles.end(); it++) { IUniformResourceLocator *pUrl = NULL; HRESULT hRes = CoCreateInstance (CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (LPVOID*)&pUrl); if (SUCCEEDED(hRes)) { IPersistFile *pPF = NULL; hRes = pUrl->QueryInterface(IID_IPersistFile, (void **)&pPF); if (SUCCEEDED(hRes)) { LPTSTR pszUrl = NULL; if (S_OK == pPF->Load(it->first, STGM_READWRITE) && S_OK == pUrl->GetURL(&pszUrl) && pszUrl != NULL) { CString str = it->second; if (str.Right(4).CompareNoCase(TEXT(".url")) == 0) { str.Delete(str.GetLength() - 4, 4); } //HTREEITEM hItem = m_treCtrl.InsertItem(str, hParentItem); HTREEITEM hItem = m_treCtrl.InsertItem(str, 1, 1, hParentItem); m_treCtrl.SetItemData(hItem, (DWORD)pszUrl); //m_treCtrl.EnsureVisible(hItem); } pPF->Release(); } pUrl->Release(); } } CoUninitialize(); FindClose(hFinder); }
PFAVORITELIST CFavoriteEngine::GetFavorite(IShellFolder *pFavFolder,PFAVORITELIST& pFavoListCurFolderRoot,IEnumIDList* pItems){ if((!pFavFolder) || (!pItems)) return NULL; PFAVORITELIST pFavoListCurFolder = NULL; PFAVORITELIST pFavoListCurURL = NULL; LPITEMIDLIST pidlNext = NULL; STRRET StrRetName; STRRET StrRetFile; wstring lpszName, lpszFileName, lpszURL; HRESULT hr = S_OK , hRet = S_OK; while (hr == S_OK){ hr = pItems->Next(1, &pidlNext, NULL); if (NOERROR == hr){ pFavFolder->GetDisplayNameOf(pidlNext, SHGDN_NORMAL, &StrRetName); pFavFolder->GetDisplayNameOf(pidlNext, SHGDN_FORPARSING, &StrRetFile); StrRetToWString(StrRetName, lpszName, pidlNext); StrRetToWString(StrRetFile, lpszFileName, pidlNext); SHFILEINFO fileInfo; WIN32_FILE_ATTRIBUTE_DATA fileAttrData; if (::GetFileAttributesEx(lpszFileName.c_str(), GetFileExInfoStandard, &fileAttrData)){ //GetFileTimeValues (&fileAttrData); } SHGetFileInfo(lpszFileName.c_str(), NULL, &fileInfo, sizeof(fileInfo),SHGFI_ATTRIBUTES|SHGFI_TYPENAME); HRESULT hrSub = S_OK; if ((fileInfo.dwAttributes & SFGAO_FOLDER)!=0){ // Bind to the sub-folder and then call this function again. IShellFolder* pSubFolder=NULL; hrSub = pFavFolder->BindToObject(pidlNext, NULL, IID_IShellFolder,(LPVOID*)&pSubFolder); if (FAILED(hrSub)) continue; PFAVORITELIST PFavoList = new FAVORITELIST; ZeroMemory(PFavoList, sizeof(FAVORITELIST)); PFavoList->m_favoType = SFGAO_FOLDER; PFavoList->m_pFavoTitle = lpszName; PFavoList->m_pFavoURL = L""; if(pFavoListCurFolder){ pFavoListCurFolder->m_pNextFolder = PFavoList; PFavoList->m_pPrevFolder = pFavoListCurFolder; }else{ if(!m_pFavoListRoot) m_pFavoListRoot = pFavoListCurFolderRoot; pFavoListCurFolderRoot->m_pNextFolder = PFavoList; PFavoList->m_pPrevFolder = pFavoListCurFolderRoot; } pFavoListCurFolder = PFavoList; //Go to the sub-folder IEnumIDList* pSubItems = NULL; hrSub = pSubFolder->EnumObjects(NULL, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS, &pSubItems); if(SUCCEEDED(hrSub)){ PFAVORITELIST pFavoListCurFolderRootEx = new FAVORITELIST; ZeroMemory(pFavoListCurFolderRootEx, sizeof(FAVORITELIST)); pFavoListCurFolder->m_pSubFolder = pFavoListCurFolderRootEx; pFavoListCurFolderRootEx->m_pOwnerFolder= pFavoListCurFolder; GetFavorite(pSubFolder,pFavoListCurFolderRootEx,pSubItems); } pSubFolder->Release(); }else{ if((fileInfo.dwAttributes & SFGAO_LINK) != 0){ IUniformResourceLocator* pUrlLink = NULL; hRet = CoCreateInstance(CLSID_InternetShortcut,NULL,CLSCTX_INPROC_SERVER,IID_IUniformResourceLocator, (LPVOID*)&pUrlLink); if (SUCCEEDED(hRet)){ IPersistFile* pPersistFile = NULL; hRet = pUrlLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile); if (SUCCEEDED(hRet)){ hRet = pPersistFile->Load(lpszFileName.c_str(), STGM_READ); if (SUCCEEDED(hRet)){ static wchar_t tmp[1024*4]={}; wchar_t* ptmp = &tmp[0]; hRet = pUrlLink->GetURL(&ptmp); if(SUCCEEDED(hRet) && ptmp!=NULL){ lpszURL=ptmp; PFAVORITELIST pFavoList = new FAVORITELIST; ZeroMemory(pFavoList, sizeof(FAVORITELIST)); pFavoList->m_favoType = SFGAO_LINK; pFavoList->m_pFavoTitle = lpszName; pFavoList->m_pFavoURL = lpszURL; if(pFavoListCurURL){ pFavoListCurURL->m_pNextURL = pFavoList; pFavoList->m_pPrevURL = pFavoListCurURL; }else{ pFavoListCurFolderRoot->m_pNextURL = pFavoList; pFavoList->m_pOwnerFolder = pFavoListCurFolderRoot; } pFavoListCurURL = pFavoList; }else{ //Local short cut! } } pPersistFile->Release(); } pUrlLink->Release(); } } } } } return pFavoListCurFolder; }