Monday, November 17, 2008

VBScript Custom Actions


With VBScript actions (as well as with MSI DLL actions, described in the following section),

the general process is to assemble a record containing the message information, and then

send the record to the running installation. With VBScript, you use Installer.CreateRecord to

create the message record, and use Session.Message to send the record to the running

installation.

The record begins with a "template" in field 0, which is a string containing placeholders of

the form [1], [2], and so forth. These placeholders will then be filled in with the values in

record fields 1, 2, and so on.

To demonstrate a VBScript action writing to the log, you can create an immediate-mode

VBScript custom action called callLoggingTestVBS, scheduled immediately after

LaunchConditions, with the following code.

(For this example, you can store the code directly in the custom action since the script is

short. In practice, using an external .vbs file is generally recommended so you can specify a

function return value and optionally exit the installer from the custom action.)

Const msiMessageTypeInfo = &H04000000

' create the message record

Set msgrec = Installer.CreateRecord(1)

' field 0 is the template

msgrec.StringData(0) = "Log: [1]"

' field 1, to be placed in [1] placeholder

msgrec.StringData(1) = "Calling LoggingTestVBS..."

' send message to running installer

Session.Message msiMessageTypeInfo, msgrec

The action might appear similar to the following figure.

After building the project and creating a log file, the following lines should appear in the log:

Action 25:00:00: callLoggingTestVBS.

Action start 25:00:00: callLoggingTestVBS.

[...lines omitted...]

Log: Calling LoggingTestVBS...

Action ended 25:00:00: callLoggingTestVBS. Return value 0.

A possible refinement is to modify the template field (field 0) of the record to include a

timestamp or information about whether the action is being called from immediate

execution or deferred execution. Studying the log messages displayed by standard actions

can provide a useful model.

No comments: