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();
}
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.
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.
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.