Microsoft KB Archive/815718

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

Article ID: 815718

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







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 TASK

 * INTRODUCTION
 * Use WinInet functions
 * Clear all the files in the cache by using the WinInet functions in Visual C++ .NET
 * REFERENCES



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.

back to the top

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.

back to the top

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:  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 &quot;stdafx.h&quot; statement and before the _tmain function:  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;
 * 1) include <windows.h>
 * 2) include <wininet.h> </li>

// 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(&quot;deleted the cache entries\n&quot;); 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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

</li> To link the code successfully, add the WinInet.lib library file. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click the Q815718 project node, and then click Properties.</li> Expand Configuration Properties, expand Linker, and then click Input.</li> Type WinInet.lib in the Additional Dependencies property, and then click OK.</li></ol> </li> Press CTRL+SHIFT+S to save the project.</li> Press CTRL+SHIFT+B to build the solution.</li> Press CTRL+F5 to run the program.</li> To confirm that the temporary Internet files in your cache have been deleted, follow these steps in Microsoft Internet Explorer: <ol style="list-style-type: lower-alpha;"> On the Tools menu, click Internet Options.</li> On the General tab, click Settings under Temporary Internet files.</li> Click View Files.

Notice that all the files in the Internet Explorer cache have been deleted.</li></ol> </li></ol>

Back to the top

<div class="references_section">