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
Archive: Create Registry Value, but only if it doesn't exist
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
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.
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?
ClearErrors first, IfErrors after.
-Stu
Thanks.
So I have made something like this:
Var myvalue
ClearErrors
myvalue = ReadRegDword ...
${If} $myvalue == 0
IfErrors 0 +3
WriteRegDWORD ...
WriteRegDWORD ...
${EndiF}
You can do ${If} ${Errors} with LogicLib too.
${If} $myvalue == 0
${AndIf} ${Errors}
WriteRegDWORD ...
WriteRegDWORD ...
${EndIf}
-Stu
I have problem with declaring variable
I got error: "Invalid command: Var" on line:
Var myvalue
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
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 ...
There shoud be:
StrCmp $0 "" 0 +4
instead of:
StrCmp $0 0 0 +4
And it works now.
Thanks Afrow.
It might be wise to do the IfErrors check before the StrCmp, although it probably won't matter, but it's good practice.
-Stu
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 ...