Settings In Registry

Actually, this topic will teach you nothing new. In the previous chapter we used the formula

${registry::SaveKey} “HKEY_CURRENT_USERSoftwareMy Notes KeeperThe My Notes Keeper” “$EXEDIRportableSettings.reg” “/G=1″ $R0

which saves the registry key “HKEY_CURRENT_USERSoftwareMy Notes KeeperThe My Notes Keeper”

to file “$EXEDIRportableSettings.reg” file.

We’ll use this formula to save the program’s settings stored in the registry to a file, and on next launch, we’ll write it back to the registry (importing it).

If you examine one of my templates, you should see the following code:


;—–Importing Regkeys——

${registry::RestoreKey} “$EXEDIRportableSettings.reg” $R0

Sleep 300

${registry::RestoreKey} “$EXEDIRportableRegInfo.reg” $R0

Sleep 200

${registry::RestoreKey} “$EXEDIRportableDirSettings.reg” $R0

Sleep 200

Here three registry file is imported: Settings.reg, RegInfo.reg and DirSettings.reg.

Settings.reg

this file contains settings that is stored in the registry

RegInfo.reg

this is only the registration info (user name and serial in most cases)

DirSettings.reg

this file is used to clear paths in registry

The order of these .reg files are important, you shouldn’t change it.

If you change some settings in the application, this change is stored in the registry (provided that the application stores its settings in the registry, of course). Because we save settings to Settings.reg from the registry, this file is always changing. So it shouldn’t contain registration data because it is constant.

That’s why we store registration info in RegInfo.reg. It is imported after Settings.reg to ensure that the data it writes to the reigstry is not overwritten by the data in Settings.reg. Otherwise you may loose your registration data which we really don’t want to.

The third one is DirSettings.reg. I use it to clear paths in the registry which must be overwritten with absolute paths.

For example, we want to import these path to the registry: “H:Data”. “H” is the drive letter of the usb drive, which changes every time you plug in your USB drive at another computer. Because the previous time we used our portable, in Settings.reg there’s “Y:Data”. “Y” was the USB drive’s letter on the previous computer.

Now, if we import Settings.reg, it writes “Y:Data” into the registry. But we want “H:Data” to be present in the registry when the main program starts. So, we’ll clear it DirSettings.reg first:

REGEDIT 4

[HKEY_CURRENT_USERSoftwareMy SoftwareSettings]

“Datadir”=-

Notice that red minus (-) sign after “Datadir” – that will clear the data of “Datadir” value. So before importing DirSettings.reg, “Datadir” looked like this:

“Datadir”=”H:Data”

It contains “H:Data” because our Settings.reg contains that from the previous run. After importing DirSettings.reg, it should look like:

“Datadir”=

So totally empty. Now we can write the absolute path to it with NSIS:

WriteRegStr HKEY_CURRENT_USER “SoftwareMy SoftwareSettings” “Datadir” “$EXEDIRAppdata”

Theoretically if you the launcher executes the code above, it overwrites the previous value. In some cases I found that although it should overwrite it, it didn’t, so I’m using this DirSettings.reg method instead.

Ok, now the settings, the registration info and the required absolute paths are in the registry, we can execute the main program with ExecWait. ExecWait executes the main program and waits until it finishes.

Then we should save settings and clean up.

To save settings, use this code:

${registry::SaveKey} “HKEY_CURRENT_USERSoftwareMy SoftwareSettings” “$EXEDIRportableSettings.reg” “/G=1″ $R0

You can see that if something was changed in this registry key (for example, you changed some settings of the software), the new Settings.reg will be different as it was before the portable software run. The old Settings.reg is overwritten with the new Settings.reg.

For deleting a registry key we’ll import RegClean.reg into the registry:

${registry::RestoreKey} “$EXEDIRportableRegClean.reg” $R0

Because RegClean.reg contains minus (-) signs in front of the registry key’s name, it will delete it:

REGEDIT 4

[-HKEY_CURRENT_USERSoftwareMy SoftwareSettings]

Notice that not the whole branch was deleted, only its “Settings” sub-branch.

Why don’t we delete the entire “HKEY_CURRENT_USERSoftwareMy Software” branch?

It is for security reasons. The branch “My Software” may store other programs’ settings also, and we don’t want to delete them. For example, Corel’s registry key looks like this:

HKEY_CURRENT_USERSoftwareCorel

HKEY_CURRENT_USERSoftwareCorelCorelDRAW11.0

HKEY_CURRENT_USERSoftwareCorelCorelDRAW9

If we delete “HKEY_CURRENT_USERSoftwareCorel” registry key, the settings of version 9 and version 11 will be gone. We’ll need NSIS to check if the registry key we want to delete is empty or not:

DeleteRegKey /ifempty HKEY_CURRENT_USER “SoftwareCorel”

Notice that “/ifempty” switch after DeleteRegKey (written in red). That tells the launcher that the given registry key can be deleted only if it is empty.

And that’s all about saving and restoring registry settings. Of course, these all are only general things and you should modify your code that suits best to the application you’re trying to portablize.

Because each software is different, you have to examine them thoroughly. You must be aware of how it works, should understand every settings of it and so on. If you’re not knowing the software very well, your portable might be unusable. I stated it earlier but I have to emphasize again that if a software runs without installation on every PC it is not sure that it is also portable. If you cannot use them or if it makes a dustbin from your PC, it is not really something that you should call as portable.

One response to “Settings In Registry”

  1. wilsont3ch.com » How To Make Portables

    [...] 4. Settings In Registry [...]