Reputation: 10662
I've created an installer using the Wix framework.
After a user installs the software for the first time, they can customize features which then get saved in the registry for the next time the program is ran.
Here is the problem I am having:
The user's settings are being saved in the registry as they should by my program. When the user uninstalls the program, their settings are still saved in the registry (again, as they should). But, when the user goes to install the newer version of the software, the registry values get erased.
How can I prevent registry values from being erased on install?
Edit:
I am now trying to run a custom action script to copy and restore the registry. Here is my code for my script:
<CustomAction Id="SaveCmd" Directory="TempFolder" ExeCommand="[SystemFolder]ccbackup.bat" Execute="deferred" Impersonate="no" />
<CustomAction Id="RestoreCmd" Directory="TempFolder" ExeCommand="[SystemFolder]REG.exe RESTORE HKCU\Software\Company\Program[TempFolder]BkUp.hiv" Execute="deferred" Impersonate="no" />
<InstallExecuteSequence>
<Custom Action="SaveCmd" After="InstallInitialize" />
<Custom Action="RestoreCmd" Before="InstallFinalize" />
</InstallExecuteSequence>
The SaveCmd custom action runs a script (ccbackup.bat) that looks like the following so that I could get some debug info:
REG.exe SAVE HKCU\Software\Company\Program BkUp.hiv /Y
pause
The script ccbackup.bat will run and give me the error:
"The system was unable to find the specified registry key or value."
The problem is that the installer is running in an "Admin" context so it has no idea how to find where HKCU is located in the registries.
Does anyone know how I can find an account (and then subsequently the correct registries) under HKCU when I am in an "admin" context?
My next strategy I was going to try was to fetch the SID of the current user and then access their registry by searching through HKEY_USERS... this however is proving to be difficult.
Upvotes: 1
Views: 350
Reputation: 10662
There is a lot of stuff to learn about WiX... Here's how I got out of this pickle:
To elevate permissions for my custom action (if needed) I used this VB script:
Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\CopyRegScript.VBS") Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\CopyRegScript.VBS" & Chr(34), "", "runas", 1
Else
MsgBox "Script file CopyRegScript.VBS not found"
End If
This script will prompt the user to allow admin priviliges.
To write to the HKU registry, I needed to know the SID of a particular user. For that I used this script:
Private Function getSid()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set wshShell = CreateObject( "WScript.Shell" )
strDomain = wshShell.ExpandEnvironmentStrings("%ComputerName%")
Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & _strUsername & "',Domain='" & strDomain & "'")
getSID = objAccount.SID
End Function
Then it was simply a matter of copying the registry and after restoring the registry when the installer execute sequence had finished. I used Reg.exe command line functions to do this.
Note: I'm not advising that anyone use this technique. We had to use it because in the prior released version it was decided that on uninstall it would be necessary to wipe all custom settings. After releasing this, it was then decided that this was not in fact an acceptable solution, but this was too late...it had already been shipped to customers.
Upvotes: 1