ComHelper
COM and ACTIVEX
Programming made easier in HotBasic
Part IV – Using
a helper file without the Use Invoke option
Part III – Using a helper file
with the Use Invoke option
Part IV – Using a helper file without the Use Invoke
option
Part V – Using an ActiveX helper
file
Unchecking the Use Invoke option
You probably have noticed the Use Invoke
checkbox on ComHelper’s screen. What if you uncheck it?
In that case, the helper file will be somehow
different. It will contain information about the same objects, but packaged
differently. In short, the helper file is more detailed, and more specific code
is generated for every interface. Subs and functions are not called through the
generic Invoke keyword but by using their individual names. This has
both advantages and drawbacks (see a discussion at the end of this page: Invoke or not Invoke).
If you have unchecked the Use Invoke checkbox
upon generating you helper file, the first thing you should do is to open the
.inc file generated and to try compiling it with HotBasic. The reason is that
some code inside is generated automatically, so there may be errors or some
name conflicts with HotBasic reserved words. Those errors need to be fixed
manually.
Once tested, include the .inc file into your main
program, by adding:
$include "XXX.inc"
Contents of the helper file
The .inc file contains:
- some generic declarations for COM programming (unless you unchecked the Include
COM Generics option),
- the declaration of the data types you chose to include (if any),
- the declaration of the constants you chose to include (if any),
- the declaration of the interfaces you chose, as HotBasic types. This is the
core part of the file.
The big difference with the Use Invoke option
is that explicit code is generated for every sub and every function that the interface
contains. Instead of calling a sub through Invoke, you call it directly
by its name.
Using an Interface
An Interface is declared as a specific HotBasic
user-defined type. You can dim a variable of that type and use its properties
and methods.
$include "calendar.inc" ‘Use Invoke’ unchecked
dim o as Icalendar
o.CreateCalendar
Then you access its subs and functions as members of
the interface:
o.Quit
o.set_Height(100)
wb.Navigate(url$, void, void, void, void)
The helper file contains a description of all the subs
and functions, along with their arguments. What many methods do is pretty
straightforward from their name and the arguments they take. For some others,
it is not obvious and you should look for some documentation or some
third-party code to help you.
Filling the interface pointer
An interface still has a .pointer property, which is
an integer initialized with 0. You can do nothing with the interface until you
have somehow filled its .pointer
The difference with the Use Invoke style is
that instead of using CreateObject(<object type>), you’re provided
with an explicit sub for every specific object type you selected.
o.CreateCalendar ‘instead of
o.CreateObject(“Microsoft.Calendar”)
Refer to Filling the
interface pointer for more details.
Invoke or
not Invoke?
How to decide to Use Invoke or not? Both ways have their advantages and drawbacks. Here is a summary of the differences.
|
|
|
|
|
|
|
Specific Types and Constants associated to the
objects (if selected). |
Specific Types and Constants associated to the
objects (if selected). Specific code for accessing the object’s subs and
functions. |
|
dim intf as INTERFACE |
dim intf as IPicture |
|
obj.invoke(“DoThis”) x=obj.getnum(“Height”) s$=obj.getstr(“Name”) |
x=obj.Height s$=obj.Name |
|
Fast compilation |
Syntax-checking when compiling sub and function
calls |
|
No syntax checking of sub and function calls |
Slow compilation |
Tip: I suggest you leave the Use Invoke
option checked, unless:
- ComHelper
tells you that the interface you selected does not support Invoke
- Or if you run
into problems of run-time speed.
The Include COM Generics option
You may have noticed another option for generating
helper files: Include COM Generics.
It refers to the set of common declarations and
initializations that are needed to use COM and ActiveX. Normally, this section,
called “COM Generics” is included at the beginning of the helper file.
However, since it is always the same, you may prefer
to save it to a separate file that you manually $include in your main program.
This way, you can uncheck the Include COM Generics option. That keeps your helper file
smaller and more legible.
- go back to
the Use Invoke section
- go to the ActiveX Objects section.
- go to
the Using COM events
section