ComHelper

COM and ACTIVEX Programming made easier in HotBasic

Part IV – Using a helper file without the Use Invoke option

General Summary

Part I – Getting started

Part II – Using ComHelper

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

 Part VI – Using COM events

 

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 with a value.

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.


helper file type


Use Invoke


don’t Use Invoke


Applies to


Many objects (often the more complex ones) can be used that way. Some don’t, though, and must be called “non-Invoke” style (ComHelper will tell you when it is the case)


Most objects can be used that way. Some don’t, though, and must be called “Invoke-style”


What does the helper file contain


Generic COM and ACTIVEX declarations

Specific Types and Constants associated to the objects (if selected).


Generic COM and ACTIVEX declarations

Specific Types and Constants associated to the objects (if selected).

Specific code for accessing the object’s subs and functions.


How to create an object


Use the INTERFACE type and the CreateObject keyword. Example:

dim intf as INTERFACE
intf.CreateObject(“Word.Application”)


Use a specific type and a specific method, defined in the helper file. Example:

dim intf as IPicture
intf.CreatePicture


How to access an object’s sub or function


Use the invoke keyword to call a method. Use getnum if the method returns a number, or getstr if it returns a string.

obj.invoke(“DoThis”)

x=obj.getnum(“Height”)

s$=obj.getstr(“Name”)


Use one of the object’s methods, as explicitly defined in the helper file. Example:

obj.DoThis

x=obj.Height

s$=obj.Name


Advantages


Compact code (small include file)

Fast compilation


Faster execution

Syntax-checking when compiling sub and function calls


Drawbacks


Slower at run-time

No syntax checking of sub and function calls


Code overhead (can be really big)

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.

Now, you may want to:

-          go back to the Use Invoke section

-          go to the ActiveX Objects section.

-     go to the Using COM events section