Skip to content
⌘ NSIS Forum Archive

Create Registry Value, but only if it doesn't exist

12 posts

grzech#

Create Registry Value, but only if it doesn't exist

I want to write a DWORD into Windows Registry, but only if such value doesn't exist already. Unfortunately WriteRegDWORD will overwrite existing value if it exists.

How can I do this?

TIA
Comperio#
Try ReadRegDWORD
From the docs:
The error flag will be set and $x will be set to an empty string ("" which is 0) if the DWORD is not present.
So, if the return value from ReadRegDWORD is an empty string ("") and the error flag is set after reading, then you'll know the value does not exist and you can proceed.
grzech#
I have read documentation on ReadRegDWORD before, the point is I cannot find anything about "error flag" in the docs.
How do I check if it has been set?
grzech#
Thanks.
So I have made something like this:
Var myvalue
ClearErrors
myvalue = ReadRegDword ...
${If} $myvalue == 0
IfErrors 0 +3
WriteRegDWORD ...
WriteRegDWORD ...
${EndiF}
Is this correct?
Afrow UK#
You can do ${If} ${Errors} with LogicLib too.

${If} $myvalue == 0
${AndIf} ${Errors}
WriteRegDWORD ...
WriteRegDWORD ...
${EndIf}

-Stu
grzech#
I have problem with declaring variable

I got error: "Invalid command: Var" on line:
Var myvalue
?
Afrow UK#
Unless /GLOBAL is specified, you must use Var outside Sections and Functions.
You might as well just put Var instructions at the top of your script.

-Stu
grzech#
Ok, forget it I don't want to use any library.
So back to the drawing board:

ClearErrors
ReadRegDword $0 ...
StrCmp $0 0 0 +4 ;I would like something better here? How to compare DWORD?
IfErrors 0 +3
WriteRegDWORD ...
WriteRegDWORD ...
Is that Ok?
Afrow UK#
It might be wise to do the IfErrors check before the StrCmp, although it probably won't matter, but it's good practice.

-Stu
grzech#
Yes, you're right. I am not very familiar yet with this NSIS syntax so my code is far from being perfect. I'll change it to:


ClearErrors
ReadRegDword $0 ...
IfErrors 0 +4
StrCmp $0 "" 0 +3
WriteRegDWORD ...
WriteRegDWORD ...