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)
}
Esempio n. 2
0
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;
}
BOOL CShortcutNBookmark::CreateInternetShortcut(const CString& LinkFilename, const CString& Argument, CString csTargetFolder, CString csIconLocalPath)
{
	CoInitialize(NULL);
//	const CString PathLink = GetSpecialFolderLocation(CSIDL_FAVORITES) + "\\" + LinkFilename;
	const CString PathLink = csTargetFolder + "\\" + LinkFilename;
	
	// #include <intshcut.h>?
	IUniformResourceLocator *pURL;
	IPersistFile *pPf;
	WCHAR wszSCPath[MAX_PATH]={0,};
	HRESULT hr;
	
	// 
	hr=CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,IID_IUniformResourceLocator, (void **)&pURL);
	if (FAILED(hr)) return FALSE;
	ATLASSERT(SUCCEEDED(hr));

	hr = pURL->SetURL(Argument,IURL_SETURL_FL_GUESS_PROTOCOL );
	ATLASSERT(SUCCEEDED(hr));

	hr=pURL->QueryInterface(IID_IPersistFile, (void **)&pPf);
	if (FAILED(hr)) {
		pURL->Release();
		::CoUninitialize();
		return FALSE;
	}
	ATLASSERT(SUCCEEDED(hr));

	MultiByteToWideChar(CP_ACP, 0, (LPCSTR) PathLink, -1, wszSCPath, MAX_PATH);
	hr = pPf->Save(wszSCPath,TRUE);
	ATLASSERT(SUCCEEDED(hr));

	// set the property : icon
	CComPtr<IPropertySetStorage> spIPropertySetStorage;
	hr=pURL->QueryInterface(IID_IPropertySetStorage, (void **)&spIPropertySetStorage);
	if (FAILED(hr)) {
		pURL->Release();
		::CoUninitialize();
		return FALSE;
	}
	ATLASSERT(SUCCEEDED(hr));
//	CComPtr<IPropertyStorage> spIPropertyStorageNew;
//	hr=spIPropertySetStorage->Create(FMTID_Intshcut,&FMTID_Intshcut,PROPSETFLAG_DEFAULT,STGM_READWRITE,&spIPropertyStorageNew);
//  return value says E_NOTIMPL                        _HRESULT_TYPEDEF_(0x80004001L)
	CComPtr<IPropertyStorage> spIPropertyStorage;
	hr = spIPropertySetStorage->Open(FMTID_Intshcut, STGM_READWRITE, &spIPropertyStorage);
	if (FAILED(hr)) {
		pURL->Release();
		::CoUninitialize();
		return FALSE;
	}
	ATLASSERT(SUCCEEDED(hr));
	// Writing Property 
	PROPSPEC propspecw[2];
	//PROPSPEC ppids[2] = { {PRSPEC_PROPID, PID_IS_ICONINDEX}, {PRSPEC_PROPID, PID_IS_ICONFILE} };
	propspecw[0].ulKind = PRSPEC_PROPID;
	propspecw[0].propid = PID_IS_ICONINDEX;  
	propspecw[1].ulKind = PRSPEC_PROPID;
	propspecw[1].propid = PID_IS_ICONFILE;  
	PROPVARIANT propvarw[2];
	PropVariantInit(&propvarw[0]);
	PropVariantInit(&propvarw[1]);
	propvarw[0].vt = VT_I4;
	propvarw[0].lVal = 0;
	propvarw[1].vt = VT_LPWSTR;
	//propvarw[1].pwszVal = L"D:\\CProjects\\GShortcut\\shortcutjp.ico"; //works
	//propvarw[1].pwszVal = L"http://mfiles.naver.net/9b4fac7b613b57e4c9b1/data33/2008/6/28/55/tulip-jangtimjang.ico"; // this also works	 !!!!!!
	if ( csIconLocalPath.GetLength() > 0 ) propvarw[1].pwszVal = CComBSTR(csIconLocalPath);
	else propvarw[1].pwszVal = CComBSTR(GetThisEXEFullPath());
	//::MessageBox(NULL, GetThisEXEFullPath(), "file path", MB_OK);
	hr = spIPropertyStorage->WriteMultiple(2, propspecw, propvarw, 0); 
	ATLASSERT(SUCCEEDED(hr));
	hr = spIPropertyStorage->Commit( STGC_OVERWRITE);	//STGC_OVERWRITE  STGC_DEFAULT 
	ATLASSERT(SUCCEEDED(hr));
//	PropVariantClear(&propvarw[0]);
//	PropVariantClear(&propvarw[1]);
	// */

	// save once again
	 hr = pPf->Save(NULL,TRUE);
	 ATLASSERT(SUCCEEDED(hr));

	// veryfy the value : Reading Property Sample
	//PROPSPEC propspec;
	//propspec.ulKind = PRSPEC_PROPID;
	//propspec.propid = PID_IS_ICONFILE;  
	//PROPVARIANT propvar;
	//propvar.vt = VT_LPWSTR;
	//hr = spIPropertyStorage->ReadMultiple(1, &propspec, &propvar);
	//CString csPropValue =	propvar.pwszVal;	

	pURL->Release();
	pPf->Release();
	
	::CoUninitialize();


/*  //??? ?? ?? ??

	LPMALLOC pMalloc;
	LPITEMIDLIST lpidl;
	//char szSCPath[MAX_PATH];
	char szShortcutInfo[MAX_PATH];
	DWORD dwWritten;

  // ???? ???? ??? ???. ?? ??? ?????? ???? ??? ? ??? ?????. 
  //const CString PathLink = GetSpecialFolderLocation(CSIDL_FAVORITES) + "\\" + LinkFilename;
  CString szSCPath = GetSpecialFolderLocation(CSIDL_FAVORITES);
  
	//  if (nFolder != -1) {
	//   SHGetSpecialFolderLocation(HWND_DESKTOP, nFolder, &lpidl);
	//   SHGetPathFromIDList(lpidl, szSCPath);
	//   strcat(szSCPath, "\\");
	//    strcat(szSCPath, szFileName);
	//   SHGetMalloc(&pMalloc);
	//   pMalloc->Free(lpidl);
	//   pMalloc->Release();
	//  }
	//  if (nFolder == -1) {
	//   strcpy(szSCPath, szFileName);
	//  }
	
	  // ???? ??? ??? ???.
	  wsprintf(szShortcutInfo, "[InternetShortcut]nURL=%snIconIndex=0nIconFile=%sn","Modified=000000000000000000n",URL,IconPath);
	  
		// ??? ??? ????.
		HANDLE hFile = CreateFile(szSCPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,  FILE_ATTRIBUTE_NORMAL, NULL);
		if (!hFile) {
		return FALSE;
		}
		WriteFile(hFile, szShortcutInfo, strlen (szShortcutInfo), &dwWritten, NULL);
		CloseHandle(hFile);
		
	//*/
	

	return TRUE;
}
Esempio n. 4
0
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);
}
Esempio n. 5
0
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;
}