HomeQuestions and AnswersArticlesSamplesLibrariesForumsNewsLinks

How to add link to PIE Favorites?

By Alexander Shargin, August 18, 2004.

Question

I want to add link(s) to Favorites list of Pocket Internet Explorer. How can I do this?

Answer

Each link in Favorites list is a special *.url file located in \Storage\Windows\Favorites folder. So, to add a new link there, you need to od the following:

  1. Get path of this folder in a generic, language-independent way.
  2. Create *.url file there.

Folder path can be determined using SHGetSpecialFolderPath function with CSIDL_FAVORITES flag. As to *.url file, it's a simple ANSI text file which has the following format:

[InternetShortcut]
URL=<URL assigned to Favorites link>

Note that PIE will use name of *.url file (without extension) as display name of the link in Favorites list.

Sample code

You can use the following functions to add links to PIE Favorites, replace and remove them. AddLinkToFavorites is used to add new link to Favorites. If bFailIfExists flag is set, the function does not overwrite existing links. RemoveLinkFromFavorites deletes given link from Favorites. MakeLinkFileName is a worker function used to build full path for *.url file.

BOOL MakeLinkFileName(LPCTSTR pszName, LPTSTR pszBuf)
{
	// Get path to Favorites folder.
	if(!SHGetSpecialFolderPath(NULL, pszBuf, CSIDL_FAVORITES, TRUE))
		return FALSE;

	// Make file name.
	_tcscat(pszBuf, _T("\\"));
	_tcscat(pszBuf, pszName);
	_tcscat(pszBuf, _T(".url"));

	return TRUE;
}

BOOL AddLinkToFavorites(LPCTSTR pszName, LPCTSTR pszURL, BOOL bFailIfExists)
{
	// Get link file name.
	TCHAR szFileName[MAX_PATH];
	if(!MakeLinkFileName(pszName, szFileName))
		return FALSE;

	if(GetFileAttributes(szFileName) != 0xFFFFFFFF && bFailIfExists)
	{
		// File exists. Do not overwrite it.
		return FALSE;
	}

	DeleteFile(szFileName);

	// Create *.url file.
	FILE *pFile = _tfopen(szFileName, _T("wt"));
	if(pFile == NULL)
		return FALSE;

	_fputts(_T("[InternetShortcut]\n"), pFile);
	TCHAR *pBuf = (TCHAR*)_alloca(10+_tcslen(pszURL));
	_stprintf(pBuf, _T("URL=%s"), pszURL);
	_fputts(pBuf, pFile);
	fclose(pFile);

	return TRUE;
}

BOOL RemoveLinkFromFavorites(LPCTSTR pszName)
{
	// Get link file name.
	TCHAR szFileName[MAX_PATH];
	if(!MakeLinkFileName(pszName, szFileName))
		return FALSE;

	return DeleteFile(szFileName);
}

The following code adds "Spb Software House" link to Favorites and then removes it.

// Add link.
AddLinkToFavorites(_T("Spb Software House"), _T("http://www.spbsoftwarehouse.com"), FALSE);

...

// Remove link.
RemoveLinkFromFavorites(_T("Spb Software House"));

Discuss

Discuss this QA. Here you can write your comments and read comments of other developers.

  © 2002-2004 Smartphone Developer Network | Want to be an author? | Contact Us