Service restarting before preserved registry values are restored - Advanced Installer Community
jbothe
Posts: 13
Joined: Tue Mar 31, 2020 11:53 am

Service restarting before preserved registry values are restored

Hi,

Our installer project is almost complete, with auto-updates! We have one last problem.

We are installing a service along with some registry keys (to store configuration for this service). When upgrading, we are preserving some of our registry keys so they don't get overwritten or destroyed, like this:

Image

It appears that on update, these registry keys are preserved, but our service is restarting and reading the default config from the registry. It's like service is restarting too early and reading the registry before the existing values are restored. We are restarting our service like this:

Image

Is there a way of restarting the service after the registry values have been restored during an upgrade?

Thanks,
John
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Service restarting before preserved registry values are restored

Hello John,

Just to make sure I fully understand your scenario here:

You have a service that starts using a set of arguments. At install time, you write these arguments as registry entries, which you try to preserve during upgrade.

At upgrade time, you provide new values (for the arguments) and you want the service to start with the new arguments, but in reality it starts with the ones from the first time install?

If this is the case, then it is quite strange, as the action that starts the service happens after the action that writes the registry entries.
order.png
order.png (135.5 KiB) Viewed 4392 times

Unfortunately, I can not say why this happens. In order for me to further investigate this issue, please forward me the following resources:

- a copy of the older version of the .AIP file

- a copy of the newer version of the .AIP file

- a download link for the older version of the setup

- a download link for the newer version of the setup

- a test case which I can follow in order to reproduce the issue (also, details about, for instance, the arguments used are also welcome)

by e-mail at support at advancedinstaller dot com.

Also, please let me know if what I've described above is the scenario you are in. If not, please give me some more details (maybe exemplify) about what you are trying to achieve.

Looking forward to hearing from you.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jbothe
Posts: 13
Joined: Tue Mar 31, 2020 11:53 am

Re: Service restarting before preserved registry values are restored

Hi Catalin,

Thanks for getting back to me... you almost summarised the problem exactly, here's specifically what is happening:
  • Installer ships with a property called URL, this has a default value, say api.example.com
  • This URL property is written to the registry at install time.
  • Service is started, reads this config and connects to this URL.
  • The service can also be configured to use a different URL, the value in the registry is changed to api2.example.com and the service uses this URL now.
  • On upgrade, this registry value is preserved, the service is upgraded and restarted.
  • However, we fond that the service connects to api.example.com not api2.example.com - the only place the URL information comes from is the registry, and the registry contains api2.example.com. It feels like for a brief moment, api.example.com was put in the registry since it's the default value for the property.
Should we consider performing a search and populating the installer property with the existing value from the registry, if it exists?

I'll attempt to send all this information you requested.

Thanks,
John
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Service restarting before preserved registry values are restored

Hello John,

First of all, thank you for the explanation.

I believe your scenario is clear to me now. What happens here makes sense, please allow me to explain why.

Basically, the preserve option for your registry key works as it follows:

- first, the registry key is saved into a .reg file

- at the end of the installation, this reg file is installed (restoring the values)

Unfortunately, this happens after the service is started.

Normally, this would be as simple as moving the custom action that does the restore operation before the service is starting. However, this is not possible as the custom action that does this is of "Immediate" type, while the "StartService" happens during the "Deferred" stage.

This means that we will have to do this ourselves, through a custom action. Here is how this can be done:

1. create a search that saves the value of the registry entry
2. create a custom action that modifies the registry entry, using as a value the value of the search

Let's dive a bit into this:

1. the "Search" can be added in the "Search" page. After doing so, you can right click on the search --> "Add search location" --> "Registry".

2. After configuring the search, it's time to create the custom action.

The custom action should be added after the "WriteRegistryValue" and before the "InstallService" standard actions. To show these actions in the UI, please proceed as it follows:

- go to "Custom Actions" page

- under "Install Execution Stage", click on any action group --> "Show standard actions" --> "Add resources" --> "WriteRegistryValues"

- now please repeat the above step for the "InstallService" standard action as well

To schedule your custom action between those actions specified above, simply drag and drop it in the appropriate place.

Important:

- the custom action should be running during the deferred stage. To do so, select your custom action and set its "Execution Time" to "When the system is being modified (deferred)"

- the custom action should have enough privileges to modify the registry entry

- make sure the custom action will run in the appropriate scenarios using the "Execution Stage Condition" section

Now, another really important thing I would like you to take into consideration is the redirection that happens in the "backstage" of Windows.

Let's consider the following:

- x86 OS & x64 OS

- x86 process & x64 process

When a 32-bit process runs on a 64-bit computer, the modifications it brings to the computer will most likely be redirected. For instance, if it tries to modify the registry, the OS will automatically redirect whatever modifications it does to the WOW6432Node key.

For instance, if it tries to add the following key:

Code: Select all

HKLM:\Software\MyKey
it will actually be added like this:

Code: Select all

HKLM:\Software\WOW6432Node\MyKey
Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Service restarting before preserved registry values are restored

As a followup to my last thread:

Since I was able to reproduce this (and also because I did not notice your e-mail), I did not download your resources.

I have now downloaded the .AIP file and I could've seen that your setup is a 64-bit package.

With this in mind, please ignore what I've said above about the registry redirection.

The fact that your package is of 64-bit actually eases the things a lot.

Let's consider the following scenario:

- at install time, we write a registry written through an edit box control

- the registry we write has the "sss" value

- after installation, we modify its value to "ccc"

- at upgrade time, before the "InstallService" standard action takes place, we want the registry to have the "ccc" value

Here are some screenshots which hopefully you will find helpful:

regEntry.png
regEntry.png (35.48 KiB) Viewed 4382 times
search.png
search.png (28.03 KiB) Viewed 4382 times
ca.png
ca.png (148.16 KiB) Viewed 4382 times
In the last screenshot, just replace the "ccc" value with $oldVal.


Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jbothe
Posts: 13
Joined: Tue Mar 31, 2020 11:53 am

Re: Service restarting before preserved registry values are restored

Hi Catalin,

Thanks for the reply.

What about if instead of the powershell script, we just use a "set property" custom action to conditionally assign the value of the registry property (from the search result, if it exists) to the original property that gets written to the registry anyway (before WriteRegistryValues)?

Image

- at install time, we write a registry written through an edit box control - the property written is called SERVER

- the registry we write has the "sss" value

- after installation, we modify its value to "ccc"

- at upgrade time, before the "WriteRegistryValues" standard action takes place, we want the SERVER property to have the "ccc" value assigned to it and it gets written as usual by WriteRegistryValues - the registry search reads the value into SERVER_REG, the "set property" custom action sets SERVER to SERVER_REG only if SERVER_REG contains something.

Will this work?

If the registry value is not found, will the SERVER_REG property be null or an empty string? Is SERVER_REG <> "" the correct test?

Does the search need to happen in both the dialog and install stages? We already skip the custom dialog that asks for this information if SERVER is not empty. I guess the dialog could be skipped if SERVER <> "" OR SERVER_REG <> "" - we will only ask for user input if both are empty. Yes, so just ignore the SetProperty in the dialog stage.

Thanks,
John
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Service restarting before preserved registry values are restored

Hello John,
What about if instead of the powershell script, we just use a "set property" custom action to conditionally assign the value of the registry property (from the search result, if it exists) to the original property that gets written to the registry anyway (before WriteRegistryValues)?
To be fully honest with you, this indeed sounds like a good ideea. However, as I have not tested this yet, I would suggest testing it and seeing if everything works as expected.
If the registry value is not found, will the SERVER_REG property be null or an empty string?
Indeed, the property will be an empty string/null. Basically, the property will not exist.

Code: Select all

Does the search need to happen in both the dialog and install stages?
No. As long as the property of your search is a public property, everything should be fine.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”