10

I need to check, if a registry value exists. How can I do that?

My first approach:

ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:"
        ${IF} $0 == ""
              MESSAGEBOX MB_OK "NUL exists"
        ${ELSE}
               WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" ""
        ${ENDIF}

But this also works, when the value doesn’t exist. I guess, because "doesn’t exist" and empty string are handled the same way.

With Registry.nsh I did it like this:

${registry::Read} "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" $var1 $var2

        ${IF} $var2 == "REG_SZ"

But I get an error, because the Pop ${_STRING} in the registry.nsh doesn’t work.

Help and suggestions welcome!

user3629892
  • 2,960
  • 9
  • 33
  • 64

1 Answers1

15

You should check the error flag after reading:

ClearErrors
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:"
${If} ${Errors}
  MessageBox MB_OK "Value not found"
${Else}
  ${IF} $0 == ""
              MESSAGEBOX MB_OK "NUL exists and it's empty"
        ${ELSE}
               WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" ""
        ${ENDIF}
${EndIf}

Also, you may be interested in EnumRegValue before trying to read it.

Francisco R
  • 4,032
  • 1
  • 22
  • 37
  • Is there any way to write NSIS script in language like python instead of this C/VBScript combo? – ed22 Apr 27 '18 at 12:57
  • 1
    @ed22 do you realize that your question sounds like: "is there a way to write in English but in Chinese?". In that sense a "script" and a "language" are interchangeable notions and NSIS script is what it is and nothing else. I'm afraid you'd either have to learn the script or invent your own installer platform that uses a python-like scripting – YePhIcK Jan 27 '19 at 15:39
  • 1
    @YePhIcK I was just asking if any other languages are supported. I wouldn't choose Chinese for the job though. – ed22 Jan 31 '19 at 13:45