MSHTML System.AccessViolationException

Hello all, I seem to be in a quandry over an error that makes no sense to me.
In following some examples I have found on theNet, I've been attempting to use the MSHTML library in vb.net to access the HTMLDOM. However, the following code (copied directly from this article:http://www.codeguru.com/vb/vb_internet/html/article.php/c4815/)...

Dim objMSHTML As New MSHTML.HTMLDocument
Dim objDocument As MSHTML.HTMLDocument

objDocument = objMSHTML.createDocumentFromUrl(txtURL.T

ext, vbNullString)

produces this error:
A first chance exception of type 'System.AccessViolationException' occurred in mscorlib.dll

And

when I look at the error message, it tells me that memory could be

corrupt elsewhere. I've attempted this line of code by omitting the "http://"

portion of the URL, byt trying numerous web sites, and with various

other arguments in the 2nd parameter, such as "", ControlChars.NullChar

and "null". I've also reset myPC and created a brand newapplication with only that code and get the same results.

I am using VS.NET 2005, version 8.0.50727.42

I don't know if it will help, but the details from the exception object are as follows:

System.AccessViolationException was caught
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="mscorlib"
StackTrace:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at mshtml.HTMLDocumentClass.createDocumentFromUrl(String bstrUrl, String bstrOptions)
at MSHTML_DOM_Practice_1.Form1.createDoc(String URL) in D:\Dev V.2\Misc practice projects\MSHTML DOM Practice 1\MSHTML DOM Practice 1\Form1.vb:line 19

I'm reaching my threshold of frustration and could really use some help!

Thanks,
Beau

[3552 byte] By [Beauner] at [2007-12-24]
# 1

To use MSHTML you need to set up the IPersistStream in the HTMLDocument class. You can do this by casting the HTMLDocument class to IPersistStream and initializing the stream. Here is some sample code that works for me:

Imports System.Runtime.InteropServices

<ComVisible(True), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), _

InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _

Public Interface IPersistStreamInit

Sub GetClassID(ByRef pClassID As Guid)

<PreserveSig()> Function IsDirty() As Integer

<PreserveSig()> Function Load(ByVal pstm As UCOMIStream) As Integer

<PreserveSig()> Function Save(ByVal pstm As UCOMIStream, _

ByVal ByValByValfClearDirty As Boolean) As Integer

<PreserveSig()> Function GetSizeMax(<InAttribute(), Out(), _

MarshalAs(UnmanagedType.U8)> ByRef pcbSize As Long) As Integer

<PreserveSig()> Function InitNew() As Integer

End Interface

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim urlx As String = "http://www.microsoft.com/"

Dim htmldoc As New mshtml.HTMLDocument()

Dim htmlresult As mshtml.IHTMLDocument2

Dim iPersistStream As IPersistStreamInit

iPersistStream = DirectCast(htmldoc, IPersistStreamInit)

iPersistStream.InitNew()

htmlresult = htmldoc.createDocumentFromUrl(urlx, vbNullString)

Do Until htmlresult.readyState = "complete"

Application.DoEvents()

Loop

MsgBox(htmlresult.body.innerHTML())

End Sub

End Class

TimothyNgMSFT at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...