Using COM components

About COM components

COM components (also known as ActiveX components or ActiveX controls) provide various useful functions. For example, the Wsh (Windows Script Host) component provides many file, drive, network and other functions that are not available as intrinsic QM functions. In the web browser control, for example, you can display web pages in dialogs. Working with COM components requires minimum programming knowledge. You can simply select functions from the list, without even reading help files.

 

There are two main types of COM components - ActiveX controls, and non-control components. ActiveX controls provide graphical user interface and are used in dialogs. Other components are invisible but provide various useful functions. Controls are added to dialogs using the Dialog Editor. Non-controls are created using _create or other functions.

 

Most COM components provide type libraries. Type libraries contain declarations of coclasses, interfaces and functions provided by these components, making available them to other programs, including QM.

 

See also: COM support in QM

The 'COM Libraries (Type Libraries, ActiveX Controls)' dialog

Here you can see registered (installed) type libraries. You also can register new components and separate type libraries. The main purpose of the dialog - find a component's type library and insert typelib declaration statement in current macro. Declaration is needed to use the component in macros.

 

 

The path of the file that contains the selected type library is displayed at the bottom of the dialog. If there are separate type libraries for several languages, you can select language.

 

At the right side is displayed some information from the type library: name (although you can use any name), coclasses (types of COM objects) and interfaces (collections of functions that can be used with these COM objects).

Finding and installing COM components

Many COM components are installed and used by Windows and various applications. Some of them also can be useful in QM. Many other COM components are on the Internet.

 

It is not easy to find a good component on the Internet, especially if you want a freeware component. Many components are too simple to be useful, or have bugs, or are too big, or too expensive, or depend on various runtime files.

 

New (downloaded) COM components must be registered (installed). Some downloaded components have setup programs and are installed like any other software. Then you can find component's type library in the COM Libraries dialog. Other components came without a setup program, as an ocx or dll file in a zip file. To install a component that does not have a setup program, use the Register button in the COM Libraries dialog. It is possible to use COM components without registering; read next chapter.

 

Usually type libraries are embedded in component files (dll, ocx) and are registered together with component. If you have downloaded a separate type library (tlb, olb, dll), register it using the Register button.

 

If you move a component file to a different location, you have to Register it again (it will not be reregistered automatically).

 

Registered type libraries can be declared using GUID (syntax1) or file path (syntax2). Unregistered - only by path.

Distributing macros that use COM components

If you want to distribute macros that use a COM component, make sure that users have the component. Also, they must have compatible version. The macro will work if they have type library with same major version as in typelib statement, and same or higher minor version. This is actual even if you distribute exe file. You can distribute the component with your macros.

 

If you distribute a COM component with your macros, users or your setup macro/program must register it on that computer. COM components can be registered/unregistered using the COM Libraries dialog (see above) or function RegisterComComponent. Usually, when registering a component, it also registers its type library. To register .NET COM components, instead use regasm.exe; it is in .NET runtime folder; more info is on the internet.

 

It is possible to use COM components without registering. There are 2 ways:

The first way is easier, faster and works on all Windows versions, but does not work with .NET COM components and some other components. The second way works with all components, but requires at least Windows XP SP2.

 

Example of downloading automatically.

Components can be downloaded automatically, using function DownloadComponent. Example:

 

typelib MSScript {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC} 1.0 0 1
#err MSScriptDownload

 

Function MSScriptDownload:

 

lpstr s="$system$\msscript.ocx"
if(FileExists(s)) ret
int fa=&RegisterComComponent
mac "DownloadComponent" "" s "http://www.quickmacros.com/com/msscript.zip" "Microsoft Windows Script Control" 43 fa 6
ret -1

 

It runs at compile time. The typelib statement throws error if the COM component is not installed. The next statement handles the error and calls function that downloads and registers the component. However this cannot be used in exe; will need to somehow detect unavailable component at run time, for example if _create fails.

Using COM components

To use a component, its type library must be declared. The declaration statement can be in the same macro, or in other macro, e.g., in init2 function or some other function that runs at startup (has "QM file loaded" trigger). Several type libraries are already declared by QM, and you can see them at the bottom of the list when you type dot.

 

When just evaluating new component, you can simply create new macro, insert the type library declaration statement (using the COM Libraries dialog), and run the macro, or just compile (Ctrl+Shift+R). Then you can type the library name in the next line, type dot, and see what classes are available. Classes are listed at the top of the list. When you double click a class in the list, its name is inserted in the macro. If it is an ActiveX control class, you usually can see ";;ActiveX control" in QM status bar. Some controls also can be used as non-control components, that is, created using _create, not in a dialog.

 

The following are two examples how new COM components are typically installed and used. Steps 1 and 2 are omitted if you have found the component on your computer (in the COM Libraries dialog).

Example1: installing and using a non-control component

1. Find it on the Internet, and download. When searching, you can include keywords "COM component", "ActiveX component", "ActiveX DLL", "ActiveX control".

2. Install the component using the setup program or the Register button in the COM Libraries dialog.

3. Insert type library declaration: In the COM Libraries dialog, find component's type library and click Insert Declaration From List. If you cannot find it in the list, try Insert Declaration From File.

4. Run or compile the macro where you have inserted the declaration (typelib ...). If you have placed the declaration in the init2 function or some other function that runs at startup (has "QM file loaded" trigger), it is compiled automatically. Normally, you don't have to somehow separately compile just to run the macro, but while creating the macro you cannot see the available classes and functions until the declaration is not compiled.

5. Type the library name (it is the first word after typelib). Then type dot, and double click a coclass (coclasses are at the top of the list). If there are several coclasses, pick one that have a meaningful name, eg something similar to the component name.

6. Declare a variable of that class, and call _create.

7. Then you can call component's functions, and/or set events. Available functions are shown when you type dot after variable name. Those with green brick icon are methods. Gray icons - properties. Lightning icons - events.

 

Example:

 

Excel.Application a._create
a.SomeMethod(arguments)
somevariable=a.SomeProperty
a.SomeProperty=somevalue

Example2: installing and using an ActiveX control

1. Find it on the Internet, and download. When searching, you can include keywords "ActiveX control".

2. Install. Same as above.

3. Create or open the smart dialog where you will place the ActiveX control.

4. In the Dialog Editor, click "ActiveX controls..." in the list. It opens the COM Libraries dialog that displays available (registered) ActiveX controls.

5. In the COM Libraries dialog, select the control and click Add Control To Dialog. Click Yes if prompts to insert type library declaration.

6. It adds the control or its placeholder to the dialog. You can move/resize/zorder it like any other control.

7. If you want to use events, click the control in the Dialog Editor and click Events. It inserts a statement that declares an interface pointer variable for the control, and another statement that calls _getcontrol function. It also gives you instructions how to set events. The declaration is inserted under WM_INITDIALOG. If you will use the control under other case statements too, use the _getcontrol function everywhere to initialize the variable.

8. Then you can call component's functions, and/or set events. Same as step 7 above.

 

Example