Link to home
Start Free TrialLog in
Avatar of cskiong
cskiongFlag for Malaysia

asked on

Wrapper for ActiveX Object

Hi experts,
I know this may sound easy, but I really need some advices on this urgently.
I have a third party Active X Object (.ocx) written by using Microsoft C++ 2005 (MFC).  The object contains a dialog based custom control and some control functions.
I wonder if it is possible for me to embed the active X object to my MFC application to reuse its control functions but not the object's GUI, because I'll create my own GUI to use the object's control function.
A step-by-step guide is very much appreciate as I'm really in an urgent.

Avatar of williamcampbell
williamcampbell
Flag of United States of America image

The OCX is simply a COM Object that exposes several Interfaces.

You can do a CoCreateObject using it GUID then do a QueryInterface for the control function
Interface.

Use OleView to study the OCX and find its GUID and the exposed Interfaces.

Alternatively if the OCX is registered it should show up as an insertable object. Once inserted you can query it and call hide or set the size to 0,0. Not 1005 sure on this route maybe some experimentation is in order.

Something to start you off anyways.

Avatar of HalfAsleep
HalfAsleep

As long as the control functions are not reliant on the component's own GUI, then you can call those control functions if they are exposed, and create your own GUI.  If the Control creates and shows the GUI as soon as you instantiate it, then you are out of luck I think.
Avatar of DanRollins
The simplest technique would be to use the AppWizard create a dialog-based app. In the dialog editor, right-click and select "Insert ActiveX object" then find the object. When you are done, the ClassWizard will provide you with a C++ wrapper for the exposed functions of the object.
There may be exposed functions that do no require a U/I and/or it may be possible to make the window that is normally displayed hidden or set its size to 0,0.
If you need help with any of this, please reply.
Avatar of cskiong

ASKER

Hi DanRollins,

I would like to try your way, can you please kindly advise me on these by some step-by-step example?
I tried to Insert the Active X control object to my dialog, somehow it just turns out to be a white rectangular.  I have used OLEView to view the control, luckily the functions are exposed.

Thank you!
The functions will have to be exposed AND not have UI dependencies.  Aside from that, the white area, does it show at design time only, or does it show at runtime as well?  A common method, is to create a separate form to put the control on, and keep that form invisible.

After you have added the control to the form, can you call the control methods without getting errors and without the UI appearing?

Also, does this ActiveX rely on a windows handle?  .NET application are not very keen on handing out windows handles (unmanaged/unsafe code??), which may be why you only see a white area, instead of the ActiveX UI properly.
Hmm.  I just saw that your application is also MFC, so disregard the .NET part of my comment.
If you have used the dialog editor to insert an ActiveX object onto the form, then use the ClassWizard to create a "Control" type variable for that object.  The ClassWizard will then automatically generate .CPP and .H files that describe and will let you access the exposed functions of the object.
-- Dan
Avatar of cskiong

ASKER

Hi All,  thanks for your suggestions and advices, I'm working on it and it really gives me a great start.  I have to create a wrapper dll for the object so that it can be reused in future for all kinds of Windows Programming language.  Any advices or experiences to share on this?  Thanks in advance.

HalfAsleep, thanks for your suggestion, it is a good approach for me.  Luckily the white area only appears at design time, and I manage to call the control functions with no UI appearing.
Avatar of cskiong

ASKER

Hi,
I have created a DLL which wraps the ActiveX Object.  I create the ActiveX object in the DLL with the following code Snippet:

However at the VC++ 2005 Compile Output window, it shows the following message when I call the Wrapper->InitControl(this, 0);

Control wants to be windowless
ATL: IOleControlImpl::GetControlInfo not implemented.

Any idea?  Thanks in advance.
int Wrapper::InitControl (CWnd *pWnd, int nCtrlID)
{
CRect rect(0, 0, 50, 50);
m_Object.Create(NULL, 0, rect, pWnd, nCtrlID);
m_Object.ShowWindow(SW_HIDE);
return S_OK;
}

Open in new window

I do not know what your wrapper DLL consists of, but an ActiveX component, or any COM object, is dependent on a set of methods from IOleControlImpl among others.

This is because any entity that wants to "contain" an ActiveX component, has to be an ActiveX control container.  My guess is that you will have to declare a class that inherits from IOleControlImpl and maybe others as well, and then implement some of these methods.  I am not very experienced with making ActiveX control containers, so I may be a bit in over my head here.  I will see if I can find out more when I get home from work.

What you can also do, is create a very simple ActiveX component yourself, maybe just containing one property and one method, and see if you can create a wrapper for that.  Another benefit of that, is that you can post the entire source here as well.  Again, I will see if I can find out more after work :)
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cskiong

ASKER

To DanRollins, thank you for your kind advice, I'm working on with your approach.
To HalfAsleep, really appreciate your help and posts, which really do assist in my work.  Your last post is a good reference to me.