How to clear the cache when your application hosts a WebBrowser control in Visual C++ .NET or in Visual C++ 2005

Article translations Article translations
Article ID: 815718 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

This 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.

Use WinInet functions

You can use the following WinInet functions to clear the cache directly:

FindFirstURLCacheEntry

You 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.


Clear all the files in the cache by using the WinInet functions in Visual C++ .NET or in Visual C++ 2005

To clear all the files in the cache by using the WinInet functions in Visual C++ .NET or in Visual C++ 2005, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the Name text box, type Q815718, type C:\Test in the Location text box, and then click OK.
  5. In Solution Explorer, double-click Q815718.cpp in the Source Files folder.
  6. 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>
  7. 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://msdn2.microsoft.com/en-us/library/ms909365.aspx			
        // 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:
    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
  8. To link the code successfully, add the WinInet.lib library file. To do this, follow these steps:
    1. In Solution Explorer, right-click the Q815718 project node, and then click Properties.
    2. Expand Configuration Properties, expand Linker, and then click Input.
    3. Type WinInet.lib in the Additional Dependencies property, and then click OK.
  9. Press CTRL+SHIFT+S to save the project.
  10. Press CTRL+SHIFT+B to build the solution.
  11. Press CTRL+F5 to run the program.
  12. To confirm that the temporary Internet files in your cache have been deleted, follow these steps in Microsoft Internet Explorer:
    1. On the Tools menu, click Internet Options.
    2. On the General tab, click Settings under Temporary Internet files.
    3. Click View Files.

      Notice that all the files in the Internet Explorer cache have been deleted.

REFERENCES

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:
http://msdn2.microsoft.com/en-us/netframework/default.aspx
For more information about the syntax of the WinInet caching functions, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms909365.aspx
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following MSDN Web site:
http://msdn.microsoft.com/ie/

Properties

Article ID: 815718 - Last Review: June 26, 2009 - Revision: 4.0
APPLIES TO
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbwebbrowser kbcominterop kbprogramming kbcode kbcaching kbapi kbhowtomaster KB815718

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com