Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 18.9 ExercisesChapter 19Next: 19.2 Creating Automation Objects
 

19. OLE Automation

Contents:
Introduction to OLE Automation
Creating Automation Objects
Using Automation Objects
Variants
Tips and Techniques
Exercises

19.1 Introduction to OLE Automation

OLE Automation is a method for a client program to control an OLE server. Microsoft designed automation to be a solution for the problem of cross-application macro programming. Because there seemed to be little chance of convincing users to use a single language, the best solution was to make a way for any language to access the capabilities that an application chose to offer.

Automation objects provide two types of interactivity: properties and methods. Properties are values that you can get and set.[1] Methods are functions that can be called with (optional) parameters and (optionally) provide a return value, possibly even another automation object. PerlScript also provides support for OLE events, which are a type of handler that get invoked when certain things happen, such as when the user clicks on a button in a browser. However, we will limit our discussion to automation and to properties and methods.

[1] Although, it is possible to have read only properties.

Perl implements support for automation objects in the same way as for any other Perl object. Object methods can be called using the pointer arrow:

$obj->some_func();  # call some_func() method of $obj

Properties are stored in a hash, and can also be accessed through the pointer arrow:

$obj->{foo} = "Some String";   # set foo to Some String
$val = $obj->{foo};            # get the value of foo

Notice that we normally don't need to enclose the property name in quotes. If you're getting a property value, you can also use a short form:

$val = $obj->foo;              # get the value of foo
$obj->foo = $val;              # WRONG, set requires {foo}

Now, we know that we can control our favorite automation servers from Perl. But how do we know what methods, properties, and objects a server exposes? The answer, unfortunately, it that these things are completely server dependent. No standards for object names, methods, or properties exist. The best solution is to turn to your server's documentation for answers. If the server doesn't provide documentation, your situation is still not completely hopeless. If the automation server provides typelib information (an OLE mechanism to describe the interfaces that an OLE server provides), you can use an OLE object viewer (such as Microsoft's OLE2VW32.EXE) that can read OLE typelib information and try to figure out what methods and properties the object exposes on your own.

Automation servers come in a couple of different flavors. There are local servers that live in an application (.exe file) and run as their own processes. There are in-proc servers that live in DLLs (dynamic-link libraries) and run in the process of the automation controller. There are also remote servers that may run on a different machine using Distributed COM (DCOM).

Perl for Win32 cannot currently use OCX controls, which require additional OLE support.


Previous: 18.9 ExercisesLearning Perl on Win32 SystemsNext: 19.2 Creating Automation Objects
18.9 ExercisesBook Index19.2 Creating Automation Objects