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 Type Libraries dialog

In the Type Libraries dialog 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.

 

Find - search the list of type library names or control names. Selects next item that contains the above text.

Insert declaration from list - insert type library declaration in current macro. What to do with it - read the "Using COM components" chapter below in this topic.

Use file path - if checked, the declaration will use syntax2. By default is used syntax1.

Insert declaration from file... - locate a type library file and insert declaration statement in current macro. The library may or may not be in the list. This does not register the type library or component.

Register..., Unregister... - register (install) or unregister (uninstall) a component or separate type library. Registers/unregisters only dll and ocx components. Registers/unregisters type libraries in any files that contain them.

Libraries - view registered type libraries. The list includes type libraries for ActiveX controls and other COM components, and other type libraries.

Controls - view registered ActiveX controls. When you select a control, is shown some info from its type library, if available. When you click Insert Declaration From List, is inserted its type library declaration.

Preview - see how looks selected control. Not all controls will look as they look at run time because some features must be activated by calling their functions at run time (in macro).

Add control to dialog - adds selected control to dialog. Available when opened from the dialog editor.

 

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 top-right 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 available for download on the Internet.

 

It is not easy to find a good component on the Internet, especially if you want to find a freeware component. Many components are too simple to be useful, or don't support some required features, or have bugs, or are too big, or too expensive, or depend on various runtime files. For example, many components are created with Visual Basic and depend on MSVBVM60.dll or MSVBVM50.dll that may be unavailable on some older Windows versions or on Vista.

 

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 Type 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 Type Libraries dialog.

 

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 COM components, make sure that your clients have the component. Also, they must have compatible version. The macro will work if they have type library with same major version that is declared in typelib statement, and same or higher minor version. If your clients don't have the component, you should distribute the component with your macros, or give a download link. This is actual even if you distribute exe file.

 

If you distribute a COM component with your macros, your clients or your setup macro/program must register it. COM components can be registered/unregistered using the Type Libraries dialog (see above) or function RegisterComComponent (flag 4 recommended). Usually, when registering a component, it also registers its type library.

 

Components also 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(dir(s)) ret
int fa=&RegisterComComponent
mac "DownloadComponent" "" s "http://www.quickmacros.com/com/msscript.zip" "Microsoft Windows Script Control" 43 fa 6
ret -1

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 a period.

 

When just evaluating new component, you can simply create new macro, insert the type library declaration statement (using the Type Libraries dialog), and run the macro, or just compile (Ctrl+Shift+R). Then you can type the library name in the next line, type a period, 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 "... . control" in the 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 ommitted if you have found the component on your computer (in the Type 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 Type Libraries dialog.

3. Insert type library declaration: In the Type 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 a period, 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 a period 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 Type Libraries dialog that displays available (registered) ActiveX controls.

5. In the Type 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