NSIS - check if registry key value exists

I need to check if a registry value exists. How can i do this?

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 it also works when the value does not exist. I think because "doesnt exist" and the empty string are treated the same way.

With Registry.nsh, I did this as follows:

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

But I get an error because Pop $ {_ STRING} in registry.nsh does not work.

Help and suggestions are welcome!

+5
source share
1 answer

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 empty" ${ELSE} WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" "" ${ENDIF} ${EndIf} 

You might also be interested in EnumRegValue before trying to read it.

+9
source

Source: https://habr.com/ru/post/1210665/


All Articles