Visual Prolog 7 Tutorials

How to use COM Components in Visual Prolog 7

Written by Yuri Ilyin
Prolog Development Center A/S

Last updated: 05-08-2006

This tutorial is a step by step description of adding external COM components to your project.

Download:  Source files of the example project used in this tutorial.

Import Component Interfaces Using Type Library

Use Project|New VDE menu item to create a new Visual Prolog project with the name comTutorial1.

The first step of importing a component is creation of declarations in Visual Prolog for the COM interfaces, exported by the component.

Fortunately, Visual Prolog VDE can create necessary declarations, using the information, stored in a COM type library. This tutorial uses the msxml component, that is usually located in c:\windows\system32\msxml2.dll.

To add the information from msxml2.dll to the project, use File|New menu item to create a new package.

In the Create Project Item dialog:

  1. Choose the COM Package alternative in the left list. 
  2. Load the type library by pressing the Load button.
  3. Enter msxml as Name for the package.
  4. Select all components and press the Create button:

The declarations will be created in the msxml subdirectory. To see the interfaces in the project tree it is necessary to build the project.

Creation of Component Instance

Now we are ready to invoke the imported component.  In this example we will use the DOMDocument component.

  1. Add handling of the event File/Open in the Code Expert for the Task window
  2. Add the following code to the file taskwindow.pro:
predicates  
  onFileOpen : window::menuItemListener. 
clauses  
  onFileOpen(_Source,_MenuTag) :-
    DomDocument = dOMDocument::new(),
    IsSuccessful= DomDocument:loadXML(
          "<customer><first_name>Paul</first_name>"
          "<last_name>Bright</last_name></customer>"),
    stdIO::write(IsSuccessful).

In order to use COM, the goal must call the predicate mainExe::runCom. This will ensure that COM is initialized properly.  Modify the goal in comTutorial1.pro like this:

goal
    mainExe::runCom(comTutorial1::run)

Now the component can be run. Recall that the component is invoked by the File|Open menu item.
 

Explanation for Files, Generated by IDE.

xxx_native interfaces represent COM interfaces as they look on the native COM level including support of IUnknown or IDispatch.

xxx_import interfaces represent the same interface, but at the prolog level. It contains all methods of xxx_native, but using prolog types. All COM arguments have the type <something>_import.

Important: The xxx_import interface can also return the COM level interface xxx_native. This is needed, when passing the interface as a parameter on the native level.

If the interface is imported, then the xxx_native interface is simply an external COM interface.

However, if you want to implement a COM interface, the _native part of the interface have to be implemented from Prolog part, because it is the _native part that really makes this to be a COM interface.

So, to implement a COM interface in Prolog, you have to implement both the Prolog part of the interface and the native COM part of the interface. This is, of course, not very nice, and therefore we have _export wrappers.

The xxx_export wrapper takes an xxx_impl implementation and constructs xxx_import interface on the top of it. Please, recall that the xxx_import interface can also return the COM level interface xxx_native, therefore you can pass xxx_native as output.

The xxx_impl interface corresponds to the xxx_import interface, except the following:

  • It does not contain the method for obtaining the xxx_native interface.
  • It has longer predicate names.

So, basically the xxx_export wrapper adds the xxx_native part of the interface to the pure prolog implementation of it. And thus the xxx_export takes a pure prolog interface and turns it into a COM interface on Prolog level. So such a wrapped interface can be passed as an argument on the Prolog level.

The longer names are necessary, because, when implementing a COM component (a CoClass), you will often need to implement several interfaces in a single class, and the longer names prevent name clashes (since the interface name is a part of each predicate).

All COM interface arguments on the xxx_impl interface will also have type xxx_import, because this is a form that all COM interfaces have in the prolog program regardless of whether they are imported or exported.

The scheme for implementing an interface looks like this:

inherits xxx_export % luckily I can inherit the _native part from xxx_export
supports xxx_impl % I only have to implement the pure Prolog part

class  myImplementationOfxxx : xxx_import 
% I am implementing a Prolog level COM interface

implement  myImplementationOfxxx
inherits  xxx_export 
% I can inherit the _native part from xxx_export
supports  xxx_impl 
% I only have to implement the pure Prolog part

 

Note

The features described in this tutorial are supported in Visual Prolog 7.0 Commercial Edition.

 

Home | Company | News | Products Downloads | Shop | Support | Visual Prolog Features | Visual Prolog Compiler | FAQ | Tutorials | Examples | How to Migrate from Visual Prolog 5 to Visual Prolog 7 | Knowledge Base | Discussion Forum | wiki | Site Map
 

Prolog Development Center A/S - H.J. Holst Vej 3-5C - 2605 Broendby, Denmark - Tel +45 3636 0000 - Fax +45 3636 0001 - sales@visual-prolog.com