🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #991

Re: Connecting IE to a running instance.

#991
From: gizmo
Date: Mon, 10 Oct 2005 06:39
220 lines
7355 bytes
"Enrique" <enridp@yahoo.com.ar> wrote:

[skip]

>And searching in google i found that GetActiveObject doesn't work with IE.
>
>MSDN has a sample code but I can't understand it (and "translate" it to
>BCB):
>----------------------------------------------------------------------------
>-----------------------------------------------------------
>It say:
>Add references to Shdocvw.dll and Mshtml.dll to the project:
>#import <mshtml.dll> // Internet Explorer 4.0x
>#import <mshtml.tlb> // Internet Explorer 5
>#import <shdocvw.dll>
>
>I do:
>Project - Import Type Library - Microsoft HTML Object Library (version
>4.0) - Create Unit. (then i include the unit to my form)
>Project - Import Type Library - Microsoft Internet Controls (Version 1.1) -
>Create Unit. (it gives me an error: "A class named 'TCppWebBrowser' is
>already installed")

The #import directive is included for Microsoft compatibility only. Its use is generally not recommended. You should use the Import Type Library command or TLIBIMP.EXE when importing type libraries.
Do it:
$(BCB)\BIN\TLIBIMP.EXE -c:\windows\system32\shdocvw.dll 

>----------------------------------------------------------------------------
>-----------------------------------------------------------
>MSDN:
>Create an instance of a ShellWindows object in your view's constructor:
>m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));
>I add this in TForm constructor.
>Errors:
>E2316 : ShellWindows is not a member of 'SHDocVw'
>I don't know how to solve this.

in this case:
SHDocVw_TLB::ShellWindows

>----------------------------------------------------------------------------
>-----------------------------------------------------------
>finally, MSDN (I add comments in lines that I don't understand (almost all
>:-))
>Use the ShellWindows object in your view's OnInitialUpdate function:
>void CConnectIEView::OnInitialUpdate()    //What's this method? can i create
>my own in TForm?

throw this...

>{
>   CFormView::OnInitialUpdate(); //and this??

and this...

>
>   ASSERT(m_spSHWinds != NULL); //BCB doesn't know ASSERT

and this...

>
>   CString strCount;   //BCB doesn't know CString, can i use AnsiString? (or
>string...)

Yes, AnsiString

>   long nCount = m_spSHWinds->GetCount();
>
>   strCount.Format("%i", nCount);
>   m_strWinCount = strCount;
>
>   UpdateData(FALSE);  //BCB doesn't know UpdateData

all is MFC...throw

>
>   IDispatchPtr spDisp;
>   for (long i = 0; i < nCount; i++)
>   {
>      _variant_t va(i, VT_I4);  //_variant_t? i must use Variant? or
>TVariant?

yes...

>      spDisp = m_spSHWinds->Item(va);
>
>      SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);    //linker error!

need add to project SHDocVw_TLB.cpp.

>      if (spBrowser != NULL)
>      {
>         m_ctlListLoc.AddString(spBrowser->GetLocationName());    //what is
>m_ctlListLoc??

use TListBox...

>
>         MSHTML::IHTMLDocument2Ptr spDoc(spBrowser->GetDocument());
>         if (spDoc != NULL)
>         {
>             m_ctlListTitle.AddString(spDoc->Gettitle());
>//and  m_ctlListTitle?
>         }
>      }
>   }
>}

sorry my english is bad...
Your can do it:


#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
// for CComPtr
#include <atlbase.h>
// ShellWindows
#include <exdisp.h>
// MSHTML
#include <mshtml.h>

//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TPanel *Panel1;
    TButton *Button1;
    TListBox *ListBox1;
    TMemo *Memo1;
    TSplitter *Splitter1;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
private:	// User declarations
    CComPtr<IShellWindows> m_spSHWinds;
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    // initialize COM
    CoInitialize(NULL);

    if (FAILED(m_spSHWinds.CoCreateInstance( __uuidof( ShellWindows))))
        ShowMessage("Couldn't create object ShellWindows");

    // get count windows
    LONG nCount;
    m_spSHWinds->get_Count( &nCount);

    for (int i=0; i<nCount; i++)
    {
        // get next window
        CComPtr<IDispatch> pDisp;
        m_spSHWinds->Item( CComVariant(i), &pDisp);
        CComQIPtr<IWebBrowser2> pIE( pDisp);
        if (pIE == NULL)
            ShowMessage("Unable to query Internet Explorer object");

        // get URL
        USES_CONVERSION;
        CComBSTR bsUrl;
        pIE->get_LocationURL( &bsUrl);
        LPTSTR lpstrUrl = W2T(bsUrl);

        // trying htmldoc
        CComPtr<IDispatch> pDispDoc;
        pIE->get_Document( &pDispDoc);
        CComQIPtr<IHTMLDocument2> pHtmlDoc( pDispDoc);

        // if Ok, - Internet Explorer
        if (pHtmlDoc)
        {
            //pHtmlDoc->put_bgColor( CComVariant( L"blue"));
            ListBox1->Items->Add(lpstrUrl);
            // connect events
            //...
        }
        // else - Explorer
        else
        {
            //ListBox1->Items->Add("  Window Explorer: ");
            // close window
            //pIE->Quit();
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    CoUninitialize();
}
//---------------------------------------------------------------------------
Technical Details
Message-ID: <434a0cb3$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <43471a45@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,2fc8286bda6a7885
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "gizmo" <_gizmo@mail.ru>
Sender: "gizmo" <_gizmo@mail.ru>
Reply-To: "gizmo" <_gizmo@mail.ru>
Subject: Re: Connecting IE to a running instance.
Newsgroups: borland.public.cppbuilder.internet.web
X-User-Info: 195.201.33.206     
X-Forwarded-For: 10.3.10.162, 10.3.2.96
References:  <43471a45@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <434a0cb3$1@newsgroups.borland.com>
Date: 9 Oct 2005 23:39:47 -0700
X-Trace: newsgroups.borland.com 1128926387 207.105.83.66 (9 Oct 2005 23:39:47 -0700)
Lines: 222
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail