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



The information in this article applies to:

  • Microsoft Internet Explorer (Programming)

This article was previously published under Q327569

SYMPTOMS

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

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();
}
				

STATUS

This behavior is by design.

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.

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.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites: For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:4/21/2006
Keywords:kbprb kbURLMon KB327569 kbAudDeveloper