z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Can't backup an empty registry key!

Hello,

When use the following sample script in help file of AI to backup an empty registry key without any of the contents under this registry key, the CustomAction of backup was interrupted. Lead to the other keys can't be backuped.

How to modify the script? Please help me.

Execution Options: Deferred or Immediate execution
Action Sequence: Install

And this script is invalid when using Execution Options of "Deferred with no impersonation".
On Error Resume Next

'set the registry hive used in the custom action
Const HKEY_LOCAL_MACHINE = &H80000002

'set the main registry keys
strComputer = "."
command = Session.Property("CustomActionData")
tokens = Split(command,"|")
source = tokens(0)
target = tokens(1)

'create an object which handles the registry operations
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

'create the backup key
objReg.CreateKey HKEY_LOCAL_MACHINE, target

'call the function which copies everything from the target key
CopyKey HKEY_LOCAL_MACHINE, source, target

'the copy function
Sub CopyKey(HKEY_LOCAL_MACHINE, source, target)

'enumerate all subkeys of the main key
objReg.EnumKey HKEY_LOCAL_MACHINE, source, SubKeysList

'for each subkey call the copy function
If IsArray(SubKeysList) Then
For Each SubKey In SubKeysList
'create the subkey in the backup key before calling the copy function for it
objReg.CreateKey HKEY_LOCAL_MACHINE, target & "\" & SubKey
CopyKey HKEY_LOCAL_MACHINE, source & "\" & SubKey, target & "\" & SubKey
Next
End If

'enumerate the values in the current key
objReg.EnumValues HKEY_LOCAL_MACHINE, source, ValueList, ValueTypes

'copy each value to the corresponding backup key
For i = 0 To UBound(ValueList)
objReg.GetStringValue HKEY_LOCAL_MACHINE, source, ValueList(i), Value
objReg.SetStringValue HKEY_LOCAL_MACHINE, target, ValueList(i), Value
Next
End Sub
regards,
Zhang Shangyi
Last edited by z_shangyi on Wed Nov 19, 2008 7:30 pm, edited 10 times in total.
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Can't backup a empty registry key!

Hi,

Please try to offer more details in your posts. Can you please give me more details about what you need to do? Also, from where did you get this code?

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: Cann't backup an empty registry key!

Cosmin,
Thank you for your reply so quickly. I was writing it. Details such as the above.
Zhang Shangyi
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Cann't backup an empty registry key!

Hi,

The sample custom action in the User Guide was tested and it can backup empty registry keys. Perhaps the problem is caused by something else. Can you please send us a log of the installation with the problem and the AIP file you are using to support at advancedinstaller dot com so we can investigate it?

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: Can't backup an empty registry key!

Zhank you!

OK, Please download this RAR compression packages that contains AIP file, Installation log file, Reg files and VBS files at: http://www.wanfutrade.com/software/Passolo2007.rar. It contains some simplified Chinese characters!

This installation package, at http://www.wanfutrade.com/software/HA_P ... ll_zsy.exe(25MB)

Zhang Shangyi
Last edited by z_shangyi on Wed Nov 19, 2008 7:23 pm, edited 2 times in total.
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: Can't backup an empty registry key!

I am sorry. If you already have downloaded, then Please download Passolo2007.rar file again.

Zhang Shangyi
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Can't backup an empty registry key!

Hi,

In the log you sent I noticed that the custom action which performs the backup is returning 0. This means that it finishes successfully. Perhaps the problem is caused by something else. What is the exact behavior you are encountering? Do you receive any error messages during the installation? If so, please send us a log in English (created on a machine which uses an English Windows with an English locale) so we can investigate it.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: Can't backup an empty registry key!

Hello,
I have already solved this problem, use the following script. The script in help of AI is wrong!
But it can not be integrated with "remove" script!
' -----------------------------------------------------------------------------
' @info BackupKey script
' -----------------------------------------------------------------------------
'Function BackupKey

On Error Resume Next

Dim inputArray, token
'Dim sourceKeyList, sourceArray, sourceKey

'set the registry hive used in the custom action
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006
Const REG_NONE = 0
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_DWORD_LITTLE_ENDIAN = 4
Const REG_DWORD_BIG_ENDIAN = 5
Const REG_LINK = 6
Const REG_MULTI_SZ = 7
Const REG_RESOURCE_LIST = 8
Const REG_FULL_RESOURCE_DESCRIPTOR = 9
Const REG_RESOURCE_REQUIREMENTS_LIST = 10

'create an object which handles the registry operations
strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

'set the main registry keys
targetKey = ""
sourceKeyList = ""

custActData = Session.Property("CustomActionData")
If Len(custActData) = 0 Then
ExitFunction
End If
WriteToLog("[BackupKey]: CustomActionData=" & custActData)

' Target<>SourceList
inputArray = Split(custActData, "<>", -1)

i = 0
For Each token In inputArray
If i = 0 Then
targetKey = Trim(token)
ElseIf i = 1 Then
sourceKeyList = Trim(token)
End If
i = i + 1
Next

If Len(targetKey) = 0 Then
ExitFunction
End If
WriteToLog("[BackupKey]: Target Key: " & targetKey)

If Len(sourceKeyList) = 0 Then
ExitFunction
End If
WriteToLog("[BackupKey]: Source Key list: " & sourceKeyList)

MainTargetKey = TrailingBackslash(targetKey)
Maintarget = Right(targetKey, Len(targetKey) - InStr(targetKey,"\"))

If MainTargetKey = "" Then
ExitFunction
ElseIf Len(Maintarget) = 0 Then
ExitFunction
End If

'create the backup key
objReg.CreateKey MainTargetKey, Maintarget

' This array contains the full source Keys
sourceArray = Split(sourceKeyList, "|", -1)

For Each sourceKey In sourceArray
If Len(sourceKey) <> 0 Then
MainSourceKey = TrailingBackslash(sourceKey)
source = Right(sourceKey, Len(sourceKey) - InStr(sourceKey,"\"))
target = Maintarget & "\" & Right(sourceKey, Len(sourceKey) - InStrRev(sourceKey,"\"))

WriteToLog("[BackupKey]: Main Source key: " & Chr(34) & MainSourceKey & Chr(34))
WriteToLog("[BackupKey]: Source key: " & Chr(34) & source & Chr(34))
WriteToLog("[BackupKey]: Main Target key: " & Chr(34) & MainTargetKey & Chr(34))
WriteToLog("[BackupKey]: Target key: " & Chr(34) & target & Chr(34))

'create the backup key
objReg.CreateKey MainTargetKey, target

'call the function which copies everything from the target key
CopyKey MainSourceKey, source, MainTargetKey, target
End If
Next

ExitFunction
'Exit Function

' -----------------------------------------------------------------------------
'the copy function
' -----------------------------------------------------------------------------
Sub CopyKey(MainSourceKey, source, MainTargetKey, target)

'enumerate all subkeys of the main key
objReg.EnumKey MainSourceKey, source, SubKeysList

'for each subkey call the copy function
If IsArray(SubKeysList) Then
For Each SubKey In SubKeysList
If Len(SubKey) <> 0 Then
'create the subkey in the backup key before calling the copy function for it
objReg.CreateKey MainTargetKey, target & "\" & SubKey
End If
CopyKey MainSourceKey, source & "\" & SubKey, MainTargetKey, target & "\" & SubKey
Next
End If

'enumerate the values in the current key
objReg.EnumValues MainSourceKey, source, ValueList, ValueTypes

'copy each value to the corresponding backup key
If IsArray(ValueList) Then
For i = 0 To UBound(ValueList)
If Len(ValueList(i)) <> 0 Then
Select Case ValueTypes(i)
Case REG_SZ
objReg.GetStringValue MainSourceKey, source, ValueList(i), Value
objReg.SetStringValue MainTargetKey, target, ValueList(i), Value
Case REG_MULTI_SZ
objReg.GetMultiStringValue MainSourceKey, source, ValueList(i), Value
objReg.SetMultiStringValue MainTargetKey, target, ValueList(i), Value
Case REG_EXPAND_SZ
objReg.GetExpandedStringValue MainSourceKey, source, ValueList(i), Value
objReg.SetExpandedStringValue MainTargetKey, target, ValueList(i), Value
Case REG_DWORD
objReg.GetDWORDValue MainSourceKey, source, ValueList(i), Value
objReg.SetDWORDValue MainTargetKey, target, ValueList(i), Value
'objReg.GetQuadWordValue MainSourceKey, source, ValueList(i), Value
'objReg.SetQuadWordValue MainTargetKey, target, ValueList(i), Value
Case REG_BINARY
objReg.GetBinaryValue MainSourceKey, source, ValueList(i), Value
objReg.SetBinaryValue MainTargetKey, target, ValueList(i),Value
End Select
End If
Next
End If
End Sub

' -----------------------------------------------------------------------------
' @info Removes the trailing backslash \ (if there is one)
' -----------------------------------------------------------------------------
Function TrailingBackslash(KeyPath)
TrailingBackslash = ""
If Left(KeyPath, InStr(KeyPath,"\")-1) = "HKCR" Then
TrailingBackslash = HKEY_CLASSES_ROOT
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_CLASSES_ROOT" Then
TrailingBackslash = HKEY_CLASSES_ROOT
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKCU" Then
TrailingBackslash = HKEY_CURRENT_USER
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_CURRENT_USER" Then
TrailingBackslash = HKEY_CURRENT_USER
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKLM" Then
TrailingBackslash = HKEY_LOCAL_MACHINE
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_LOCAL_MACHINE" Then
TrailingBackslash = HKEY_LOCAL_MACHINE
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKUR" Then
TrailingBackslash = HKEY_USERS
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_USERS" Then
TrailingBackslash = HKEY_USERS
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKCC" Then
TrailingBackslash = HKEY_CURRENT_CONFIG
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_CURRENT_CONFIG" Then
TrailingBackslash = HKEY_CURRENT_CONFIG
End If
End Function

' -----------------------------------------------------------------------------
' @info Writes the specified string in the Windows Installer log file
' -----------------------------------------------------------------------------
Function WriteToLog(msg)
Const msiMessageTypeInfo = &H04000000
Set record = Session.Installer.CreateRecord(1)
record.StringData(0) = "[1]"
record.StringData(1) = CStr(msg)
Session.Message msiMessageTypeInfo, record
End Function

' -----------------------------------------------------------------------------
' @info to exit run of script
' -----------------------------------------------------------------------------
Sub ExitFunction()
End Sub
Zhang Shangyi
Last edited by z_shangyi on Sun Dec 07, 2008 3:02 am, edited 6 times in total.
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: Can't backup an empty registry key!

a Remove Key script.
' -----------------------------------------------------------------------------
' @info RemoveKey script
' -----------------------------------------------------------------------------
'Function RemoveKey

On Error Resume Next

'set the registry hive used in the custom action
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

'create an object which handles the registry operations
strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

'set the main registry keys
DelKey = ""
custActData = Session.Property("CustomActionData")
If Len(custActData) = 0 Then
ExitFunction
End If
WriteToLog("[DeleteKey]: CustomActionData=" & custActData)

' This array contains the full Delele Keys
DelKeyArray = Split(custActData, "|", -1)

For Each toDelKey In DelKeyArray
toDelkey = Trim(toDelKey)
If Len(toDelKey) <> 0 Then
MainDelKey = TrailingBackslash(toDelKey)
DelKey = Right(toDelKey, Len(toDelKey) - InStr(toDelKey,"\"))

If MainDelKey = "" Then
ExitFunction
ElseIf Len(DelKey) = 0 Then
ExitFunction
End If

WriteToLog("[DeleteKey]: Main delete key: " & Chr(34) & MainDelKey & Chr(34))
WriteToLog("[DeleteKey]: Delete key: " & Chr(34) & DelKey & Chr(34))

'call the function which deletes the key
DeleteKey MainDelKey, DelKey
End If
Next

ExitFunction
'Exit Function

' -----------------------------------------------------------------------------
' the delete function
' -----------------------------------------------------------------------------
Sub DeleteKey(MainDelKey, DelKey)
'enumerate all subkeys of the main key
objReg.EnumKey MainDelKey, DelKey, SubKeysList

'for each subkey call the delete function
If IsArray(SubKeysList) Then
For Each SubKey In SubKeysList
DeleteKey MainDelKey, DelKey & "\" & SubKey
Next
End If

'delete the current key
objReg.DeleteKey MainDelKey, DelKey
End Sub

' -----------------------------------------------------------------------------
' @info Removes the trailing backslash \ (if there is one)
' -----------------------------------------------------------------------------
Function TrailingBackslash(KeyPath)
TrailingBackslash = ""
If Left(KeyPath, InStr(KeyPath,"\")-1) = "HKCR" Then
TrailingBackslash = HKEY_CLASSES_ROOT
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_CLASSES_ROOT" Then
TrailingBackslash = HKEY_CLASSES_ROOT
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKCU" Then
TrailingBackslash = HKEY_CURRENT_USER
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_CURRENT_USER" Then
TrailingBackslash = HKEY_CURRENT_USER
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKLM" Then
TrailingBackslash = HKEY_LOCAL_MACHINE
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_LOCAL_MACHINE" Then
TrailingBackslash = HKEY_LOCAL_MACHINE
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKUR" Then
TrailingBackslash = HKEY_USERS
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_USERS" Then
TrailingBackslash = HKEY_USERS
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKCC" Then
TrailingBackslash = HKEY_CURRENT_CONFIG
ElseIf Left(KeyPath, InStr(KeyPath,"\")-1) = "HKEY_CURRENT_CONFIG" Then
TrailingBackslash = HKEY_CURRENT_CONFIG
End If
End Function

' -----------------------------------------------------------------------------
' @info Writes the specified string in the Windows Installer log file
' -----------------------------------------------------------------------------
Function WriteToLog(msg)
Const msiMessageTypeInfo = &H04000000
Set record = Session.Installer.CreateRecord(1)
record.StringData(0) = "[1]"
record.StringData(1) = CStr(msg)
Session.Message msiMessageTypeInfo, record
End Function

' -----------------------------------------------------------------------------
' @info to exit run of script
' -----------------------------------------------------------------------------
Sub ExitFunction()
End Sub
Zhang Shangyi
Last edited by z_shangyi on Sun Dec 07, 2008 2:59 am, edited 1 time in total.
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Can't backup an empty registry key!

Hi,

The sample custom action in the User Guide was tested and on our machines it can backup an empty registry key. I'm not sure why it didn't work for you.

Your custom actions seem to use the same code as we did, except they have been modified to use all registry hives and they write to a log. Please note that the sample custom action uses the HKEY_LOCAL_MACHINE hive and it needs to be modified if used for other hives.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”