PDA

Click to See Complete Forum and Search --> : Set current date in Registry string


johnpc
May 1st, 2008, 05:34 AM
Can the current date be set in the registry section of VS Installer?

dilettante
May 1st, 2008, 07:30 AM
Are you asking if the current date can be recorded in some arbitrary registry key?

johnpc
May 1st, 2008, 01:27 PM
I would like to use the HKCU\Software\JCunningham Key and set a string value "Installed Date" and somehow code in the current date.

dilettante
May 1st, 2008, 02:38 PM
Well here is an example of how you might do this assuming you're using Visual Studio Installer 1.1:

There is no direct way that I have found to do this. However it is possible to set the value for a registry key to be equal to an MSI database's Property value. Create the entry for the desired registry key via VSI and provide as its value a bracketed Property name.

The snag is that VSI 1.1 doesn't let you define custom property table entries. Even if it did, there is no direct way I have found to set the value dynamically to something like the current date.

To get around this one workaround I have found is to run a script against the MSI database to insert a small bit of VBScript into the database and schedule a Custom Action that runs it early in the installation process. This VBScript function is called by Windows Installer, and it can set a custom Property to the current date quite easily.

Every time you build a new MSI you must run this injection script.

Here is the script:

AddScriptAction.wsf
<job id="AddScriptAction">
<!-- This script injects a VBScript Custom Action named CUSTOM_PROPS_ACTION
into the MSI database (defined below as String variable MSI) and
sequences it early in the installation process.

The injected script has a Function CustomPropsAction() that sets the
value of a custom Property CUST_INSTALL_DATE for use in setting a
registry key value.

The actual registry key is set as a normal registry action that was
predefined in the MSI via VSI 1.1 to reference our new custom Property
CUST_INSTALL_DATE.

As written, this script is expected to reside in the same folder as
the VSI 1.1 .SLN file. It is looking for the MSI database in a folder
beneath its own folder.
-->
<object id="Installer" progId="WindowsInstaller.Installer"/>
<reference object="WindowsInstaller.Installer"/>
<resource id="InjectedText">
<![CDATA[Function CustomPropsAction()
On Error Resume Next
Session.Property("CUST_INSTALL_DATE") = FormatDateTime(Date(), vbShortDate)
If Err.Number = 0 Then
CustomPropsAction = 1 'Ok.
Else
CustomPropsAction = 3 'Error.
End If
End Function]]>
</resource>
<script language="VBScript">
Option Explicit
Dim MSI, MSIDB, MSIRecord, MSIView

Function ScriptPath()
ScriptPath = Left(WScript.ScriptFullName, _
InStrRev(WScript.ScriptFullName, "\"))
End Function

'Open MSI database.
MSI = ScriptPath() & "Output\Disk_1\PeerChats.msi"
Set MSIDB = Installer.OpenDatabase(MSI, msiOpenDatabaseModeTransact)

'Create new property record.
Set MSIRecord = Installer.CreateRecord(2)
With MSIRecord
.StringData(1) = "CUSTOM_PROPS_ACTION"
.StringData(2) = getResource("InjectedText")
End With

'Insert record in Property table.
Set MSIView = MSIDB.OpenView("SELECT * FROM `Property`")
With MSIView
.Execute
.Modify msiViewModifyInsert, MSIRecord
.Close
End With

'Insert new custom action record.
Set MSIView = MSIDB.OpenView( _
"INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`)" _
& " VALUES ('CUSTOM_PROPS_ACTION', 54," _
& " 'CUSTOM_PROPS_ACTION', 'CustomPropsAction')")
With MSIView
.Execute
.Close
End With

'Sequence new custom action very early.
Set MSIView = MSIDB.OpenView( _
"INSERT INTO `InstallExecuteSequence` (`Action`, `Sequence`)" _
& " VALUES ('CUSTOM_PROPS_ACTION', 5)")
With MSIView
.Execute
.Close
End With

Set MSIView = Nothing
MSIDB.Commit
Set MSIDB = Nothing
MsgBox "MSI Database Updated", vbOkOnly, "AddScriptAction"
</script>
</job>

This works just fine here. Note that you will have to modify the line that sets MSI to the location of your .MSI file!

All of this is documented in the MSDN Library under Window Installer.

One caveat: For some reason you seem to want to set a key in HKCU. The question is, for which user?

This will be unreliable unless your MSI package is built for a per-user installation. This is not the default! Worse yet, under Vista these installs must run elevated, which means under an admin user. Thus the key will be created for that user alone, and nobody else.

There are workarounds, but they get ugly.

Are you sure you can't live with a key under HKLM, i.e. a per-machine value?


Here's a screen shot of VSI 1.1 showing how to set the registry value to an MSI database Property:

johnpc
May 1st, 2008, 08:09 PM
Hello, and Thank You Dilettante..........I think. This looks like the answer, I
will have to study it a bit to see how to implement it. I am not quite familiar
enough with implementing script int the VSI. I am using VSI 1.1 and I will
spend some time and try to work it out. Again Thanks & I'll get back to you:wave:

dilettante
May 1st, 2008, 08:39 PM
That's why I wrote the simple injection script. It takes care of three steps that you'd otherwise have to do by hand using Orca after each build.

Not many people here seem to know much about Windows Installer scripts, so take your time looking it over. You may need to consult the reference material to make much sense of it though.

dilettante
May 10th, 2008, 08:36 PM
Late update:

Further testing revealed a small but fatal flaw. If the MSI is installed in Vista by a non-admin user who supplies elevation credentials an Installer error may be encountered. The fix is to change one value:

'Insert new custom action record.
Set MSIView = MSIDB.OpenView( _
"INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`)" _
& " VALUES ('CUSTOM_PROPS_ACTION', 54," _
& " 'CUSTOM_PROPS_ACTION', 'CustomPropsAction')")

Becomes:
'Insert new custom action record.
Set MSIView = MSIDB.OpenView( _
"INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`)" _
& " VALUES ('CUSTOM_PROPS_ACTION', 3126," _
& " 'CUSTOM_PROPS_ACTION', 'CustomPropsAction')")

See: Is your MSI Package ready for Vista? (http://blogs.msdn.com/robmen/archive/2006/02/22/537106.aspx)

Clearly UAC has many layers to it.