How to clear the cache when your application hosts a WebBrowser control in Visual C++ .NET or in Visual C++ 2005 (815718)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
For a Microsoft Visual Basic .NET version of this
article, see
311289. For a Microsoft Visual C# .NET version of this
article, see
326201. IN THIS TASKINTRODUCTIONThis step-by-step article describes how to use the WinInet
API functions to clear the cache
directly. You may have to clear the cache programmatically when your
application hosts a WebBrowser control. However, this feature is not available
through the interfaces of the WebBrowser control. back to the topUse WinInet functionsYou can use the following
WinInet functions to clear the cache directly: FindFirstURLCacheEntryYou use this function to find the first cache entry. FindNextUrlCacheEntry You use this function to enumerate through the cache. DeleteUrlCacheEntry You use this function to delete each entry.
Note The code samples in this article use all these
functions. Note These functions are available with Microsoft Internet
Explorer version 5.0 and later. Therefore, you must include the appropriate checks to prevent errors. The code samples in this article include the appropriate checks to prevent errors. back to the topClear all
the files in the cache by using the WinInet functions in Visual C++ .NET or in Visual C++ 2005To clear all
the files in the cache by using the WinInet functions in Visual C++ .NET or in Visual C++ 2005, follow these steps:
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- In Visual Studio .NET 2002,
click Visual C++ Projects under
Project Types, and then click Win32 Project under Templates.
In Visual Studio .NET 2003, click Visual C++ Projects under
Project Types, and then click Win32 Console
Project under Templates.
In Visual Studio 2005, click Visual C++ under
Project Types, and then click Win32 Project under Templates. - In the Name text box, type
Q815718, type
C:\Test in the Location text box, and then click OK.
- In Solution Explorer, double-click Q815718.cpp in the Source Files
folder.
- Add the following include statements in the Code window after the #include "stdafx.h" statement and before the _tmain function:
#include <windows.h>
#include <wininet.h> - Replace the code in the_tmain function
with the following code:
// Pointer to a GROUPID variable
GROUPID groupId = 0;
// Local variables
DWORD cacheEntryInfoBufferSizeInitial = 0;
DWORD cacheEntryInfoBufferSize = 0;
int *cacheEntryInfoBuffer = 0;
INTERNET_CACHE_ENTRY_INFOA *internetCacheEntry;
HANDLE enumHandle = NULL;
BOOL returnValue = false;
// Delete the groups first.
// Groups may not always exist on the system.
// For more information, visit the following Microsoft Web site:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcewinet/html/ceconwininetfunctions.asp
// By default, a URL does not belong to any group. Therefore, that cache may become
// empty even when the CacheGroup APIs are not used because the existing URL does not belong to any group.
enumHandle = FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, 0, 0, &groupId, 0);
// If there are no items in the Cache, you are finished.
if (enumHandle != NULL && ERROR_NO_MORE_ITEMS == GetLastError())
return 0;
// Loop through Cache Group, and then delete entries.
while(1)
{
// Delete a particular Cache Group.
returnValue = DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0);
if (!returnValue && ERROR_FILE_NOT_FOUND == GetLastError())
{
returnValue = FindNextUrlCacheGroup(enumHandle, &groupId, 0);
}
if (!returnValue && (ERROR_NO_MORE_ITEMS == GetLastError() || ERROR_FILE_NOT_FOUND == GetLastError()))
{
break;
}
}
// Start to delete URLs that do not belong to any group.
enumHandle = FindFirstUrlCacheEntry(NULL, 0, &cacheEntryInfoBufferSizeInitial);
if (enumHandle != NULL && ERROR_NO_MORE_ITEMS == GetLastError())
return 0;
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
internetCacheEntry = (INTERNET_CACHE_ENTRY_INFOA *)malloc(cacheEntryInfoBufferSize);
enumHandle = FindFirstUrlCacheEntry(NULL, internetCacheEntry, &cacheEntryInfoBufferSizeInitial);
while(1)
{
cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
returnValue = DeleteUrlCacheEntry(internetCacheEntry->lpszSourceUrlName);
if (!returnValue)
{
returnValue = FindNextUrlCacheEntry(enumHandle, internetCacheEntry, &cacheEntryInfoBufferSizeInitial);
}
DWORD dwError = GetLastError();
if (!returnValue && ERROR_NO_MORE_ITEMS == dwError)
{
break;
}
if (!returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize)
{
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
internetCacheEntry = (INTERNET_CACHE_ENTRY_INFOA *)realloc(internetCacheEntry, cacheEntryInfoBufferSize);
returnValue = FindNextUrlCacheEntry(enumHandle, internetCacheEntry, &cacheEntryInfoBufferSizeInitial);
}
}
free(internetCacheEntry);
printf("deleted the cache entries\n");
return 0; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in
Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the
Common Language Runtime support project setting in the right pane, click Apply, and then
click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: - To link the code successfully, add the WinInet.lib library file. To do this, follow these steps:
- In Solution Explorer, right-click the
Q815718 project node, and then click
Properties.
- Expand Configuration Properties, expand Linker, and then click Input.
- Type WinInet.lib in the
Additional Dependencies property, and then click
OK.
- Press CTRL+SHIFT+S to save the
project.
- Press CTRL+SHIFT+B to build the
solution.
- Press CTRL+F5 to run the
program.
- To confirm that the temporary Internet files in your cache
have been deleted, follow these steps in Microsoft Internet Explorer:
- On the Tools menu, click
Internet Options.
- On the General tab, click
Settings under Temporary Internet files.
- Click View Files.
Notice that all
the files in the Internet Explorer cache have been deleted.
Back to the
topREFERENCES
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
262110
HOWTO: Clear cache when your application hosts a WebBrowser control
For information about Microsoft .NET Framework development, visit the
following Microsoft Developer Network Web (MSDN) site: For more information about the syntax of the WinInet caching functions, visit the following MSDN Web site: For more information about developing Web-based
solutions for Microsoft Internet Explorer, visit the following MSDN Web
sites: Back to the
top
Modification Type: | Major | Last Reviewed: | 4/21/2006 |
---|
Keywords: | kbWebBrowser kbCOMInterop kbProgramming kbcode kbCaching kbAPI kbHOWTOmaster KB815718 kbAudDeveloper kbAudITPRO |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|