473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

UCOMIStream, MSHTML and WebBrowser control Persistence Problem

Sorry for the cross post, but I'm not sure who is best placed to answer this
one.

This is the most bizarre behaviour of MSHTML and streams.

I have a WebBrowser control that contains nothing but some default HTML. I
want to copy the document and modify it before saving it to disk.

So, I clone the document like this:

<code>
Private Function CloneDocument(ByVal doc As mshtml.IHTMLDocument2) As
mshtml.IHTMLDocument2

Dim newdoc2 As mshtml.IHTMLDocument2
Dim ips As IPersistStreamInit

Dim strm As UCOMIStream

Dim source As String

' Create and initialise a new document object
newdoc2 = New mshtml.HTMLDocument

ips = DirectCast(newdoc2, IPersistStreamInit)

ips.InitNew()

Do Until newdoc2.readyState = "complete"
Application.DoEvents()
Loop

' Get the current document HTML as a stream
source = GetDocumentSource(AxWebBrowser1.Document) ' see below
strm = GetStream(source) ' see below

' Load the new document from the stream
ips.Load(strm)

' Wait until the new document has settled
Do Until newdoc2.readyState = "complete"
Application.DoEvents()
Loop

Return newdoc2

End Function

Private Function GetStream(ByVal s As String) As UCOMIStream

Dim iptr As IntPtr
Dim istrm As UCOMIStream

' Get a pointer to the string
iptr = Marshal.StringToHGlobalAuto(s)

' Create the stream from the pointer
CreateStreamOnHGlobal(iptr, True, istrm)

Return istrm

End Function
Private Function GetStream(ByVal size As Integer) As UCOMIStream

Dim iptr As IntPtr
Dim strm As UCOMIStream

' Create a pointer to a block of the required size
iptr = Marshal.AllocHGlobal(size)

' Create the stream from the pointer
CreateStreamOnHGlobal(iptr, True, strm)

Return strm

End Function

Private Function GetDocumentSource(ByVal doc As mshtml.IHTMLDocument2,
ByVal resetIsDirty As Boolean) As String

Dim stream As UCOMIStream

Dim ips As IPersistStreamInit

Dim s As String

ips = DirectCast(doc, IPersistStreamInit)

If ips Is Nothing Then
s = Nothing
Else
stream = GetStream(2048)

' Save the document into the comstream, without clearing the
IsDirty flag
ips.Save(stream, resetIsDirty)

s = StreamToString(stream)
End If

Return s

End Function

Private Function StreamToString(ByVal strm As UCOMIStream) As String

Dim iptr As IntPtr

Dim s As String

GetHGlobalFromStream(strm, iptr)

' *** THIS IS ODD TOO ***
' If the source is the WebBrowser control then Ansi must be used ***
s = Marshal.PtrToStringAnsi(iptr)

' If the source is the cloned and modified document then Auto must
be used ***
s = Marshal.PtrToStringAuto(iptr)

' ***

Return s

End Function
</code>

Having cloned the document, I modify it by inserting some tags into the body
element. I then call GetDocumentSource() to get the HTML from the cloned
document.

However, the HTML that is returned is the original HTML, from the WebBrowser
control, and not from the cloned document. I know that the cloned document
contains the correct HTML because if I execute the following for the cloned
document

?doc.all.tags("html").Item(0).outerhtml

in the command window, I get what I expect in the body element.

Can anyone suggest why this is happening?

I have also highlighted an oddity in function StreamToString(), which I do
not understand. Why would the encoding of the HTML change between the
browser and a cloned document?

TIA

Charles
Jul 21 '05 #1
5 2740
Cor
Hi Charles,

Just a question, why do you not just use the outerHtml of the HTML tag?

Cor
Jul 21 '05 #2
Hi Cor

I don't use that because the outerHtml of the HTML tag doesn't include the
<!DOCTYPE tag at the top, so doesn't represent the complete document.

Charles
"Cor" <no*@non.com> wrote in message
news:Oc****************@TK2MSFTNGP11.phx.gbl...
Hi Charles,

Just a question, why do you not just use the outerHtml of the HTML tag?

Cor

Jul 21 '05 #3
Cor
Hi Charles,

That I copy just in, than is it even better.

:-)

Cor
Jul 21 '05 #4
Cor
Hi Charles,

Have a look for this

mshtml.HTMLCommentElementClass

(I think the rest you know yourself but if you want more information tell
me).

I had still something to do for you.

Cor
I don't use that because the outerHtml of the HTML tag doesn't include the
<!DOCTYPE tag at the top, so doesn't represent the complete document.

Jul 21 '05 #5
Hi Cor

I can see that I could possibly retrieve the <!DOCTYPE tag info, but that
relies on me knowing that it is there to be retrieved. I could look for it
anyway, but then what else might there be for me to retrieve that I don't
know about, or am not expecting?

The HTML outerHTML is not designed to return the entire contents of the
document, and I don't know of a definition that states that I will get the
entire document if I retrieve the HTML outerHTML and all preceding comments.
The IPersistStreamInit interface, on the other hand, is designed to maintain
the entire document, through the Load and Save methods, so I feel that I
should persevere with this for now.

I appreciate your comments, though, but I am still perplexed why my
technique is not working. It is as though the IPersistStreamInit interface
is always going to the WebBrowser control document, even when I direct it to
my cloned document. Alternatively, could there be something wrong with the
way in which I am manipulating the stream? Sadly, I just can't see it.

Regards

Charles
"Cor" <no*@non.com> wrote in message
news:%2********************@TK2MSFTNGP11.phx.gbl.. .
Hi Charles,

Have a look for this

mshtml.HTMLCommentElementClass

(I think the rest you know yourself but if you want more information tell
me).

I had still something to do for you.

Cor
I don't use that because the outerHtml of the HTML tag doesn't include the <!DOCTYPE tag at the top, so doesn't represent the complete document.


Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert May | last post by:
I have an application that uses the Ax web browser object. When I call the IHTMLElement.click() method on an input button (<input type="submit"> or <input type="button">), the click fires...
0
by: John Bowman | last post by:
Hi, I've got a simple C# (.NET 1.1) app that will run only on Win2K and WinXP (min IE ver. 5.01) that uses the ax webbrowser control to display a simple hmtl page and disables the right click...
0
by: Giuseppe | last post by:
Hi I've this problem Inside a winform application I've put a "webbrowser" control I've tried to catch the oncontextmenu event of the loaded HTML document with the following approach 1) In...
3
by: Charles Law | last post by:
I'm asking this here as I have already asked in the dedicated newsgroups but with no answer. I wonder if there is an answer that is more generally applicable to VB.NET. I want to be able to drag...
5
by: Charles Law | last post by:
Sorry for the cross post, but I'm not sure who is best placed to answer this one. This is the most bizarre behaviour of MSHTML and streams. I have a WebBrowser control that contains nothing...
0
by: Quantix | last post by:
I've been having a number of headaches trying to use the Webbrowser control and MSHTML to generate off screen images of webpages. I've got it working on a good number of sites, except the following...
4
by: adwooley2 | last post by:
Can't seem to find anything that works for me. I have a small bit of HTML that is stored in a string variable. I want to assign this string to an mshtml object (IHTMLDocument2 I believe), so that...
0
by: Andy Bates | last post by:
Hi - This issue seems to have been kicking around since the dawn of time and no one appears to have come up with an answer. In short the MHT/MSHTML provides a method of archiving an HTML page...
6
by: cellocoder | last post by:
I have tried it in Vb2005, vb2008, and CS2008. The program just hangs up with an hour glass. I downloaded a program with that reference added and it didn't work. I can add it to vb6 with no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.