Occasionally, when developing a desktop application or service in PHP, you’ll need to delegate tasks to components of Windows. This is where COM comes in. Luckily, PHP has had working COM support for several years.
What is COM?
The best description of COM, in this context, comes from the PHP documentation:
“COM is an acronym for Component Object Model; it is an object orientated layer (and associated services) on top of DCE RPC (an open standard) and defines a common calling convention that enables code written in any language to call and interoperate with code written in any other language (provided those languages are COM aware). Not only can the code be written in any language, but it need not even be part of the same executable; the code can be loaded from a DLL, be found in another process running on the same machine, or, with DCOM (Distributed COM), be found in another process on a remote machine, all without your code even needing to know where a component resides.”
For the purposes of this post, I’d simplify that to the following:
COM defines a means of manipulating Windows applications and services which is independent of any programming language. As well, it provides a means to expose an interface to other applications to allow them to manipulate your application or service.
What Can I Do?
COM is especially useful for dealing with Microsoft-developed applications such as the Windows Registry, Internet Explorer, Office applications, and so on. Without COM, it is nearly impossible to elegantly interface with these applications in any meaningful way.
In this post, I’ll detail a few use-cases, with some example code, and provide some tools that will make developing your own applications easier.
OLE/COM Object Explorer provides detailed information about every “Object Class”, “Application ID”, “Type Library” and “Interface” exposed by COM on the Windows system. It is essential for COM application development.
MSDN Library provides (somewhat poor) documentation for a number of important COM objects, such as InternetExplorer, Word, etc. It’s not essential that you use this, but it can augment your understanding that you would otherwise gain from inspecting the Object signature.
The com_print_typeinfo function in PHP will output a skeleton class of a COM Object, which can provide some useful information regarding the signature of that Object. This is the primary way that you will want to inspect COM Objects.
This article does a decent job of explaining some of the fundamentals of dealing with COM. Specifically, the Technical Details section is of particular help.
COM is very-much-so designed in Object-Oriented style, so the terminology is all fairly straightforward if you are familiar with PHP’s OOP style.
Each application is referred to as a class. This is similar to a Class in PHP, where the Object is an instance of a particular Class. It is a wrapper of sorts for all of the various methods that are exposed.
An interface defines what methods a particular class implements. Knowing which interfaces a class implements, you can know which methods are available and how they’re invoked.
A type library defines a set of interfaces which a class implements.
GUID / Application ID
A GUID is a unique identifier that can be used to refer to an application or interface, rather than by object or interface name.
Useful Application Classes
This COM Object provides an interface to Internet Explorer, which can be used to spawn a browser window, cause it to navigate, and trigger any number of events that the browser provides, such as printing.
Previously, I’ve used Internet Explorer as a quick and dirty rendering engine for report generation and printing. I’ll detail that process in one of the examples.
This COM Object provides an interface to a number of integral mid-level functions in Windows, such as registry access, shortcut creation and accessing system folders.
I’ll detail how to get and set registry values in one of the examples.
This COM Object provides an interface to Microsoft Excel, which can be used to create Excel spreadsheets without having to rely on a third-party library.
The Basics in PHP
Before we get to any examples, let’s cover some of the basics of working with COM in PHP.
In the vast majority of situations, all you will need to do to access a COM Object is instantiate the COM class, with the name of the COM Object Class as the single parameter. Doing so should create an overloaded object with all of the methods of the Class.
If you would like to see what methods are exposed by the object, you can simply use com_print_typeinfo as mentioned earlier. This will output a signature for the class.
Lastly, you may wish to “sink” an events interface for the application you’re interfacing with. What this means is that you can create a class with methods that will be called when certain events occur (are dispatched) in the application. For instance, you may wish to sink the “ProgressChange” event in Internet Explorer’s DWebBrowserEvents2 interface, which will call your specified class method every time a download’s progress changes.
Here are a few examples of things you can do with COM.