Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C++
Article

Use an ActiveX control in your Win32 Project without MFC with CreateWindowEx or in a dialog box

Rate me:
Please Sign up or sign in to vote.
4.41/5 (28 votes)
15 Apr 20072 min read 225.8K   4.8K   54   97
An article on how to use an ActiveX control in your Win32 Project without MFC with CreateWindowEx or in a dialog box
Screenshot - d.jpg

Introduction

You would really like to use the multitude of ActiveX controls available in Windows and on the Internet, however you did have to use MFC or OWL or a non native platform. The solution is here! A HWND that acts as an ActiveX container!

Using the code

In short, AX is a HWND that acts as an ActiveX container. You register its class with AXRegister():

C++
int __stdcall WinMain(HINSTANCE h,HINSTANCE,LPSTR,int)
{
    OleInitialize(0);
    if (!AXRegister())
        return 0;
    ...
}

Using the control is easy. You either use CreateWindowEx(), or specify the AX control within the RC editor:

C++
DIALOG_1 DIALOGEX 0, 0, 500, 400
...
{
    CONTROL "{8856F961-340A-11D0-A96B-00C04FD705A2}", 801, "AX", 
                WS_CHILD | WS_VISIBLE, 0, 0, 500, 400
}

As the Window Title, you use the CLSID of the ActiveX object you wish to create. Here I used the CLSID of Internet Explorer. You can find all the CLSIDs you want by using MS's OLEView.

When you call CreateWindowEx() or DialogBox() to create the window, the ActiveX object will be created, but it won't as yet be activated in place. Use AX_INPLACE (wParam = 1 to Activate , wParam = 2 to deactivate):

C++
case WM_INITDIALOG:
{
    HWND hX = GetDlgItem(hh,801);
    SendMessage(hX,AX_INPLACE,1,0)
...

And how do you access the interfaces that this ActiveX object supports? Use AX_QUERYINTERFACE, with WPARAM as a pointer to the reference ID, and LPARAM as a double pointer to the output interface. I try this with IWebBrowser2:

C++
// Navigate
IWebBrowser2* wb = 0;
SendMessage(hX,AX_QUERYINTERFACE,(WPARAM)&IID_IWebBrowser2,(LPARAM)&wb);
if (wb)
{
    wb->Navigate(L"http://www.codeproject.com",0,0,0,0);
    wb->Release();
}

Points of Interest

AX.CPP and AX.H contain more code, not yet implemented (or yet buggy!) . My plan is to allow Advise Sink connections, OLE menus etc. in the future. AX implements as few as possible of the IOleClientSite methods, for simplicity. When you further explore OLE and ActiveX, you put your own implementations there!

With the above IWebBrowser2, for example, you might need to get notified when the user clicks on a URL. This can be done using IDispatch, but it isn't simple and I won't demonstrate it here, because it depends on the ActiveX control you want to host.

Good luck!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Greece Greece
I'm working in C++, PHP , Java, Windows, iOS, Android and Web (HTML/Javascript/CSS).

I 've a PhD in Digital Signal Processing and Artificial Intelligence and I specialize in Pro Audio and AI applications.

My home page: https://www.turbo-play.com

Comments and Discussions

 
GeneralRe: Context menu Pin
basiek5-Apr-11 3:00
basiek5-Apr-11 3:00 
GeneralKeyboard shortucts Pin
Martin Kucera23-Jan-11 22:39
Martin Kucera23-Jan-11 22:39 
GeneralRe: Keyboard shortucts Pin
NIKHIL788 Agrawal2-Dec-12 1:59
NIKHIL788 Agrawal2-Dec-12 1:59 
GeneralAdobe Acrobat Browser Control Pin
drunkbear16-Jul-10 19:16
drunkbear16-Jul-10 19:16 
GeneralRe: Adobe Acrobat Browser Control Pin
Daktor15-Sep-10 8:09
Daktor15-Sep-10 8:09 
QuestionSizing error when I display google homepage. Pin
salvatore.sergi4-Mar-10 21:32
salvatore.sergi4-Mar-10 21:32 
AnswerRe: Sizing error when I display google homepage. Pin
Martin Kucera23-Jan-11 22:36
Martin Kucera23-Jan-11 22:36 
QuestionUsing flash activeX control using this code Pin
snehayp14-Jan-10 21:35
snehayp14-Jan-10 21:35 
Hi,
The above code is very helpul for using activeX control without MFC. I was looking for this solution since long. Thanks for posting this solution.
I have used the above code for flash activeX control. As mentioned in one of the earlier post,I have used the flash activeX control id in createWindow api.I have succesfully abtained the reference of IShockwaveFlash interface and used Play(), put_Movie() api to launch a .swf file.
But i am able to load only .swf file present in d:\ drive. I want to play a flv file from URL. I am not even successful in playing the flv file present in my d:\ drive.
Please cud any one help and suggest how can we play flv file from url using IShockwaveFlash interface.(any api present in that activeX control) Frown | :(

Thanks in advance

Snehal
AnswerRe: Using flash activeX control using this code Pin
Michael Chourdakis14-Jan-10 21:43
mvaMichael Chourdakis14-Jan-10 21:43 
GeneralRe: Using flash activeX control using this code Pin
snehayp14-Jan-10 22:52
snehayp14-Jan-10 22:52 
GeneralRe: Using flash activeX control using this code Pin
Michael Chourdakis15-Jan-10 1:23
mvaMichael Chourdakis15-Jan-10 1:23 
GeneralRe: Using flash activeX control using this code Pin
snehayp18-Jan-10 23:05
snehayp18-Jan-10 23:05 
GeneralRe: Using flash activeX control using this code Pin
Michael Chourdakis19-Jan-10 2:41
mvaMichael Chourdakis19-Jan-10 2:41 
GeneralRe: Using flash activeX control using this code Pin
snehayp19-Jan-10 18:46
snehayp19-Jan-10 18:46 
GeneralRe: Using flash activeX control using this code Pin
Michael Chourdakis20-Jan-10 1:38
mvaMichael Chourdakis20-Jan-10 1:38 
Questionhow to make it in WinForm Pin
tanya_kuraeva5-Oct-09 6:03
tanya_kuraeva5-Oct-09 6:03 
AnswerRe: how to make it in WinForm Pin
Michael Chourdakis14-Jan-10 22:07
mvaMichael Chourdakis14-Jan-10 22:07 
GeneralMy vote of 2 Pin
nityasantosh23-Sep-09 1:31
nityasantosh23-Sep-09 1:31 
GeneralDevCpp compilation and usage Pin
syntotic5-Sep-09 14:06
syntotic5-Sep-09 14:06 
GeneralRe: DevCpp compilation and usage Pin
Michael Chourdakis6-Sep-09 3:14
mvaMichael Chourdakis6-Sep-09 3:14 
GeneralRe: DevCpp compilation and usage Pin
syntotic6-Sep-09 14:45
syntotic6-Sep-09 14:45 
GeneralRe: DevCpp compilation and usage Pin
Michael Chourdakis19-Jan-10 3:20
mvaMichael Chourdakis19-Jan-10 3:20 
GeneralRe: DevCpp compilation and usage Pin
Whay Kay Pee20-Oct-11 17:06
Whay Kay Pee20-Oct-11 17:06 
GeneralRe: DevCpp compilation and usage Pin
Michael Chourdakis21-Oct-11 1:51
mvaMichael Chourdakis21-Oct-11 1:51 
AnswerCompilation problem in VS2008 Pin
InfiniteMort1-Sep-09 3:17
InfiniteMort1-Sep-09 3:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.