Help and Support

PRB: DeleteUrl() Does Not Delete the Internet Explorer History Folder Entry

Article ID:327569
Last Review:December 25, 2006
Revision:2.1
This article was previously published under Q327569
On This Page

SYMPTOMS

After you call the IUrlHistoryStg::DeleteUrl method for a URL, the URL entry still appears in the Internet Explorer History Shell folder.

Back to the top

RESOLUTION

Use the IContextMenu::InvokeCommand method to delete the items. This is similar to manually deleting the item. The disadvantage to this method is that you cannot disable the confirmation dialog box that appears.

The source code that follows demonstrates the resolution by deleting the first, top-level History Shell folder. Typically, this is the oldest day in the history (for example, Tuesday). You can modify the code to delete a more specific item.
// Error checking minimized for clarity.
void DeleteUrlFromHistoryShell()
{
	HRESULT hr;

	// Call this if needed.
	CoInitialize( NULL );
	
	IShellFolder* pDesktopFolder = NULL;
	IMalloc* pMalloc = NULL;

	hr = ::SHGetMalloc(&pMalloc);

	// Get desktop folder.
	hr = ::SHGetDesktopFolder(&pDesktopFolder);

	// Get the history folder.
	ITEMIDLIST* pidlHistoryFolder = NULL;
	hr = ::SHGetSpecialFolderLocation(NULL, CSIDL_HISTORY, &pidlHistoryFolder);

	// Get the IShellFolder of the history folder.
	IShellFolder* pHistoryFolder = NULL;
	hr = pDesktopFolder->BindToObject(pidlHistoryFolder, NULL, IID_IShellFolder, (void**)&pHistoryFolder);

	// Enumerate the history items.
	IEnumIDList* pHistoryEnum = NULL;
	hr = pHistoryFolder->EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pHistoryEnum);

	ITEMIDLIST* pidl = NULL;
	ULONG fetched = 0;

	hr = pHistoryEnum->Next(1, &pidl, &fetched);

	if (SUCCEEDED(hr))
	{
		const ITEMIDLIST* pidl2 = pidl;

		// Get the IContextMenu interface.
		IContextMenu* pContextMenu = NULL;
		hr = pHistoryFolder->GetUIObjectOf(NULL, 1, &pidl2, IID_IContextMenu, NULL, (void**)&pContextMenu);

		if (SUCCEEDED(hr))
		{
			CMINVOKECOMMANDINFO pCommandInfo = { 0 };

			pCommandInfo.cbSize = sizeof(CMINVOKECOMMANDINFO);
			pCommandInfo.lpVerb = _T("delete");
			pCommandInfo.fMask = CMIC_MASK_FLAG_NO_UI; // has no effect
			hr = pContextMenu->InvokeCommand(&pCommandInfo);
		}

		pContextMenu->Release();
	}

	pHistoryEnum->Release();
	pHistoryFolder->Release();
	pMalloc->Release();
	pDesktopFolder->Release();
}
				

Back to the top

STATUS

This behavior is by design.

Back to the top

MORE INFORMATION

The IUrlHistoryStg2::ClearHistory method clears the deleted history items from the Internet Explorer History Shell folder and internally. DeleteUrl is not designed to delete the Internet Explorer History Shell folder entries. Because many internal functions rely on this method, its behavior cannot be changed.

Back to the top

Steps to Reproduce the Behavior

1.Create a console application, and then add the following code:
#include <atlbase.h>
#include <shlobj.h>
#include <UrlHist.h>

// Error checking minimized for clarity.
int main(int argc, char* argv[])
{
	USES_CONVERSION;
	CoInitialize(NULL);

	IUrlHistoryStg2* pUrlHistoryStg2 = NULL;
	HRESULT hr = CoCreateInstance(CLSID_CUrlHistory,
		NULL, CLSCTX_INPROC, IID_IUrlHistoryStg2,
		(void**)&pUrlHistoryStg2);

	IEnumSTATURL* pEnumURL;
	hr = pUrlHistoryStg2->EnumUrls(&pEnumURL);

	STATURL suURL;
	ULONG pceltFetched;
	suURL.cbSize = sizeof(suURL);
	hr = pEnumURL->Reset();

	while((hr = pEnumURL->Next(1, &suURL, &pceltFetched)) == S_OK)
	{
		hr = pUrlHistoryStg2->DeleteUrl(suURL.pwcsUrl, 0);
		printf("\"%s\" deleted.\n", W2T(suURL.pwcsUrl));
	}

	pEnumURL->Release();
	pUrlHistoryStg2->Release();
	CoUninitialize();
}
					
2.Build and run the application. Notice that the first time that you run the code, your history is deleted. The second time that you run the code, the previous history entries are not there anymore.

Back to the top

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
IContextMenu Interface
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/Shell/reference/ifaces/icontextmenu/icontextmenu.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/Shell/reference/ifaces/icontextmenu/icontextmenu.asp)

IUrlHistoryStg::DeleteUrl Method
http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/urlhist/iurlhistorystg/deleteurl.asp (http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/urlhist/iurlhistorystg/deleteurl.asp)
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
http://msdn2.microsoft.com/en-us/ie/default.aspx (http://msdn2.microsoft.com/en-us/ie/default.aspx)

http://support.microsoft.com/iep (http://support.microsoft.com/iep)

Back to the top


APPLIES TO
Microsoft Internet Explorer (Programming)

Back to the top

Keywords: 
kbprb kburlmon KB327569

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.