In the previous topics you successfully created the launcher. However, what if a local installation is present in the target machine?
If we launch our new portable on a machine where MNK is installed, it will destroy the local installation’s registration data and file associations. To fix it, we should modify our script.
If you examine our script, the first modification to the registry we do is when we import RegInfo.reg. Doing so, if there’s a locally installed MNK, its registration data will be overwritten with our registration data found in RegInfo.reg.
To prevent this, we should first save the local installation’s registry key into a .reg file, and after the final clean-up we’ll import it back. To save a registry key, use this formula:
${registry::SaveKey} “HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}” “$EXEDIRportableLocalRegInfo.reg” “/G=1″ $R0
Sleep 500
This will save the registry key “HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}” to the file LocalRegInfo.reg in “C:Portable My Notes Keeperportable” directory.
We should add some milliseconds to ensure that the saving of this registry key will be successful (that’s why “Sleep 500″ is there).
Now examine RegClean.reg:
REGEDIT4
[-HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}]
[-HKEY_CLASSES_ROOT.mnk]
[-HKEY_CLASSES_ROOTmnkfile]
[-HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstallMy Notes Keeper_is1]
The first line removes the registration data. Because we will restore local registration data at the and of the script, it is not important here.
The two HKCR key is important because these are required for file associations. If we don’t save them, clicking on a .mnk file won’t open MNK. But let’s see if they are really present in the registry!
Launch “Portable My Notes Keeper.exe” and while it’s running, launch Registry Workshop. Paste this line into its Address bar (below the toolbar) and hit Enter:
HKEY_CLASSES_ROOT.mnk
The “Confirm Create Key” pops up which means there’s no such key in the registry. So I was wrong in topic 9 (“Modifying The Template”) when I wrote that MNK will create these keys when launching it even if it’s not installed.
Let’s test the next key, “HKEY_CLASSES_ROOTmnkfile”. The same thing happens as before, so there’s no such key in the registry. If we test the last key in RegClean.reg, we’ll find that it is not in the registry.
This means that we shouldn’t delete them from the registry when the program finishes because these keys are not in the registry. However, if they are in the registry, this could mean only one thing: these belongs to the local installation of MNK, and if we delete them then we destroy the local installation.
The local MNK will run even if these keys are deleted. However, deleting these will destroy file type associations and presumable you will not be able to uninstall the local MNK.
We have to change our RegClean.reg file to:
REGEDIT4
[-HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}]
Now it is clear that we should bother with only this key to keep the local installation of MNK intact.
The new NSIS script will look like this (I only quote the Section “Main” part here, changes are written in red):
Section “Main”
;—–Saving Local Registration Data——
${registry::SaveKey} “HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}” “$EXEDIRportableLocalRegInfo.reg” “/G=1″ $R0
Sleep 500
;—–Importing Regkeys——
${registry::RestoreKey} “$EXEDIRportableRegInfo.reg” $R0
Sleep 200
;—–Launching My Notes Keeper——
ExecWait “$EXEDIRAppdataMyNotesKeeper.exe”
;—–Cleaning up——
${registry::RestoreKey} “$EXEDIRportableRegClean.reg” $R0
Sleep 500
;—–Restoring Local Registration Data——
${registry::RestoreKey} “$EXEDIRportableLocalRegInfo.reg” $R0
SectionEnd
Let’s complicate the thing a little bit: what happens if there’s no local installation in the target PC but there’s a LocalRegInfo.reg file on the USB drive (from previous runs)?
If you look at the script part above, you can imagine what would happen: the launcher tries to save the registry key but it founds nothing, so exports nothing. LocalRegInfo.reg remains intact. Then it imports RegInfo.reg, runs the software and then cleans up with RegClean.reg. But after that, it restores LocalRegInfo.reg, so when you leave the computer, a registry key is left on it which wasn’t there before running Portable MNK. We do not want that, so we’ll modify the script again.
To my mind, the easiest way to prevent this is to delete LocalRegInfo.reg at the end of the script. If we do this, on next run there’ll be no LocalRegInfo.reg on the USB drive, so nothing will be restored (presuming there’s no local installation of MNK).
To delete a file with NSIS, use the following code:
Delete “$EXEDIRportableLocalRegInfo.reg”
We’ll have to add some milliseconds before this line to ensure that there’s enough time to restore LocalRegInfo.reg (if it exists).
Here is the complete and final script:
;—Definitions—-
!define SNAME “Portable My Notes Keeper”
;—-Includes—-
!include “Registry.nsh”
;—–Runtime switches—-
CRCCheck off
AutoCloseWindow True
SilentInstall silent
WindowIcon off
XPSTYLE on
;—–Set basic information—–
Name “${SNAME}”
Icon “${SNAME}.ico”
Caption “${SNAME} Launcher”
OutFile “..${SNAME}.exe”
;—–Version Information——
LoadLanguageFile “${NSISDIR}ContribLanguage filesEnglish.nlf”
VIProductVersion “0.1.0.0″
VIAddVersionKey /LANG=${LANG_ENGLISH} “ProductName” “Portable My Notes Keeper Launcher”
VIAddVersionKey /LANG=${LANG_ENGLISH} “LegalCopyright” “© tpr 2007″
VIAddVersionKey /LANG=${LANG_ENGLISH} “FileDescription” “My Notes Keeper”
VIAddVersionKey /LANG=${LANG_ENGLISH} “FileVersion” “0.1″
Section “Main”
;—–Saving Local Registration Data——
${registry::SaveKey} “HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}” “$EXEDIRportableLocalRegInfo.reg” “/G=1″ $R0
Sleep 500
;—–Importing Regkeys——
${registry::RestoreKey} “$EXEDIRportableRegInfo.reg” $R0
Sleep 200
;—–Launching My Notes Keeper——
ExecWait “$EXEDIRAppdataMyNotesKeeper.exe”
;—–Cleaning up——
${registry::RestoreKey} “$EXEDIRportableRegClean.reg” $R0
Sleep 500
;—–RestoringLocal Registration Data——
${registry::RestoreKey} “$EXEDIRportableLocalRegInfo.reg” $R0
Sleep 800
Delete “$EXEDIRportableLocalRegInfo.reg”
SectionEnd
Compile the launcher and the Portable MNK is ready.
Note: if you launch the new Portable MNK, you’ll see that no LocalReginfo.reg file is created. That is because NSIS will not save a non-existing registry key. To test if this part of the script is working, select the key (“HKEY_CLASSES_ROOTCLSID{B0CC8D3F-EF1A-4400-A5C7-1A2230AA0B8C}”) in Registry Workshop while Portable MNK is running and simply delete it. Now close Portable MNK. Go back to Registry Workshop and click on the Undo button on the toolbar (or hit Ctrl+Z). Now the registration info is again in the registry but MNK is not running. If you run Portable MNK again, it should save the registration data into the file LocalRegInfo.reg and when you close it, it will put this key back to the registry.
If you want, you may delete some files from the “Appdata” directory to make the size of your portable smaller. For example, deleting “unins000.exe”, “unins000.dat” and some other non-important txt files (licence.txt, Readme.txt, Tips.txt, Whatsnew.txt) won’t do any harm.

[...] 11. Keeping Local Installation 12. Testing, Testing, Testing… 13. Conclusion [...]