Board index » cppbuilder » Help with Delphi Translation

Help with Delphi Translation


2005-09-25 02:42:39 AM
cppbuilder11
Hello,
Could someone help me with translating this deph code into Builder5 code.
procedure TForm1.acIESniffer1WBDownloadComplete(Sender: TObject;
const URL: String; const Browser: IWebBrowser2);
var
doc: IHTMLDocument2;
Collection: IHTMLElementCollection;
Element: IHTMLElement;
HTMLPage: String;
PlainText: String;
begin
try
doc := (Browser.Document as IHTMLDocument2);
Collection := doc.all;
Collection := Collection.Tags('BODY') as IHTMLElementCollection;
Element := Collection.Item(NULL, 0) as IHTMLElement;
HTMLPage := Element.OuterHTML; // read the HTML page
PlainText := Element.OuterText; // or just plain text
except
end;
end;
Thanks for any assistance in this.
Mike
 
 

Re:Help with Delphi Translation

"mike" < XXXX@XXXXX.COM >wrote in message
Quote
Could someone help me with translating this deph code into Builder5 code.
#include <ComObj.hpp>
#include <utilcls.h>
void __fastcall TForm1::acIESniffer1WBDownloadComplete(TObject *Sender,
const AnsiString URL, const IWebBrowser2 *Browser)
{
TComInterface<IHTMLDocument2>Doc;
TComInterface<IHTMLElementCollection>Collection;
TComInterface<IHTMLElement>Element;
TComInterface<IDispatch>Disp;
WideString HTMLPage;
WideString PlainText;
try
{
OleCheck(Browser->get_Document(&Disp));
OleCheck(Disp->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc));
Disp.Unbind();
OleCheck(Doc->get_all(&Collection));
OleCheck(Collection->tags(TVariant(WideString("BODY")), &Disp));
Collection.Unbind();
OleCheck(Disp->QueryInterface(IID_IHTMLElementCollection,
(LPVOID*)&Collection));
Disp.Unbind();
OleCheck(Collection->item(TVariant(WideString("")), TVariant(0),
&Disp));
OleCheck(Disp->QueryInterface(IID_IHTMLElement,
(LPVOID*)&Element));
Disp.Unbind();
OleCheck(Element->get_outerHTML(&HTMLPage));
OleCheck(Element->get_outerText(&PlainText));
}
catch(const Exception &)
{
}
}
Gambit
 

Re:Help with Delphi Translation

Remy,
Thank you very much for answering me I sure do appreciate it. I tried to use
the code you supplied and I have received the following errors. Is there any
chance you could help me with these to get this working.
Again thanks very mech.
Mike
[C++ Warning] Unit1.cpp(45): W8037 Non-const function __stdcall
IWebBrowser::Get_Document(_di_IDispatch &) called for const object
[C++ Warning] Unit1.cpp(45): W8030 Temporary used for parameter
'Get_Document_result' in call to '__stdcall
IWebBrowser::Get_Document(_di_IDispatch &)'
[C++ Error] Unit1.cpp(45): E2064 Cannot initialize '_di_IDispatch &' with
'IDispatch * *'
[C++ Error] Unit1.cpp(45): E2342 Type mismatch in parameter
'Get_Document_result' (wanted '_di_IDispatch &', got 'IDispatch * *')
[C++ Error] Unit1.cpp(50): E2316 'get_all' is not a member of
'IHTMLDocument2'
[C++ Error] Unit1.cpp(51): E2034 Cannot convert 'TVariant' to 'OleVariant'
[C++ Warning] Unit1.cpp(51): W8030 Temporary used for parameter
'tags_result' in call to '__stdcall IHTMLElementCollection::tags(const
OleVariant,_di_IDispatch &)'
[C++ Error] Unit1.cpp(51): E2064 Cannot initialize '_di_IDispatch &' with
'IDispatch * *'
[C++ Error] Unit1.cpp(51): E2342 Type mismatch in parameter 'tags_result'
(wanted '_di_IDispatch &', got 'IDispatch * *')
[C++ Error] Unit1.cpp(58): E2034 Cannot convert 'TVariant' to 'OleVariant'
[C++ Error] Unit1.cpp(58): E2034 Cannot convert 'TVariant' to 'OleVariant'
[C++ Warning] Unit1.cpp(58): W8030 Temporary used for parameter
'item_result' in call to '__stdcall IHTMLElementCollection::item(const
OleVariant,const OleVariant,_di_IDispatch &)'
[C++ Error] Unit1.cpp(58): E2064 Cannot initialize '_di_IDispatch &' with
'IDispatch * *'
[C++ Error] Unit1.cpp(58): E2342 Type mismatch in parameter 'item_result'
(wanted '_di_IDispatch &', got 'IDispatch * *')
[C++ Error] Unit1.cpp(63): E2316 'get_outerHTML' is not a member of
'IHTMLElement'
[C++ Error] Unit1.cpp(64): E2316 'get_outerText' is not a member of
'IHTMLElement'
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"mike" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Could someone help me with translating this deph code into Builder5 code.

#include <ComObj.hpp>
#include <utilcls.h>

void __fastcall TForm1::acIESniffer1WBDownloadComplete(TObject *Sender,
const AnsiString URL, const IWebBrowser2 *Browser)
{
TComInterface<IHTMLDocument2>Doc;
TComInterface<IHTMLElementCollection>Collection;
TComInterface<IHTMLElement>Element;
TComInterface<IDispatch>Disp;
WideString HTMLPage;
WideString PlainText;

try
{
OleCheck(Browser->get_Document(&Disp));
OleCheck(Disp->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc));
Disp.Unbind();

OleCheck(Doc->get_all(&Collection));
OleCheck(Collection->tags(TVariant(WideString("BODY")),
&Disp));
Collection.Unbind();
OleCheck(Disp->QueryInterface(IID_IHTMLElementCollection,
(LPVOID*)&Collection));
Disp.Unbind();

OleCheck(Collection->item(TVariant(WideString("")),
TVariant(0),
&Disp));
OleCheck(Disp->QueryInterface(IID_IHTMLElement,
(LPVOID*)&Element));
Disp.Unbind();

OleCheck(Element->get_outerHTML(&HTMLPage));
OleCheck(Element->get_outerText(&PlainText));
}
catch(const Exception &)
{
}
}


Gambit


 

{smallsort}

Re:Help with Delphi Translation

"mike" < XXXX@XXXXX.COM >wrote in message
Quote
[C++ Warning] Unit1.cpp(45): W8037 Non-const function __stdcall
IWebBrowser::Get_Document(_di_IDispatch &) called for const object
That is not an error. It is, however, caused by the function's Browser
parameter being declared as 'const'. Try removing the 'const'.
Also, you are not using the code I gave you. My code calls
IWebBrowser2::get_Document(IDispatch**), not
IWebBrowser2::Get_Document(_di_IDispatch&). Please copy-paste the code
as-is.
Quote
[C++ Error] Unit1.cpp(45): E2064 Cannot initialize '_di_IDispatch &' with
'IDispatch * *'

[C++ Error] Unit1.cpp(45): E2342 Type mismatch in parameter
'Get_Document_result' (wanted '_di_IDispatch &', got 'IDispatch * *')
Those are side-effects of you not using the code I gave you.
Quote
[C++ Error] Unit1.cpp(50): E2316 'get_all' is not a member of
'IHTMLDocument2'
Yes, it is. In fact, it is the very first method declared in
IHTMLDocument2:
--- mshtml.h ---
MIDL_INTERFACE("332c4425-26cb-11d0-b483-00c04fd90119")
IHTMLDocument2 : public IHTMLDocument
{
public:
virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_all(
/* [out][retval] */ IHTMLElementCollection __RPC_FAR *__RPC_FAR
*p) = 0;
//...
};
Quote
[C++ Error] Unit1.cpp(51): E2034 Cannot convert 'TVariant' to 'OleVariant'
<snip>
The rest of the errors lead me to think that you are not using Microsoft's
original HTML interfaces at all, but instead are accessing some VCL-specific
interfaces instead. Had you been using Microsoft's interfaces directly,
OleVariant would not be needed at all. You also would not be having
problems with any of the _di_... typedefs or "xxx is not a member of ..."
errors, either.
Gambit
 

Re:Help with Delphi Translation

Remy,
Again thanks very much for taking the time to help me out.
Well it seems we have made some headway as the errors I was getting before
have gone away. I traced the sample app that I have putting your code into
and all seems to be working well until I hit:
OleCheck(Collection->item(WideString(""), 0, Disp));
Element = Disp;
Disp.Release();
When this is executed it never returns therefore the app is hung at this
point. There is no exception reported.
Hope you can help me with this as it looks like it is very close to working.
I sure appreciate your knowledge in all of this.
Thanks
Mike
 

Re:Help with Delphi Translation

Remy,
Thanks for responding again and I am sorry I am just not getting this at
all. I am trying to use the IESniffer control from the AppControls from
UtilMind and I am having this major problem. I have purchased this from them
and have sent multiple messages to them for help and they just do not
respond, so I am stuck trying to get something to work that I have paid for
without any support.
If I include the mshtml.h I receive a whole bunch of duplicate declarations.
I do include a mshtml.hpp because if I don't then I don'tget anywhere using
the control.
Is there anyway that I could send you the control so you could see what I
mean in respect to trying to get this to work.
I am at a loss as to how to get it to work.
I really do appreciate the time you have spent trying to help me out.
Mike
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"mike" < XXXX@XXXXX.COM >wrote in message
news:4336bfa0$ XXXX@XXXXX.COM ...

>[C++ Warning] Unit1.cpp(45): W8037 Non-const function __stdcall
>IWebBrowser::Get_Document(_di_IDispatch &) called for const object

That is not an error. It is, however, caused by the function's Browser
parameter being declared as 'const'. Try removing the 'const'.

Also, you are not using the code I gave you. My code calls
IWebBrowser2::get_Document(IDispatch**), not
IWebBrowser2::Get_Document(_di_IDispatch&). Please copy-paste the code
as-is.

>[C++ Error] Unit1.cpp(45): E2064 Cannot initialize '_di_IDispatch &' with
>'IDispatch * *'
>
>[C++ Error] Unit1.cpp(45): E2342 Type mismatch in parameter
>'Get_Document_result' (wanted '_di_IDispatch &', got 'IDispatch * *')

Those are side-effects of you not using the code I gave you.

>[C++ Error] Unit1.cpp(50): E2316 'get_all' is not a member of
>'IHTMLDocument2'

Yes, it is. In fact, it is the very first method declared in
IHTMLDocument2:

--- mshtml.h ---

MIDL_INTERFACE("332c4425-26cb-11d0-b483-00c04fd90119")
IHTMLDocument2 : public IHTMLDocument
{
public:
virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_all(
/* [out][retval] */ IHTMLElementCollection __RPC_FAR *__RPC_FAR
*p) = 0;
//...
};


>[C++ Error] Unit1.cpp(51): E2034 Cannot convert 'TVariant' to
>'OleVariant'
<snip>

The rest of the errors lead me to think that you are not using Microsoft's
original HTML interfaces at all, but instead are accessing some
VCL-specific
interfaces instead. Had you been using Microsoft's interfaces directly,
OleVariant would not be needed at all. You also would not be having
problems with any of the _di_... typedefs or "xxx is not a member of ..."
errors, either.


Gambit


 

Re:Help with Delphi Translation

"mike" < XXXX@XXXXX.COM >wrote in message
Quote
If I include the mshtml.h I receive a whole bunch of duplicate
declarations.
Which, again, suggests that your code is not using the original Microsoft
interfaces to begin with, but instead is using VCL translations of them.
Quote
I do include a mshtml.hpp
Exactly. Any hpp file is a VCL header file.
Quote
if I don't then I don'tget anywhere using the control.
Then it sounds like you cannot use the Microsoft interfaces directly at all.
Which means the code will have to be adjusted accordingly. Since I know
nothiing about AppControls or IESniffer, I can only guess at the necessary
changes, as follows:
#include <mshtml.hpp>
void __fastcall TForm1::acIESniffer1WBDownloadComplete(TObject *Sender,
const AnsiString URL, const IWebBrowser2 *Browser)
{
DelphiInterface<IHTMLDocument2>Doc;
DelphiInterface<IHTMLElementCollection>Collection;
DelphiInterface<IHTMLElement>Element;
_di_IDispatch Disp;
WideString HTMLPage;
WideString PlainText;
try
{
OleCheck(Browser->Get_Document(Disp));
Doc = Disp;
Disp.Release();
OleCheck(Doc->Get_All(Collection));
OleCheck(Collection->tags(WideString("BODY"), Disp));
Collection = Disp;
Disp.Release();
OleCheck(Collection->item(WideString(""), 0, Disp));
Element = Disp;
Disp.Release();
OleCheck(Element->Get_OuterHTML(HTMLPage));
OleCheck(Element->Get_OuterText(PlainText));
}
catch(const Exception &)
{
}
}
Gambit
 

Re:Help with Delphi Translation

Remy,
Thanks for all the help and I was just wondering if you had a chance to look
at this problem in the solution you provided me.
Thanks
Mike
"mike" < XXXX@XXXXX.COM >wrote in message
Quote
Remy,

Again thanks very much for taking the time to help me out.

Well it seems we have made some headway as the errors I was getting before
have gone away. I traced the sample app that I have putting your code into
and all seems to be working well until I hit:

OleCheck(Collection->item(WideString(""), 0, Disp));
Element = Disp;
Disp.Release();

When this is executed it never returns therefore the app is hung at this
point. There is no exception reported.

Hope you can help me with this as it looks like it is very close to
working. I sure appreciate your knowledge in all of this.

Thanks

Mike

 

Re:Help with Delphi Translation

"mike" < XXXX@XXXXX.COM >wrote in message
Quote
OleCheck(Collection->item(WideString(""), 0, Disp));
<snip>
When this is executed it never returns therefore the app is hung
at this point. There is no exception reported.
The only thing I can think of is the blank item name that is being passed
in. That is very unusual to do, even for the original Pascal. Looking back
at it, I think the Pascal was trying to pass in an integer of 0, not a blank
string. In which case, the above line should be the following instead:
OleCheck(Collection->item(0, 0, Disp));
Now, with that said, since the collection traversal code is simply trying to
get an IHTMLElement for the <BODY>tag of a document, there is an easier
way - the IHTMLDocument2 interface has a body property that returns that
same IHTMLElement interface directly, ie:
#include <mshtml.hpp>
void __fastcall TForm1::acIESniffer1WBDownloadComplete(TObject *Sender,
const AnsiString URL, const IWebBrowser2 *Browser)
{
DelphiInterface<IHTMLDocument2>Doc;
DelphiInterface<IHTMLElement>Element;
_di_IDispatch Disp;
WideString HTMLPage;
WideString PlainText;
try
{
OleCheck(Browser->Get_Document(Disp));
Doc = Disp;
OleCheck(Doc->Get_Body(Element));
OleCheck(Element->Get_OuterHTML(HTMLPage));
OleCheck(Element->Get_OuterText(PlainText));
}
catch(const Exception &)
{
}
}
Gambit
 

Re:Help with Delphi Translation

Remy,
Well sir I am very impressed, you have given me the perfect answer and it
now works. I tried both of your suggestions and both work just fine.
I can't thank you enough for taking the time to help me in this, I
appreciate it very much.
Mike
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"mike" < XXXX@XXXXX.COM >wrote in message
news:43388d42$ XXXX@XXXXX.COM ...

>OleCheck(Collection->item(WideString(""), 0, Disp));
<snip>
>When this is executed it never returns therefore the app is hung
>at this point. There is no exception reported.

The only thing I can think of is the blank item name that is being passed
in. That is very unusual to do, even for the original Pascal. Looking
back
at it, I think the Pascal was trying to pass in an integer of 0, not a
blank
string. In which case, the above line should be the following instead:

OleCheck(Collection->item(0, 0, Disp));

Now, with that said, since the collection traversal code is simply trying
to
get an IHTMLElement for the <BODY>tag of a document, there is an easier
way - the IHTMLDocument2 interface has a body property that returns that
same IHTMLElement interface directly, ie:

#include <mshtml.hpp>

void __fastcall TForm1::acIESniffer1WBDownloadComplete(TObject *Sender,
const AnsiString URL, const IWebBrowser2 *Browser)
{
DelphiInterface<IHTMLDocument2>Doc;
DelphiInterface<IHTMLElement>Element;
_di_IDispatch Disp;
WideString HTMLPage;
WideString PlainText;

try
{
OleCheck(Browser->Get_Document(Disp));
Doc = Disp;
OleCheck(Doc->Get_Body(Element));
OleCheck(Element->Get_OuterHTML(HTMLPage));
OleCheck(Element->Get_OuterText(PlainText));
}
catch(const Exception &)
{
}
}


Gambit