Pete Furness
Posts: 16
Joined: Fri Feb 20, 2004 9:34 am
Location: SURREY, UK

Modify registry key

Can I read the value of a registry key during install, modify it, then save new value, as part of the install? (And reverse the process on uninstall.)

If the answer is "use a custom action" please give me an example or point me to a web-site where I can get one.

Here's what I'm trying to do, so you might be able to suggest a different approach to the above (sorry for the length)...

I have an app that uses several databases. The app and each database have seperate AI installs. App is installed first.
User runs app and it detects no database registry entry (yet) and configures in "mode1".
Database1 is installed and registry entry "x=1" is created.
User runs app that detects (x=1) and configures in "mode2" (and will operate in mode2 until x=0)
Database2 is installed and increments "x" so "x=2".
and so on...
User is finished with database1 and uninstalls it, x is decremented (x=1)
When last database is uninstalled x=0 and application will start in "mode1" again.
Philip
Posts: 27
Joined: Fri Apr 04, 2003 2:06 pm
Contact: Website

Hi Pete,

This task can only be accomplished via a Custom Action. A custom action is a DLL, EXE, or a script that is launched by the installer at four different times in the installation process: install, commit, rollback, uninstall.

We are interested in commit and uninstall for this example. Commit is the time when the installation was successfully completed.

Since you need two actions done (increment and decrement the registry entry), you need two such custom actions. One that is run at commit time and one that is run at uninstall time.

You can accomplish the same thing with one custom action, that will be run at both commit and uninstall, but which will need a parameter to tell it what to do, increment or decrement the registry entry. Such a parameter can only be passed to an EXE custom action.

So, to wrap up, there are two ways to accomplish your task:

create a single custom action (.EXE that receives a parameter)

1) create a task in the commit section and assign it to your EXE file.
2) enter a parameter in the command line (such as: /inc)
3) create a new task in the uninstall section with the same EXE file
4) enter a different parameter in the command line (such as: /dec)

create two separate custom actions (can also be DLLs)

1) create a task in the commit section and assign the DLL that increments the registry entry value
2) create a new task in the uninstall section and assign the DLL that decrements the registry entry value

Inside a custom action (EXE, DLL) you can use any methods from the Platform SDK to to access the Windows registry (e.g. RegOpenKey, RegQueryValueEx).

Hope this helps,

Philip
Philip Petrescu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Pete Furness
Posts: 16
Joined: Fri Feb 20, 2004 9:34 am
Location: SURREY, UK

OK thanks.
I am not a "C" programmer so my only option would be to build the .exe in VB6 - no problem. However will I run into bootstrap problems doing this? i.e. at the FIRST install are the VB support files (e.g msvbvm60.dll etc) I include in my package, installed and registered by the time the INSTALL custom action runs?
Pete Furness
Posts: 16
Joined: Fri Feb 20, 2004 9:34 am
Location: SURREY, UK

I have built a simple EXE and it seems to work fine. I have specified as "file attached to installation".

Can you give some more information on the choice between this method and an "installed custom action"?

When I attach to the uninstall section the file has a "_1" suffix added. What does this mean?

For anyone who is interested, here is the EXE (VB) code...
'=====================================================================================
'Copyright : Furness Associates © 2004
'Module Name: MMain
'Module Type: BAS Module
'Description: Custom action EXE for MRC-GPRF Database Install/UnInstall
'Author : Peter Furness
'Last Edit : 26/04/2004
'=====================================================================================
'Comments : For INSTALL (@COMMIT) use "/i /[TARGETDIR]"
' : For UNINSTALL use "/u"
' : Any errors simply result in a fail without raising.
'=====================================================================================

Option Explicit
Option Compare Text

Sub Main()

Dim i As Integer
Dim ooRegistry As CRegistry
Dim olDatabases As Long
Dim ovParameters As Variant

Const ROOTKEY = "SOFTWARE\MRC-GPRF\Study Recruitment"

If (VBA.Command = "") Then Exit Sub
If (VBA.InStr(VBA.Command, "/") = 0) Then Exit Sub

On Error GoTo ErrorCase:

'Get parameters (first will be "")...
ovParameters = VBA.Split(VBA.Command, "/")
For i = 0 To UBound(ovParameters)
ovParameters(i) = VBA.Trim(ovParameters(i))
Next

'Registry wrapper...
Set ooRegistry = New CRegistry

'If not present will return zero...
olDatabases = ooRegistry.GetSetting(regLocalMachine, ROOTKEY, "Databases", 0)

'Install...
If (ovParameters(1) = "i") Then

If (UBound(ovParameters) = 1) Then
'No value for DataRoot (error)...
ElseIf (olDatabases = 0) Then
'Not present or zero value...
ooRegistry.SaveSetting regLocalMachine, ROOTKEY, "Databases", 1
ooRegistry.SaveSetting regLocalMachine, ROOTKEY, "DataRoot", ovParameters(2)
Else
'Another database present...
ooRegistry.SaveSetting regLocalMachine, ROOTKEY, "Databases", (olDatabases + 1)
ooRegistry.SaveSetting regLocalMachine, ROOTKEY, "DataRoot", ovParameters(2)
End If

'Uninstall...
ElseIf (ovParameters(1) = "u") Then

If (olDatabases = 0) Then
'Error condition...
ElseIf (olDatabases = 1) Then
'Last database...
ooRegistry.DeleteSetting regLocalMachine, ROOTKEY, "Databases"
ooRegistry.DeleteSetting regLocalMachine, ROOTKEY, "DataRoot"
Else
'Decrement count...
ooRegistry.SaveSetting regLocalMachine, ROOTKEY, "Databases", (olDatabases - 1)
End If

End If

ProcFail:
Exit Sub

ErrorCase:
Resume ProcFail:

End Sub

'=====================================================================================
'END OF MODULE
'=====================================================================================
amalia

Hi Pete

1.
- An installed custom action is an exe/dll/vbs/js file from your package and it will be installed on the target machine;
- An attached custom action is a file - not part of your package - whose only purpose is to be a custom action. It will not be installed with your application.

2. What you see in the left pane are internal custom action identifiers and you should not be concerned about them. They must be unique and are automatically generated.

Regards
Amalia
rkcinchi
Posts: 2
Joined: Tue Mar 23, 2004 4:46 pm

Question on VB code you posted

Pete,

You posted a set of VB Code to write your registry entries. You referenced a "CRegsitry" class. Where did that class come from? Does it ship with VB or is it a custom class you guys wrote?

Thanks!

Kendall

Return to “Common Problems”