C++Builder TCppWebBrowser and dynamically linked OnDocumentComplete event

I am creating a TCppWebBrowser component dynamically and linking the OnDocumentComplete event. I have tested this fires correctly when the component exists on the form. It does not fire now when I do a CppWebBrowser1->Navigate for the html page.

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  CppWebBrowser1 = new TCppWebBrowser(this);
  CppWebBrowser1->OnDocumentComplete = DocumentComplete;
}

void __fastcall TMainForm::DocumentComplete(TObject *Sender, LPDISPATCH pDisp, TVariant *URL)
{
 // print the document
 CppWebBrowser1->ExecWB   (Shdocvw_tlb::OLECMDID_PRINT,                          Shdocvw_tlb::OLECMDEXECOPT_DONTPROMPTUSER),&vIn, &vOut);
}

(C++Builder 5.01)

Thanks and cheers, Tom.


boardtcAsked:
Who is Participating?
 
MoondancerConnect With a Mentor Commented:
Thanks, Werner, 50 points refunded, item closed.  PAQd at zero points.
Moondancer - EE Moderator
0
 
AlexVirochovskyCommented:
How you navigate? Must be something as
TCppWebBrowser1->Navigate2( WideString(TheURLAnsiString).c_bstr() );



0
 
boardtcAuthor Commented:
Alex, thanks for the mail, as I said I use
CppWebBrowser1->Navigate
Navigate2 will not work with a WideString as you describe, it looks for a TVariant.
Any more ideas?
Thanks, Tom.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
AlexVirochovskyCommented:
>>TCppWebBrowser1->Navigate2( WideString(TheURLAnsiString).c_bstr() );
Of course, it works(from some working apps).
For Navigate, it must be
CppWebBrowser1->Navigate((TVariant)Edit1->Text); //for example

Next article is NOT mine (Edward O. Warren)

http://delphi.mers.com/cgi-bin/mer.exe/EXECSEARCH?pageno=1&searchtext=DocumentComplete&grouptext=cppbuilder&linktype=viewmsg&msgno=8771&boardno=1680
.....
Well I solve it but not 'elegant'.

I hacked some code from Microsoft that used the IWebBrowser interface.
Turns out I had to change this to the IWebBrowser2 interface to work with
the TCppWebBrowser.

This codes 'gets the job done' but I would appreciate any comments on a
better way to do this. This Lacks something.

-----------code----------
// BUTTON 1:
void __fastcall TSketchPadForm1::Button1Click(TObject *Sender)
{

CppWebBrowser1->Navigate(WideString("about:blank"));
// this works it navigates to a blank page

}

// CPPWEBBROWSER 1 DOCUMENT COMPLETE EVENT -

void __fastcall TSketchPadForm1::CppWebBrowser1DocumentComplete(
TObject *Sender, LPDISPATCH pDisp, TVariant *URL)
{

// get a pointer to a IWebBrowser2 interface

IWebBrowser2* m_pBrowser
m_pBrowser = CppWebBrowser1->ControlInterface;

HRESULT hr;
IUnknown* pUnkBrowser = NULL;
IUnknown* pUnkDisp = NULL;
IStream* pStream = NULL;
HGLOBAL hHTMLText;
static char szHTMLText[]="<html><h1>HELLO</h1>";

// MODIFICATIONS *******

AnsiString strHTML;

int szHTML;
strHTML = Memo1->Text;

szHTML= strHTML.Length();

// Is this the DocumentComplete event for the top frame window?
// Check COM identity: compare IUnknown interface pointers.
hr = m_pBrowser->QueryInterface( IID_IUnknown, (void**)&pUnkBrowser );
if ( SUCCEEDED(hr) )
{
hr = pDisp->QueryInterface( IID_IUnknown, (void**)&pUnkDisp );
if ( SUCCEEDED(hr) )
{
if ( pUnkBrowser == pUnkDisp )
{ // This is the DocumentComplete event for the top
frame - page is loaded!
// Create a stream containing the HTML.
// Alternatively, this stream may have been passed to
us.
// hHTMLText = GlobalAlloc( GPTR,
lstrlen(szHTMLText)+1 );

// MODIFICATION
// *****************************************
hHTMLText = GlobalAlloc( GPTR, szHTML+1 );

if ( hHTMLText )
{
// lstrcpy( (char*)hHTMLText, szHTMLText );

// MODIFICATION ***********************

lstrcpy( (char*)hHTMLText, strHTML.c_str() );

hr = CreateStreamOnHGlobal( hHTMLText, TRUE, &pStream );
if ( SUCCEEDED(hr) )
{
// Call the helper function to load the WebOC
from the stream.
LoadWebOCFromStream( m_pBrowser, pStream );
pStream->Release();
}
GlobalFree( hHTMLText );
}
}
pUnkDisp->Release();
}
pUnkBrowser->Release();
}


}
// -HELPER FUNCTION LOAD WEB OC FROM STREAM

HRESULT LoadWebOCFromStream(IWebBrowser2* pWebBrowser, IStream* pStream)
{
HRESULT hr;
IDispatch* pHtmlDoc = NULL;
IPersistStreamInit* pPersistStreamInit = NULL;

// Retrieve the document object.
hr = pWebBrowser->get_Document( &pHtmlDoc );
if ( SUCCEEDED(hr) )
{
// Query for IPersistStreamInit.
hr = pHtmlDoc->QueryInterface( IID_IPersistStreamInit,
(void**)&pPersistStreamInit );
if ( SUCCEEDED(hr) )
{
// Initialize the document.
hr = pPersistStreamInit->InitNew();
if ( SUCCEEDED(hr) )
{
// Load the contents of the stream.
hr = pPersistStreamInit->Load( pStream );
}
pPersistStreamInit->Release();
}
}
}

-----------end code-----

Edward O. Warren <eowarren@alltel.net>  
0
 
boardtcAuthor Commented:
Alex,

Thanks for the mail. I read that post prior to posting my question. I don't want to do that hack all I want to do is to get OnDownlaodComplete to fire when the TCppWebBrowser is created dynamically. Cane you help me?

Thanks, Tom.
0
 
boardtcAuthor Commented:
No one could help :-(
0
 
DanRollinsCommented:
Hi tomcorcoran,
You've requested to delete this question, but its status has remained as 'Pending Delete' because one or more comments have been added.  Normally, the only way to fully delete such a Question is to post a message to Community Support and ask for assistance.

EE is making a one-time database sweep to purge the Pending Delete Questions automatically.  During this sweep:

    tomcorcoran -- To allow the deletion to proceed:  Do nothing.
    EXPERTS -- Please DON'T POST a comment except to contest this deletion.

In the future, please refer to http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#8 for instruction on deleting questions.

DanRollins -- EE database cleanup volunteer
0
 
griesshCommented:
PAQ & refund

======
Werner
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.