Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching COM object event 1

Status
Not open for further replies.

drdad5727

Programmer
Feb 28, 2005
28
US
I have a COM library written in C++ not VB that exports three COM objects, one of which fires Events. These objects are not controls/components. The (first) question is: How do I catch those event in VB?

I emphasize that they're not controls because I know how to deal with events fired by a control. You put them on a form and the Sub's to capture their events show up automatically in the Form code. But the COM object that fires events doesn't belong on any Form (I don't think) because it has no visible presence and besides, it needs to be global. That is, the VB programmer has to instantiate it exactly once and then get to it from throughout his application.

Second question: The Events fired by the one COM object have parameters that are (arrays of) instances of the other two COM objects. The parameters are defined as VARIANT in the COM event interface. How should they be defined in the VB code? (All the object types are defined in the one type library so the VB code has access to them.) That is, how can the VB programmer treat them as objects of the correct COM type?

PS: I'm the C++ programmer who designed this without much consideration of how to get to it from VB. Since I know almost nothing about VB. Now, I wonder if I've done it all wrong.

 
Are they COM events or COM callbacks?

The events normally result in a window message, and in VB6 these would be caught and dispatched to corresponding event handler procedures by a Form's message loop. The message loop is actually a combination of information generated by the VB6 compiler and code running in the VB6 runtime.

So... unless instances of your objects are created "WithEvents" in a VB6 Form or Class (which must in turn be instantiated in a Form) I don't think you have any mechanism to handle these events.
 
Thanks for the reply.

I completely buy the explanation that a message loop is needed. So I guess that means a form is needed. But, the object's not a Component, so I can't draw it on a form.

Can you give me a quick example of how to create "instances of [my] objects created WithEvents in a .. Class (which must in turn be instantiated in a Form)"? I'd be really, really grateful.

 
In the VB6 Form you'd declare an object reference with Form-Global scope (general declarations at the head of the Form code) as in:

[tt]Private WithEvents Instance As MyLib.Class1[/tt]

Somewhere else (perhaps in the Form_Load event handler) you'd assign an instance as in:

[tt]Set Instance = New MyLib.Class1[/tt]

Just as with a control "painted" onto the Form, the VB6 IDE will snoop the typelib and add "Instance" and its events to the dropdown lists in the Form's code-view pane above the source code text. By selecting "Instance" and its events in these dropdowns you'll cause the IDE to generate stubs for each event handler.

Nothing says you can't declare this instance Public, but that causes the compiler to create a hidden getter/setter pair. In other words "Instance" would then also be a read/write Public Property of the Form, exposed to other code in the program.
 
I see I missed something.

You can declare "Instance" and instantiate it the same way within a VB6 Class. The event handlers are then created and fleshed out within the Class in the same way I described above.

This Class itself must be instantiated within a Form (or another Class itself instantiated in a Form). But the wrapper Class will not need to be declared WithEvents in the Form that uses it.

You can also do this same thing within a UserControl, but that probably isn't where you need to go.
 
Just to be complete: you of course need to set a reference in the VB6 IDE to your "MyLib.dll" which must also have a registered COM typelib (CoClasses, Interfaces, etc.).
 
Yes Indeed! Yes Indeed!

You are a wonderful person. May all your bits line up neatly and do just what you want them to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top