Archive: Newbie Help with RegKey


Newbie Help with RegKey
Hi I'm haveing a problem, I'm trying to detect a registry key to see if it exist, but what ever combination I try I can't get it to work

ReadRegStr $0 HKLM "Software\game\software" "Install Path"
???????????? continue
MessageBox MB_ICONINFORMATION|MB_OK "Registry path can not be found"
Abort
continue:


All I would like it to do is if the registry key exist to continue, if it doesn't then I would like it to abort, but I just don't know what the lines should be, I have try looking through the archive and docs and the forum, but what ever combination I do it won't work

I know it has to be in the Function .oninit, but I ca'nt cet it to work.

I have try the Archive IfKeyExists Macro, but all I get is

IfKeyExists not inuse or something like that

I would be gratefull for your help

Thank you


IfKeyExists from the archive is the way to go if you want to check if the key exists. But as it seems, all you need is to know if the value inside it is valid. For that, simply compare it to "" using StrCmp. You can also use InstallDirRegKey.


This seems to work, could someone confirm that it's correct and I'm right please

Thank you

Function .onInit
ReadRegStr $0 HKLM "SOFTWARE\game\software" "Install Path"
StrCmp $0 "" 0 continue
MessageBox MB_ICONINFORMATION|MB_OK "Registry path can not be found"
Abort
continue:
FunctionEnd


Yes, that's a good way. But if you want to use $0 later to fill $INSTDIR you should use InstallDirRegKey and check if $INSTDIR is empty in .onInit. It will also make sure the installation path is valid and not just not empty.


Ok I have this

Oh I should add that this is a upgrade for some files and not a exe application

InstallDir "$INSTDIR"
InstallDirRegKey HKLM "Software\game\software" "Install Path"

but how would I put $INSTDIR into the .oninit ?

you see I don't want it to be installed to anyother DIR if the Reg key doesn't exist.

I have read the InstallDirRegKey as you stated but I don't quite understand it


I'm sorry, I've mixed up. InstallDirRegKey checks if it's empty for you:

If this attribute is present, it will override the InstallDir attribute if the registry key is valid, otherwise it will fall back to the InstallDir default.

so is my little bit of script fine then?


No. InstallDir sets $INSTDIR, you can't tell it to set it to its self. You must provide a default or provide nothing. If you want to abort the installation if that regkey doesn't exist just use this:

InstallDirRegKey HKLM "Software\game\software" "Install Path"
Function .onInit
StrCmp $INSTDIR "" 0 ok
MessageBox MB_OK "error..."
Abort
ok:
FunctionEnd


so what if I put

Var GAMEDIR

Section "-InstallDir"
ReadRegStr $GAMEDIR HKLM "Software\game\software"
StrCpy $INSTDIR "$GAMEDIR"
SectionEnd

InstallDir "$INSTDIR"
InstallDirRegKey HKLM "Software\game\software" "Install Path"

and then

Function .onInit
StrCmp $INSTDIR "" 0 ok
MessageBox MB_OK "error..."
Abort
ok:
FunctionEnd

will this work


Why would you want to do that? Just use the script I gave you.

This line means nothing:
InstallDir "$INSTDIR"

This line is missing a parameter:
ReadRegStr $GAMEDIR HKLM "Software\game\software"

If you don't use InstallDir, InstallDirRegKey will put the value from the registry and if it's not there or the value is invalid, it'll put nothing in it. That's what you should use, my script.


ok then thanks, won't bother you again :)


Sorry about this I know I said I wouldn't bother you again,

so what would happen if I removed the line InstallDir

is InstallDirRegKey used instead?

Or do I have to have InstallDir line in the script

I only ask because if I have InstallDir "$PROGRAMFILES\game" it overrides the InstallDirRegKey and the Function .oninit script you supplied doesn't check if the install path is valid, if I delete the Regkey from the registry is will still install, which I don't want it to do.

or could I put InstallDir ""

if not what do I put so it will not override InstallDirRegKey?

Sorry but it can be confusing and my apologise as you have been very patient so far.


Hi just wondered if anyone could answer my question


As the quote said InstallDirRegKey overrides InstallDir if the key exists and contains a valid path. If you have InstallDir in your script it will fall back to whatever you've defined using InstallDir instead to "" as you wish.

You should simply not use InstallDir.


Thank you