Archive: Writing and reading from the same reg key


Writing and reading from the same reg key
Hello,

I haven't found a thread that describes the same issue that I'm having, therefor I will try to detail my problem as much as I can.

I've created an installer which will read a certain registry key and, after "parsing" it with StrCpy, it will write it in another registry key. When the installer ends (.onGUIEnd) that new created registry key will be deleted; so it will act as a temporary registry key.

The problem that I've encounter is that the installer doesn't read the new written registry key.

The following code will write the temporary registry key without any problem.

Var $one

Function test
ReadRegStr $one HKLM SOFTWARE\Program\Nice InstallPath
!define something $one\install_here
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath" "${something}"
FunctionEnd


Now, if I want to get that key and make it the installation directory, it won't be able to read it.

InstallDirRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath"


The above code-line will only work if the installer is reopened and the temporarily registry key is not deleted upon closing.

My question is: How can I write and read the same registry key in the same installation process?

Looking forward to your reply.

As far as I know, the InstallDirRegKey is read before the installer even starts. It's like an automatic "ReadRegStr $INSTDIR Hive Path Key" command in .onInit. What you'll need to do is do it manually, after the key is created. (But seriously, why on earth would you want to do that? Just StrCpy $INSTDIR "${something}" would suffice...)


Hello,

Thank you for the fast reply.

At first I tried that but unfortunately, by the time I ended up in the Directory Page, the $INSTDIR was empty. I tried using global variables and even !define to get that key stored after ReadRegStr but every time the Function .... FunctionEnd passed that variable became "unset"


I'm pretty sure there's absolutely no problem with reading a regstr you created in earlier. If your $INSTDIR is empty, there's a problem in your script. Use MessageBox etc to find the problem (specifically, check the location of the regstr you're telling it to read/write).

Also: ALL variables in NSIS are global. There are no local variables.


!define is a compiling command, whether use it or not is the same.
You need to find out the origin of your problem.


Hello,

Thank you for the replies.

I've tried creating a simple example to show the issue I'm having. Testing it will have the same result.

!include MUI2.nsh

OutFile "Test.exe"

!define MUI_PAGE_CUSTOMFUNCTION_SHOW test
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW CustomShow
!insertmacro MUI_PAGE_DIRECTORY
InstallDirRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath"

Var path
Var trim_path

Function test
ReadRegStr $path HKLM SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winamp DisplayIcon
StrCpy $trim_path $path 23
; MessageBox MB_OK $trim_path
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath" "$trim_path"
FunctionEnd

Function CustomShow
MessageBox MB_OK $trim_path
FunctionEnd

Function .onGUIEnd
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP"
FunctionEnd

Section .onInit
SectionEnd


When the installer is open, the key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP\TMPPath" exists but, regardless where I put the InstallDirRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath" line, it won't fill in the path.

PS: The messagebox shows the path correctly the problem is setting the InstallDir with that variable.

Like I said, you'll need to ReadRegStr MANUALLY. Don't use InstallDirRegKey.


Hello,

Ok, I changed it to ReadRegStr when the Directory Page is shown but nothing seems to be changed.

!include MUI2.nsh

OutFile "Test.exe"

!define MUI_PAGE_CUSTOMFUNCTION_SHOW test
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW CustomShow
!insertmacro MUI_PAGE_DIRECTORY
;InstallDirRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath"

Var path
Var trim_path

Function test
ReadRegStr $path HKLM SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winamp DisplayIcon
StrCpy $trim_path $path 23
; MessageBox MB_OK $trim_path
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath" "$trim_path"
FunctionEnd

Function CustomShow
ReadRegStr $INSTDIR HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP" "TMPPath"
MessageBox MB_OK $INSTDIR
FunctionEnd

Function .onGUIEnd
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Stuff TMP"
FunctionEnd

Section .onInit
SectionEnd

First of all, you DO have administrator access when you run this test installer, right? >__>

Other tips:
- Reading the written regstring is still entirely pointless. Just use the variable containing the path directly!
- $trim_path is unneeded, just use StrCpy $path $path 23. Saves one variable.
- Why trim the first 23 characters? If I installed winamp to c:\foobar, the trim result will be entirely different from when I installed to c:\Program Files (x86)\Multimedia\z0mglongpath\winamp...


Hello,

I will reply to your message in the same order:

0. Yes, I'm running as Administrator.

1. I've tried that as well (at the first time I've created the script) but it didn't worked so I guessed that I need to save it somewhere and read it back.

2. Point taken.

3. I've trimmed that because in the registry, under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winamp\DisplayIcon you will find something like "C:\Program Files\Winamp\winamp.exe,0". I've trim it to get a folder of an already installed application.

After getting that path I could easily modify it according to my needs adding somethig like "$already_installed_application\plugins" folder that can't be found in the registry or anywhere else.

I tried now something, based on your idea and I finally managed to get it to work. It seems that I need to set the $INSTDIR before the CUSTOMFUNCTION_SHOW. I guess my previous testing was made using it in that function.

Thank you for all your help and for bearing with me.


Why would you trim to 23 characters? That will only work when the path starts with C:\Program Files\Winamp in which case, what is the point of reading the path in the first place?

This is what you want:

!include FileFunc.nsh
Function .onInit
ReadRegStr $R0 HKLM SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winamp DisplayIcon
${GetParent} $R0 $INSTDIR
FunctionEnd
Stu

Hello,

Thank you for the idea, Stu.