Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Webbrowser control/ActiveX control using C Win32 API

5 views
Skip to first unread message

MaiMee

unread,
Aug 27, 2002, 5:22:51 AM8/27/02
to
Hello!
Using C Win32 API, I'd like to insert a webbrowser control (or any other
activeX control)
into a dialog box.
I realize that it is done more easily (productively?) with ATL and MFC, but
I'd like to
see working samples/snippets in C Win32 API (as an exercise and as
comparative to the ATL/MFC code).

Thanks in advance!

Maimee


Volker Hilsheimer

unread,
Aug 27, 2002, 9:05:56 AM8/27/02
to
"MaiMee" <maime...@hotmail.com> wrote in message
news:eBKjItaTCHA.2780@tkmsftngp09...

Well, use CoCreateInstance to get an interface to the control, and use the
various interfaces probably implemented by the control (IOleObject,
IOleControl etc.) to set a client site, activate the control etc. Your
client site should implement - among others - IOleWindow (ie. via
IOleInPlaceSite), and the control will take care of parenting itself
correctly.

What helped me a lot figuring out the steps was using CAxHostWindow, and
stepping through the code with the debugger to see what happens, and in
which order.

--
Volker

Alexander Nickolov

unread,
Aug 27, 2002, 1:42:35 PM8/27/02
to
May I suggest using C++ instead of C? It's a big pain writing
COM in C and there are scarcely any examples...

Also, I recommend you to read "Inside OLE" by Kraig Brockschmidt.
All of it, even though OLE Controls (the predecessors to ActiveX
Controls) are only in the last chapter... (specifically you need
chapters 17-24)

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"MaiMee" <maime...@hotmail.com> wrote in message news:eBKjItaTCHA.2780@tkmsftngp09...

MaiMee

unread,
Aug 28, 2002, 5:52:03 AM8/28/02
to
Thank you very much for the tip, Volker!
May you email me the working skeleton source of what you did, Volker?
Or post it on a website, that we may all download?
I hope it's not imposing too much. I would greatly appreciate it.

Thanks again.
Regards,
Maimee
P.S. I think you could make a tutorial webpage on what you did. I believe
there are a lot of people who would be interested: out of curiosity
and/or to implement in their
own programs.

MaiMee

unread,
Aug 28, 2002, 5:52:14 AM8/28/02
to
Thanks for tip, Alexander!
I know and understand what you mean by suggesting using C++ instead of C.
It is really difficult to find examples (in C, or for that matter , C++
non-ATL/non-MFC), I haven't found one yet.
Would you know where I can get C++ (non ATL/non - MFC) samples of writing
it?

"Inside OLE" by Kraig Brockschmidt, found in MSDN circa 1998, was a good
read, though I couldn't find complete
working examples of activex/ole control in a dialog box(and other window),
either in C or C++. Maybe the paper book had a CDROM where working examples
are stored? (Browsed through amazon.com, it said there is a cdrom for it,
but out of print).

Thanks again
Regards,
Maimee


"Alexander Nickolov" <agnic...@mvps.org> wrote in message
news:#arJnCfTCHA.3620@tkmsftngp08...

Volker Hilsheimer

unread,
Aug 28, 2002, 6:54:15 AM8/28/02
to
"MaiMee" <maime...@hotmail.com> wrote in message
news:#Eg2gsnTCHA.384@tkmsftngp12...

> Thank you very much for the tip, Volker!
> May you email me the working skeleton source of what you did, Volker?
> Or post it on a website, that we may all download?
> I hope it's not imposing too much. I would greatly appreciate it.
>
> Thanks again.
> Regards,
> Maimee
> P.S. I think you could make a tutorial webpage on what you did. I believe
> there are a lot of people who would be interested: out of
curiosity
> and/or to implement in their
> own programs.
>

I've been working on a generic ActiveX control wrapper for the Qt toolkit,
and I'm afraid I can't make all of it available here, but the releveant
initialization part is below. It uses a very limited number of Qt specific
classes, but it should be translateable to generic HWND's etc. As Alexander
pointed out it's of course written in C++ (I was assuming your "C" was
referring to the Win32 API).
The only non-Qt header I have to include is olectl.h.

hr = CoCreateInstance( QUuid(widget->control()), 0, CLSCTX_SERVER,
IID_IUnknown, (void**)ppUnk );
bool bInited = hr == S_FALSE;

if ( !SUCCEEDED(hr) || !*ppUnk )
return;

hr = S_OK;

m_spOleObject = 0;
widget->queryInterface( IID_IOleObject, (void**)&m_spOleObject );
if (m_spOleObject) {
DWORD dwMiscStatus = 0;
m_spOleObject->GetMiscStatus( DVASPECT_CONTENT, &dwMiscStatus );
if( dwMiscStatus & OLEMISC_SETCLIENTSITEFIRST )
m_spOleObject->SetClientSite( this );

if ( !bInited ) {
IPersistStreamInit *spPSI = 0;
m_spOleObject->QueryInterface( IID_IPersistStreamInit,
(void**)&spPSI );
if ( spPSI ) {
spPSI->InitNew();
spPSI->Release();
}
}

if( !(dwMiscStatus & OLEMISC_SETCLIENTSITEFIRST) )
m_spOleObject->SetClientSite( this );

IViewObject *spViewObject = 0;
m_spOleObject->QueryInterface(IID_IViewObject, (void**) &spViewObject);

m_spOleObject->Advise( this, &m_dwOleObject );
IAdviseSink *spAdviseSink = 0;
QueryInterface( IID_IAdviseSink, (void**)&spAdviseSink );
if ( spAdviseSink && spViewObject ) {
if ( spViewObject )
spViewObject->SetAdvise( DVASPECT_CONTENT, 0, spAdviseSink );
spAdviseSink->Release();
}
if ( spViewObject )
spViewObject->Release();

m_spOleObject->SetHostNames( OLESTR("AXWIN"), 0 );
QPaintDeviceMetrics pdm( widget );

if ( !(dwMiscStatus & OLEMISC_INVISIBLEATRUNTIME) ) {
SIZEL hmSize;
hmSize.cx = MAP_PIX_TO_LOGHIM( 250, pdm.logicalDpiX() );
hmSize.cy = MAP_PIX_TO_LOGHIM( 250, pdm.logicalDpiY() );

m_spOleObject->SetExtent( DVASPECT_CONTENT, &hmSize );
m_spOleObject->GetExtent( DVASPECT_CONTENT, &hmSize );

sizehint.setWidth( MAP_LOGHIM_TO_PIX( hmSize.cx, pdm.logicalDpiX() ) );
sizehint.setHeight( MAP_LOGHIM_TO_PIX( hmSize.cy,
pdm.logicalDpiY() ) );
} else {
sizehint = QSize( 0, 0 );
}
if ( !(dwMiscStatus & OLEMISC_NOUIACTIVATE ) ) {
host->setFocusPolicy( QWidget::StrongFocus );
} else {
host->setFocusPolicy( QWidget::NoFocus );
}

RECT rcPos = { host->x(), host->y(),
host->x()+sizehint.width(), host->y()+sizehint.height() };

hr = m_spOleObject->DoVerb( OLEIVERB_INPLACEACTIVATE, 0,
(IOleClientSite*)this, 0, host->winId(), &rcPos );

m_spOleObject->QueryInterface( IID_IOleControl, (void**)&m_spOleControl );
if ( m_spOleControl ) {
m_spOleControl->OnAmbientPropertyChange( DISPID_AMBIENT_BACKCOLOR );
m_spOleControl->OnAmbientPropertyChange( DISPID_AMBIENT_FORECOLOR );
m_spOleControl->OnAmbientPropertyChange( DISPID_AMBIENT_FONT );

controlInfo.cb = sizeof(controlInfo);
m_spOleControl->GetControlInfo( &controlInfo );
}

BSTR userType;
m_spOleObject->GetUserType( USERCLASSTYPE_SHORT, &userType );
widget->setCaption( BSTRToQString( userType ) );
CoTaskMemFree( userType );
}
IObjectWithSite *spSite;
widget->queryInterface(IID_IObjectWithSite, (void**)&spSite);
if ( spSite ) {
spSite->SetSite( (IUnknown*)(IDispatch*)this );
spSite->Release();
}


The implementation of the client site interfaces is basically by the book,
and the whole control container implementation is about 1600 LOC, with a
good deal of it taking care of the Qt specifics.

--
Volker

Alexander Nickolov

unread,
Aug 28, 2002, 1:41:20 PM8/28/02
to
The CD-ROM content is also in that MSDN edition. Unfortunately,
the book only scratches on OLE Controls - you won't find anything
more than what is already in the book itself. Even worse, there has
not been a single thorough book on ActiveX Controls printed ever.
Chapter 3 of "ActiveX Controls Inside Out" by Adam Denning is my
best recommendation (but the book as a whole is complete junk)
after "Inside OLE"... Your best reference after all may be the ATL
control hosting sources (I wouldn't recommend reading MFC sources
even to my worst enemy!).

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"MaiMee" <maime...@hotmail.com> wrote in message news:O4wRnsnTCHA.384@tkmsftngp12...

MaiMee

unread,
Aug 29, 2002, 5:06:01 AM8/29/02
to
Thanks for input, Volker!
I appreciate it!

What references (books,documents, etc.) did you used to write your
implementation?
With those references, did you employ little trial and error to implement a
"skeleton?"
Or it took much trial and error that during the coding you opted to go the
way of ATL/MFC?

Regards,
Maimee


"Volker Hilsheimer" <vo...@trolltech.com> wrote in message
news:#kvqLEoTCHA.4328@tkmsftngp09...

MaiMee

unread,
Aug 29, 2002, 9:30:15 AM8/29/02
to
Thanks, Alexander!
I'll take your suggestions and study the ATL sources with the Inside OLE
MSDN edition.
Any other info/code that you may come across in the future will be greatly
appreciated,too!

Regards,
Maimee

"Alexander Nickolov" <agnic...@mvps.org> wrote in message

news:uqKgkmrTCHA.3740@tkmsftngp08...

Volker Hilsheimer

unread,
Aug 29, 2002, 9:50:26 AM8/29/02
to
"MaiMee" <maime...@hotmail.com> wrote in message
news:uuOc3szTCHA.1632@tkmsftngp11...

> Thanks for input, Volker!
> I appreciate it!
>
> What references (books,documents, etc.) did you used to write your
> implementation?
> With those references, did you employ little trial and error to implement
a
> "skeleton?"
> Or it took much trial and error that during the coding you opted to go the
> way of ATL/MFC?
>
> Regards,
> Maimee
>

I've mainly relied on MSDN for the implementation details, and on "OLE
Controls Inside Out" by Adam Denning, which includes a helpful chapter about
the Microsoft specifications.

--
Volker


MaiMee

unread,
Aug 29, 2002, 9:11:29 PM8/29/02
to
Thanks, Volker!

Regards,
Maimee


"Volker Hilsheimer" <vo...@trolltech.com> wrote in message

news:#m2RTL2TCHA.2928@tkmsftngp13...

0 new messages