[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Having trouble loading content in to a MSHTML.Document from a windows form.

Posted on 2008-10-15
1
Medium Priority
?
389 Views
Last Modified: 2013-11-25
I have three .net projects, an ASP.net web app, a Class Library and a Windows Form app. Both the Windows app and the ASP.net app reference the class library. However the class library is behaving differently depending on which application is calling it.

The class library (.dll) contains a function called LoadHtmlIntoDoc. This function accepts a string and loads it in to a MSHTML.HTMLDocumentClass, the HTMLDocumentClass is then used for parsing (it is never used in a UI).

I have a couple versions of the function, both version of the function are shown below (although abbreviated for simplicity....)

Initially this library was developed for use with an ASP.net web application, and BOTH functions below work great when they are called from the ASP.net application. However when the same exact functions are called from the Windows Form application neither of them work. The ASP.net app and the Windows app both link to the same exact .dll, and the augment passed to the functions is identical in both cases; but I'm getting very different results.

When Version 1 of the function is called from the Windows Form it runs, but just an empty document is loaded, So that no matter what I pass to the LoadHtmlIntoDoc function, the following is always true after the function runs :
Doc.documentElement.outerHTML = "<HTML><HEAD></HEAD><BODY><P>&nbsp;</P></BODY></HTML>"


When Version 2 of the function is called from the Windows Form iDoc2.readyState is "loading" forever, causing an infinite loop/lockup...

I have no clue why this .dll would behave differently depending on which type of application is linking to it. I really need this library to function correctly when it is called from a Windows application AND an ASP.net application.

Here is the function I'm using : (two versions of the same function, both work when called from ASP.net, neither work when called from the Windows app.)

Any help if VERY much appreciated!!
'Version 1:
 
Public Function HTMLDocumentClass(ByVal strHTML as string) As Boolean
 
	Dim hr As Integer = WinApis.CreateStreamOnHGlobal _
(Marshal.StringToHGlobalAuto(strHTML), True, stream)
 
	If (hr <> Hresults.S_OK) OrElse (stream Is Nothing) Then
		Return false
	End If
 
	Doc = New HTMLDocumentClass()
 
	persistentStreamInit = TryCast(Doc, IPersistStreamInit)
 
	If persistentStreamInit IsNot Nothing Then
		persistentStreamInit.InitNew()
		persistentStreamInit.Load(stream)
		persistentStreamInit = Nothing
		ret = True
	End If
 
	stream = Nothing
 
	Return True
 
End Function
 
 
'Version 2:
Public Function HTMLDocumentClass(ByVal strHTML as string) As Boolean
 
	Dim Doc As IHTMLDocument2
			
	Doc.clear()
	Doc.write(strHTML)
	Doc.close()
 
	While (Doc.readyState <> "complete" And _
          iDoc2.readyState <> "interactive")
		System.Threading.Thread.Sleep(0)
		System.Threading.Thread.Sleep(100)
		System.Threading.Thread.Sleep(0)
	End While
			
	Return True
 
End Function

Open in new window

0
Comment
Question by:minermadison
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
minermadison earned 0 total points
ID: 22723366
I figured out the problem. The difference between the two apps was the thread apartment state. I changed the thread in the windows app to MTA and now it works.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question