Question

MSHTML AccessViolationException error??!

Asked by: BeauT

Hello experts, I seem to be in a quandry over an error that makes no sense to me.
In following some examples I have found on the Net, I've been attempting to use the MSHTML library in vb.net to access the HTML DOM. 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


Set objDocument = objMSHTML.createDocumentFromUrl(txtURL.Text, 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 my PC and created a brand new application with only that code and get the same results.

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

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-09-21 at 10:41:15ID21998166
Tags

accessviolationexception

Topic

Microsoft Visual Basic.Net

Participating Experts
1
Points
500
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. about:blank  MSHTML
    I load the HTML document in MSHTML, and itterate through the DOM collection. Where required, my logic requires me to insert an image in the HTML data. I do it by inserting HTML code as, ImageHTML = "<IMG SRC=MyImg.gif>" mshtmldoc_item.body.insertAdjacen...
  2. MSHTML - how to??
    Hello all, I've been searching for days on the Internet to find out how to suppress images and other content from downloading with the MSHTML library. I want to use this library to parse HTML pages, but can't figure out how to get just the HTML source, and not all the embedd...
  3. Suppressing MSHTML script errors
    Hello I am using the MSHTML object in VB6 to retrieve HTML. ie ; Dim objDoc As New MSHTML.HTMLDocument Dim objMSHTML As New MSHTML.HTMLDocument Set objDoc = objMSHTML.createDocumentFromUrl(sURL, vbNullString) etc... However the code is often forced to stop due to javas...
  4. MSHTML Interface in C#
    Can anyone help me convert this code to C# (from vb)? Imports mshtml Imports System.Runtime.InteropServices <ComImport(), Guid("0000010c-0000-0000-C000-000000000046"), _ InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _ Interface IPersist Sub GetClas...
  5. WebBrowser and MSHTML (working with HTML tables)
    Hello - hoping somebody can help me out here. PROBLEM BACKGROUND: Created a web-browser with intent to take HTML innertext from HTML tables from websites and save the data to SQL Server. I want to use MSHTML so that I have fine-granular control over the HTML elements. User...
  6. MSHTML Reference
    Hi im going to build a small html editor in windows.form useing a usercontrol - in vs 2005 / net 2.0 and have been looking a bit around on the net to find the easiest approach to this. What is preferrable to use with net 2.0? AxSHDocVw.AxWebBrowser and the MSHTML/dom meth...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: TheLearnedOnePosted on 2006-09-21 at 17:46:27ID: 17574336

There are a few ways that I have found to get an HTML document from a URL:

1) Using a web browser control:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21846753.html

2) A specialized HtmlDocument class:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21767542.html

Bob

 

by: BeauTPosted on 2006-09-27 at 03:17:38ID: 17608810

Bob, thanks for looking into this for me, but the issue seems to lie in MSHTML refusing to work properly for me. No matter how I have tried calling createDocumentFromUrl, it always returns an AccessViolationException error. So, I was wondering, could it be I have a strange version of MSHTML? The version # I have is 7.0.3300.0.

Is this maybe not a typical version #?

Also, the real reason I wanted to use MSHTML is because I want to be able to programmatically log-in to a web site and then be able to navigate to other pages on that site while remaining logged in. So, I might be able to sidestep the issue completely if you can instead tell me (or point me int he right direction to find out) how to use other namespaces & objects to accomplish this. Ideally, I would love to work from the system.net namespace, since I am informed that this carries a much smaller footprint. Most of what I will be doing is programmatic and does not require a UI or any reason to actually display web pages. Rather, I am trying to log in to a site and then provide methods for programmatically navigating to various pages, extracting the desired data (as the logged-in user) and then present the extracted data on its own.

 

by: TheLearnedOnePosted on 2006-09-27 at 04:30:16ID: 17609183

I have yet to find a way to get createDocumentFromUrl to work properly, so I chose a different direction.  What did you think about the web browser control idea?

Bob

 

by: BeauTPosted on 2006-09-27 at 04:41:55ID: 17609264

Well, it looked like it still relied on the mshtml library, which for some reason won't function correctly for me. Unless I didn't understand what it was that you wanted me to look at... I would prefer to avoid the Webbrowser control altogether though, if possible. What I really need is to be able to maintain log-in credentials whenever I navigate to a new page on the same site. Sorta like the way one would log-in here to Experts Exchange and then even if you opened a new window to a new page, you would remain logged in. I am looking for a way to do that programmatically.

 

by: TheLearnedOnePosted on 2006-09-27 at 04:49:22ID: 17609319

Experts-Exchange uses cookies, so does the site that you are connecting to use cookies for credentials?  

There are tests that you can do to see if MSHTML is properly running.

Bob

 

by: BeauTPosted on 2006-09-27 at 05:18:12ID: 17609517

I'm not sure if they use cookies, but I would think so. The site I'm trying to access is MySpace, if that helps. And I would like to be able to automatically log-in and then use my log-in to be able to navigate to certain pages, while still logged in, and download the pages and parse out various elements. All this being done programmatically, rather than by the user.

The stumbling block is figuring out how to log-in and then maintain that logged-in state while navigating MySpace. Ideally, I would like to even be able to maintian several log-in credentials, allowing each to perofrm their own programmatic navigation with separate log-ins (for instance, for logging into both my personal and professional profiles).

Also, just now I did look a little more closely at the example you mentioned, using the MS Explorer control and am appearing to have success there. I'll have to play with it more to see if it'll do what I want. I still would prefer to not have to use this control at all. Some functionality i have written that does not require the user to be logged into his/her profile uses the WebClient and HTTPRequest objects fomr the system.net namespace. Is it possible to accomplish what I want using system.net objects?

 

by: BeauTPosted on 2006-09-27 at 05:19:44ID: 17609531

Sorry, I also forgot to ask, how do I test my MSHTML to see if it's working?

nd you mentioned that you also have not been able to get the createDocumentFromUrl method to work - what exception do you receive when you try it?

 

by: BeauTPosted on 2006-09-27 at 05:59:14ID: 17609787

Bob, I think I may have actually just stumbled on a workable way to use createDocumentFromUrl. Somehow, I found this article and when I copied the code directly into my IDEa, voila, it worked! I don't understand completely the why or how, but at least now I can call createDocumentFromUrl without it throwing an exception.

http://radio.javaranch.com/balajidl/2006/01/18/1137606354980.html

I still would like to know how to maintain a log-in (or even better, several log-ins) for navigation. So, any input you have on that would be appreciated. Thanks. :-)

 

by: TheLearnedOnePosted on 2006-09-27 at 06:06:14ID: 17609830

1) That code mixes IHTMLDocument and IHTMLDocument2 to achieve the desired result.

2) You didn't answer the question about how the site handles log-ins?  Does it use cookies?

Bob

 

by: BeauTPosted on 2006-09-27 at 07:28:06ID: 17610569

I don't know if it uses cookies. I was wondering if you could maybe help me figure out if they do. I did look in my cookies folder under c:\Documents and Settings\my_desktop_name\cookies to see if anything labelled as myspace seemed to update while I logged in and I also looked at the most recently modified cookie. Neither appeared to be affected from me logging in. But I don't honestly know what I am looking for.

And, it's ok with me if it mixes the document interfaces. In fact, I think I am going to create a new document object that implements all of the interfaces and wraps that code I found so I can simplify accessing web DOMs.

 

by: TheLearnedOnePosted on 2006-09-27 at 07:32:42ID: 17610609

 

by: BeauTPosted on 2006-09-27 at 08:29:02ID: 17611160

I had not heard of Fiddler before, but that sure does sound like it would do the trick. :-)

And no, it is not an https:// address. The address I use is http://login.myspace.com.

I would like to know more about ways that a website can track login information. Previously, I was completely in the dark, now I know that cookies can be used for that. Could you tell me more about how web sites do that? Also, I'm going to give you the points on the next post. You were really helpful and your posts helped move me along in the right direction.

Thanks!
Beau

 

by: TheLearnedOnePosted on 2006-09-27 at 10:09:27ID: 17612028

Fiddler is a pretty awesome tool that you can use to trace HTTP requests.  When you are going against an HTTPS URL, it is encrypted, so you can't get very much useful information.  

Any cookies that are associated with a web site are sent with the Request object on every request, so it is pretty easy to get the cookie and examine it to see if you are currently logged in or not.  Each cookie has an expiration time, and can be refreshed to keep it current.

If that doesn't make any sense, I can try explaining that differently.

Bob

 

by: BeauTPosted on 2006-09-28 at 13:33:02ID: 17622706

It doe smake sense, but I would be very grateful for any additional information/guidance you can lend on the subject, either broadly or specifically (or both?).

In either case, your help has been, and is, much appreciated, so I'm more than glad to award you the points.

 

by: TheLearnedOnePosted on 2006-09-29 at 03:54:08ID: 17626322

Some thoughts/readings about authentication + cookies:

Forms-Based Authentication
http://samples.gotdotnet.com/quickstart/aspplus/doc/formsauth.aspx

<Quote>FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, PersistCookie.Checked)

This redirects the user back to the originally requested URL. Applications that do not want to perform the redirection can call either FormsAuthentication.GetAuthCookie to retrieve the cookie value or FormsAuthentication.SetAuthCookie to attach a properly encrypted cookie to the outgoing response. These techniques can be useful for applications that provide a logon UI embedded in the containing page or that want to have more control over where users are redirected. Authentication cookies can either be temporary or permanent ("persistent"). Temporary cookies last only for the duration of the current browser session. When the browser is closed, the cookie is lost. Permanent cookies are saved by the browser and are sent back across browser sessions unless explicitly deleted by the user.</Quote>

Security Features in ASP.NET - Authentication
http://www.asp101.com/articles/cynthia/authentication/default.asp

<Quote>Forms-based authentication is used to implement customized logic for authenticating users without having to worry about session management using cookie. It gives developer more access to specify which files on the site can be accessed and by whom, and allows identification of a login page [3,7].

This mechanism will automatically redirect the unauthenticated user to login page and ask them to provide proper credentials (e.g. username/password combination). If login is successful, ASP.NET then issues the cookie to the user and redirect them to specific resources that they originally requested. This cookie allows the user to revisit particular protected resources without having to repeatedly log in.</Quote>

Bob

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...