🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.web
10 messages
10 total messages Started by caleb Thu, 09 Mar 2006 02:16
Getting TCppWebBrowser as Text From Stream
#750
Author: caleb
Date: Thu, 09 Mar 2006 02:16
60 lines
2338 bytes
Hi:

Can someone post code for getting the source of a TCppWebBrowser
doc?

(It is better if it is from stream and not from saving to file).

I have been using this code to get text from stream but it
does not always work (on complex pages it only gets part
of the html):

  AnsiString Source;

  IHTMLDocument2 *htm = NULL;     // #include <mshtml.h>


  if(wb->Document
     &&
      SUCCEEDED(wb->Document->QueryInterface(IID_IHTMLDocument2,
      (LPVOID*)&htm))
     )
    {
    IPersistStreamInit *spPsi = NULL;    // ocidl.h


    if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit,
(LPVOID*)&spPsi)) && spPsi)
       {
        IStream *spStream = NULL;            // objidl.h
            OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
            if(spStream)
            {
                __int64 nSize = 0;
                STATSTG ss;
                LARGE_INTEGER nMove;
                nMove.QuadPart = 0;
                OleCheck(spPsi->Save(spStream, true));
                OleCheck(spStream->Seek(nMove, STREAM_SEEK_SET,
(ULARGE_INTEGER *)&nSize));
                OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
                nSize = ss.cbSize.QuadPart;


                Source.SetLength(nSize);
                OleCheck(spStream->Read((void *)Source.data(), nSize,
(ULONG *)&nSize));
                OleCheck(spStream->Release());
            }


         spPsi->Release();
        }
    htm->Release();
    }
    *string = Source;

Thank you,

Caleb
Re: Getting TCppWebBrowser as Text From Stream
#751
Author: caleb
Date: Thu, 09 Mar 2006 02:41
32 lines
1760 bytes
This code below is giving the exact same partial document 
results.

For some reason I can save the browser contents to file,
but if streaming the browser contents gets only partial
file and the same partial file each time.


Thanks

    
        TComInterface<IHTMLDocument2> HTMLDocument;
        TComInterface<IPersistStreamInit> PersistStream;


        if( SUCCEEDED(bro->Document->QueryInterface(IID_IHTMLDocument2,
              (LPVOID*)&HTMLDocument)) )
        {
            if(
            SUCCEEDED(HTMLDocument->QueryInterface(IID_IPersistStreamInit,
            (LPVOID*)&PersistStream)) )
            {
                std::auto_ptr<TStringStream> Strm(new TStringStream(""));
                std::auto_ptr<TStreamAdapter> Adapter(new
TStreamAdapter(Strm.get(), soReference));
                PersistStream->Save(*Adapter, FALSE);
                *str = Strm->DataString;
            }
        }

        *str = "failed";
Re: Getting TCppWebBrowser as Text From Stream
#752
Author: caleb
Date: Thu, 09 Mar 2006 03:10
10 lines
1303 bytes
I would like to post a sample URL but I am not allowed to
I think.

If you would like to see the same partial doc results, I can
tell you that it IS NOT an auction site's pages. 

Anyway, WB will save to file perfectly but now WB suddenly
will not stream whole doc since site changed.  I am not sure 
what is solution.
Re: Getting TCppWebBrowser as Text From Stream
#753
Author: Remy Lebeau (TeamB)
Date: Thu, 09 Mar 2006 08:35
41 lines
2004 bytes
"caleb" <no@spam.com> wrote in message
news:440f9e16$1@newsgroups.borland.com...

> Can someone post code for getting the source of a
> TCppWebBrowser doc?

Such code has been posted many times before.  Please go to
http://www.deja.com and search through the newsgroup archives.

> I have been using this code to get text from stream but it
> does not always work (on complex pages it only gets part
> of the html):

When exactly are you calling that code?  Did you verify that the entire
document is actually available yet?

Try this code instead:

    #include <mshtml.h>
    #include <utilcls.h>
    #include <memory>

    AnsiString Source;

    TComInterface<IHTMLDocument2> htm = wb->Document;
    if( htm )
    {
        TComInterface<IPersistStreamInit> psi = htm;
        if( psi )
        {
            std::auto_ptr<TStringStream> Stream(new TStringStream(""));
            std::auto_ptr<TStreamAdapter> Adapter(new
TStreamAdapter(Stream.get(), soReference));
            OleCheck(psi->Save(*Adapter, TRUE));
            Source = Stream->DataString;
        }
    }


Gambit
Re: Getting TCppWebBrowser as Text From Stream
#755
Author: caleb
Date: Thu, 09 Mar 2006 13:28
30 lines
1887 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"caleb" <no@spam.com> wrote in message
>news:440f9e16$1@newsgroups.borland.com...
>
>> Can someone post code for getting the source of a
>> TCppWebBrowser doc?
>
>Such code has been posted many times before.  Please go to
>http://www.deja.com and search through the newsgroup archives.
>
>> I have been using this code to get text from stream but it
>> does not always work (on complex pages it only gets part
>> of the html):
>
>When exactly are you calling that code?  Did you verify that the entire
>document is actually available yet?

Yes it is available.  Additional information has been found.

As I stated existing deja code was saving to file as always, 
I can view the file in notepad and it is as expected.

But when I LoadFromFile the saved file into a TStringList and 
look at the TStringList->Text the page source is again 
truncated.

It is not a memory issue.  I am not familiar with AnsisString
to know why it truncates the page.
Re: Getting TCppWebBrowser as Text From Stream
#756
Author: caleb
Date: Thu, 09 Mar 2006 13:53
53 lines
2292 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"caleb" <no@spam.com> wrote in message
>news:440f9e16$1@newsgroups.borland.com...
>
>> Can someone post code for getting the source of a
>> TCppWebBrowser doc?
>
>Such code has been posted many times before.  Please go to
>http://www.deja.com and search through the newsgroup archives.
>
>> I have been using this code to get text from stream but it
>> does not always work (on complex pages it only gets part
>> of the html):
>
>When exactly are you calling that code?  Did you verify that the entire
>document is actually available yet?
>
>Try this code instead:
>
>    #include <mshtml.h>
>    #include <utilcls.h>
>    #include <memory>
>
>    AnsiString Source;
>
>    TComInterface<IHTMLDocument2> htm = wb->Document;
>    if( htm )
>    {
>        TComInterface<IPersistStreamInit> psi = htm;
>        if( psi )
>        {
>            std::auto_ptr<TStringStream> Stream(new TStringStream(""));
>            std::auto_ptr<TStreamAdapter> Adapter(new
>TStreamAdapter(Stream.get(), soReference));
>            OleCheck(psi->Save(*Adapter, TRUE));
>            Source = Stream->DataString;
>        }
>    }
>
>
>Gambit
>
>

Thanks. This code throws an exception on this line:

TComInterface<IHTMLDocument2> htm = wb->Document;

It is not the get source code to stream code chunk  though
 as I mentioned.  It appears to be something with AnsiString
truncating the document.
Re: Getting TCppWebBrowser as Text From Stream
#773
Author: caleb
Date: Thu, 09 Mar 2006 20:36
68 lines
2662 bytes

>A better solution would be to not read the browser data into a file at all,
>but into your AnsiString dirctly.  You already have the code to do that.
>
>
>Gambit
>
>

Thank you for the responses.  

This below code normally works for what you recommend.

For some reason it is giving me fits now.

I also tried getting the innerHTML of the body of the doc

I could ShowMessage the entire innerHTML but putting the BSTR 
bstr val into an AnsiString again gave a truncated string.




  AnsiString Source;

  IHTMLDocument2 *htm = NULL;     // #include <mshtml.h>


  if(wb->Document
     &&
      SUCCEEDED(wb->Document->QueryInterface(IID_IHTMLDocument2,
      (LPVOID*)&htm))
     )
    {
    IPersistStreamInit *spPsi = NULL;    // ocidl.h


    if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit,
(LPVOID*)&spPsi)) && spPsi)
       {
        IStream *spStream = NULL;            // objidl.h
            OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
            if(spStream)
            {
                __int64 nSize = 0;
                STATSTG ss;
                LARGE_INTEGER nMove;
                nMove.QuadPart = 0;
                OleCheck(spPsi->Save(spStream, true));
                OleCheck(spStream->Seek(nMove, STREAM_SEEK_SET,
(ULARGE_INTEGER *)&nSize));
                OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
                nSize = ss.cbSize.QuadPart;


                Source.SetLength(nSize);
                OleCheck(spStream->Read((void *)Source.data(), nSize,
(ULONG *)&nSize));
                OleCheck(spStream->Release());
            }


         spPsi->Release();
        }
    htm->Release();
    }
    
Re: Getting TCppWebBrowser as Text From Stream
#771
Author: Remy Lebeau (TeamB)
Date: Thu, 09 Mar 2006 21:21
43 lines
2589 bytes
"caleb" <nos@pam.com> wrote in message
news:44103b71$1@newsgroups.borland.com...

> As I stated existing deja code was saving to file as always,
> I can view the file in notepad and it is as expected.

> But when I LoadFromFile the saved file into a TStringList
> and look at the TStringList->Text the page source is again
> truncated.

The only way the data can truncated is if there are embedded null characters
in the data.  TStrings::LoadFromFile() calls TStrings::LoadFromStream(),
which calls TStrings::SetTextStr().  SetTextStr() treats the input data as a
null-terminated string, regardless of its actual length.  If there is an
null character in the data, the data will be truncated.

> It is not a memory issue.  I am not familiar with AnsisString
> to know why it truncates the page.

It is not an AnsiString problem.  It is a TStrings problem.
TStrings::LoadFromFile() creates an AnsiString containing the full data from
the file, but TStrings::SetTextStr() does not fully parse the AnsiString
properly.

The answer to your truncating problem is simple - don't use TStringList to
load the data.  Use a TFileStream or TMemoryStream to load the file data
directly into your AnsiString, ie:

    #include <memory>

    AnsiString Source;
    std::auto_ptr<TFileStream> Stream(new TFileStream("your file.htm",
fmOpenRead | fmShareDenyWrite));
    Source.SetLength(Stream->Size);
    if( Soure.Length() > 0 )
        Stream->Read(Source.data(), Soure.Length());

A better solution would be to not read the browser data into a file at all,
but into your AnsiString dirctly.  You already have the code to do that.


Gambit
Re: Getting TCppWebBrowser as Text From Stream
#772
Author: Remy Lebeau (TeamB)
Date: Thu, 09 Mar 2006 21:22
16 lines
1255 bytes
"caleb" <nos@pam.com> wrote in message
news:44104179$1@newsgroups.borland.com...

> This code throws an exception on this line:
>
> TComInterface<IHTMLDocument2> htm = wb->Document;

Please be more specific.

> It appears to be something with AnsiString truncating the document.

It is not.  See my other reply.


Gambit
Re: Getting TCppWebBrowser as Text From Stream
#774
Author: Remy Lebeau (TeamB)
Date: Fri, 10 Mar 2006 00:25
25 lines
1947 bytes
"caleb" <n@spam.com> wrote in message
news:44109fc5$1@newsgroups.borland.com...

> For some reason it is giving me fits now.

Please be more specific.  "Giving fits" is much too vague.

Why are you not using the code I already gave you earlier?

> I could ShowMessage the entire innerHTML but putting the
> BSTR bstr val into an AnsiString again gave a truncated string.

You are probably, again, performing an operation that treats the original
data as a null-terminated string, without taking the true length of the data
into account.  That means that any embedded null characters will truncate
the data when copied into the AnsiString.  That is the same problem that you
were having with the TStringList approach.

Please show your actual code.  Please show the original HTML as-is (or
provide the URL where it can be reached).  Where is the truncation actually
occuring?


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads