Pascal Scripting: Using COM Automation objects

The Pascal script can access COM (also known as OLE or ActiveX) methods and properties via the COM Automation objects support. This allows you to access for example standard Windows COM servers, custom COM servers, Visual Basic ActiveX DLLs and .NET assemblies via COM Interop.

IDispatch based COM

There are two support functions to initialize IDispatch based COM Automation objects: CreateOleObject and GetActiveOleObject.

Use CreateOleObject to create a new COM object with the specified class name. This function returns a variable of type Variant if successful and throws an exception otherwise.

Use GetActiveOleObject to connect to an existing COM object with the specified class name. This function returns a variable of type Variant if successful and throws an exception otherwise. In case of some programs, this can be used to detect whether the program is running or not.

The value returned by CreateOleObject or GetActiveOleObject can then be used to access the properties and methods of the COM object. The access is done via 'late binding' which means it is not checked whether the methods or properties you're trying to access actually exist until Setup actually needs to at run time.

To access a property or method whose name is a reserved word, use IDispatchInvoke.

Open the "CodeAutomation.iss" file in the "Examples" subdirectory in your Inno Setup directory for an example script using IDispatch based COM Automation objects.

IUnknown based COM

If the IDispatch interface isn't implemented by the object, you can use the IUnknown based COM support.

To initialize IUnknown based COM Automation objects use CreateComObject.

The value returned by CreateComObject can then be used to access the methods of the COM object after casting it to the desired interface. The access is done via 'early binding' which means the desired interface needs to be defined in the script, unlike for IDispatch based COM support.

StringToGUID can be used to convert the string representation of a GUID into a 'real' GUID. Use OleCheck to check the return values of any method you call.

If you copy the interface definition from any existing Delphi source code, remove the brackets around the interface GUID string. Also remove any calling conventions, Inno Setup assumes 'stdcall'. If the interface contains any functions you won't call, you can replace these by dummies to avoid having to define any special types used by them.

Open the "CodeAutomation2.iss" file in the "Examples" subdirectory in your Inno Setup directory for an example script using IUnknown based COM Automation objects.

Note: IUnknown based COM support requires Unicode Inno Setup.

General

COM objects are released automatically when they go out of scope. There are no functions to 'destroy' or 'free' them.

If you are extracting a COM Automation library to a temporary location and want to be able to delete it after using it, make sure you no longer have any references to the library and then call CoFreeUnusedLibraries. This Windows function will then attempt to unload the library so you can delete it.