Monday, January 16, 2006
« Nerd Humor | Main | WinFX December CTP Install »

Many times when using the WebBrowser control, you may want to get the underlying cached file instead of using "document.innerHTML".  Using this technique avoids the HTML/JavaScript markup that IE injects into the XML file for display.  The key to doing this is using the function called GetUrlCacheEntryInfo.  Following is how you would call it from managed code. 

[UPDATE]  Using the cached file may be the best way in some cases, but another alternative is to access the "XMLDocument" expando property that IE attaches to the document element when browsing to XML documents.  Read the "Straight to XML" section this MSDN page for more details.

GetPathForCachedFile

    Public Shared Function GetPathForCachedFile(ByVal fileUrl As String) As String

        Dim cacheEntryInfoBufferSize As Integer = 0

        Dim cacheEntryInfoBuffer As IntPtr = IntPtr.Zero

        Dim lastError As Integer

        Dim result As Boolean

 

        Try

            'call to see how big the buffer needs to be

            result = GetUrlCacheEntryInfo(fileUrl, IntPtr.Zero, cacheEntryInfoBufferSize)

            lastError = Marshal.GetLastWin32Error

 

            If result = False Then

                If lastError = ERROR_FILE_NOT_FOUND Then

                    Return Nothing

                Else

                    'noop

                End If

            End If

 

            'allocate the necessary amount of memory

            cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize)

 

            'make call again with properly sized buffer

            result = GetUrlCacheEntryInfo(fileUrl, cacheEntryInfoBuffer, cacheEntryInfoBufferSize)

            lastError = Marshal.GetLastWin32Error

 

            If result = True Then

                Dim struct As Object = Marshal.PtrToStructure(cacheEntryInfoBuffer, GetType(LPINTERNET_CACHE_ENTRY_INFO))

                Dim internetCacheEntry As LPINTERNET_CACHE_ENTRY_INFO = CType(struct, LPINTERNET_CACHE_ENTRY_INFO)

                Dim localFileName As String = Marshal.PtrToStringAuto(internetCacheEntry.lpszLocalFileName)

 

                Return localFileName

            Else

                Throw New System.ComponentModel.Win32Exception(lastError)

            End If

        Finally

            If Not cacheEntryInfoBuffer.Equals(IntPtr.Zero) Then

                Marshal.FreeHGlobal(cacheEntryInfoBuffer)

            End If

        End Try

    End Function


Win32 Definitions

    Const ERROR_FILE_NOT_FOUND As Integer = 2

 

    <DllImport("Wininet.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _

    Public Shared Function GetUrlCacheEntryInfo( _

        ByVal lpszUrlName As String, _

        ByVal lpCacheEntryInfo As IntPtr, _

        ByRef lpdwCacheEntryInfoBufferSize As Integer) As Boolean

    End Function

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure FILETIME

        Public dwLowDateTime As Integer

        Public dwHighDateTime As Integer

    End Structure

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure LPINTERNET_CACHE_ENTRY_INFO

        Public dwStructSize As Integer

        Public lpszSourceUrlName As IntPtr

        Public lpszLocalFileName As IntPtr

        Public CacheEntryType As Integer

        Public dwUseCount As Integer

        Public dwHitRate As Integer

        Public dwSizeLow As Integer

        Public dwSizeHigh As Integer

        Public LastModifiedTime As FILETIME

        Public ExpireTime As FILETIME

        Public LastAccessTime As FILETIME

        Public LastSyncTime As FILETIME

        Public lpHeaderInfo As IntPtr

        Public dwHeaderInfoSize As Integer

        Public lpszFileExtension As IntPtr

        Public dwExemptDelta As Integer

    End Structure

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure SYSTEMTIME

        Public wYear As Short

        Public wMonth As Short

        Public wDayOfWeek As Short

        Public wDay As Short

        Public wHour As Short

        Public wMinute As Short

        Public wSecond As Short

        Public wMilliseconds As Short

    End Structure

 

    <DllImport("kernel32")> _

    Public Shared Function FileTimeToSystemTime( _

        ByRef lpFileTime As FILETIME, _

        ByRef lpSystemTime As SYSTEMTIME) As Boolean

    End Function

Monday, January 16, 2006 7:52:14 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Tracked by:
"http://9lt-free-porn.info/60855134/odds-of-a-royal-flush-on-video-poker.html" (... [Pingback]
"http://9lr-free-porn.info/75171489/index.html" (http://9lr-free-porn.info/75171... [Pingback]
"http://9lk-free-porn.info/41838166/cool-tigger-pics.html" (http://9lk-free-porn... [Pingback]
"http://9lm-free-porn.info/34325165/kevin-zeggers-nude.html" (http://9lm-free-po... [Pingback]
"http://9ll-free-porn.info/40261699/index.html" (http://9ll-free-porn.info/40261... [Pingback]
"http://9lm-free-porn.info/21970324/index.html" (http://9lm-free-porn.info/21970... [Pingback]
"http://9ls-free-porn.info/67964213/index.html" (http://9ls-free-porn.info/67964... [Pingback]
"http://9lq-free-porn.info/92718312/index.html" (http://9lq-free-porn.info/92718... [Pingback]
"http://9ln-free-porn.info/74369742/index.html" (http://9ln-free-porn.info/74369... [Pingback]
"http://9lm-free-porn.info/12545572/index.html" (http://9lm-free-porn.info/12545... [Pingback]
"http://9ln-free-porn.info/48220188/index.html" (http://9ln-free-porn.info/48220... [Pingback]
"http://9ls-free-porn.info/20241797/index.html" (http://9ls-free-porn.info/20241... [Pingback]
"http://9lt-free-porn.info/56022390/index.html" (http://9lt-free-porn.info/56022... [Pingback]
"http://9nb-information.info/32371413/index.html" (http://9nb-information.info/3... [Pingback]
"http://9nx-information.info/71964213/index.html" (http://9nx-information.info/7... [Pingback]
"http://9nh-information.info/68762110/index.html" (http://9nh-information.info/6... [Pingback]
"http://9ns-information.info/64037815/index.html" (http://9ns-information.info/6... [Pingback]

Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u)  

Enter the code shown (prevents robots):