Skip to content
⌘ NSIS Forum Archive

Trying to verify an environment variable exists

5 posts

toqer#

Trying to verify an environment variable exists

Hello,

I cobbled this together from various sources I found. I'm trying to read the registry to find out if JAVA_HOME is defined, and if it is pop up the value. This is my code.

; Script generated by the HM NIS Edit Script Wizard.
; Strcontains addded by rcortese
; StrContains
; This function does a case sensitive searches for an occurrence of a substring in a string. 
; It returns the substring if it is found. 
; Otherwise it returns null(""). 
; Written by kenglish_hi
; Adapted from StrReplace written by dandaman32
; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "TN1 Installer"
!define PRODUCT_VERSION "10.6.5"
!define PRODUCT_PUBLISHER "Boardvantage"
 
SetCompressor lzma
 
 
; MUI 1.67 compatible ------
!include "MUI.nsh"
 
; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "installers\boardvantage.ico"
 
; Welcome page
; Components page
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!insertmacro MUI_PAGE_FINISH
 
; Language files
!insertmacro MUI_LANGUAGE "English"
 
; Reserve files
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
 
; MUI end ------
 
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "bv_launch.exe"
InstallDir "C:\BV_TN1"
ShowInstDetails show
 
Section -SETTINGS
  SetOutPath "$INSTDIR"
  SetOverwrite on
SectionEnd
 
 Section "Checking Environment Variables" SEC01
 SetRegView 32
;test for java 1.6.024
  ReadRegStr $3 HKLM 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\JAVA_HOME' Data
  IfErrors 0 noJavaInstall
Goto doneJava
noJavaInstall:
ReadRegStr $1 HKLM 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\JAVA_HOME' Data
IfErrors 0 doneJava
ReadRegStr $1 HKLM 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\JAVA_HOME' Data
MessageBox MB_OK 'Good news, JAVA_HOME is set as, $1 .'
doneJava:
  ;All done.   
  
  NoJava:  
  ;Write the script to install Java here  
  
SectionEnd 
I'm trying to get the MessageBox to pop up and tell the user where JAVA_HOME is, but I'm not exactly sure where I'm messing up. Totally lost.
toqer#
Trying to verify if an environment variable exists part II

So this is what my code looks like.

Section "Checking JAVA_HOME" SEC01
ReadEnvStr $0 JAVA_HOME
StrLen $0 $1
${If} $0 = 0
  MessageBox MB_OK 'JAVA_HOME not found'
  Abort
${EndIf}
  MessageBox MB_OK 'Found JAVA_HOME at $0'
 SectionEnd 
What I think I'm doing (wrong, because it's not working)
1. explode JAVA_HOME into variable $0
2. Check to see if strlen of $0=0. If it's 0, then abort install (and popup JAVA_HOME isn't defined)
3. If $0 /= 0, then popup a box giving the environment variables value.

Right now it just prints a 0 and aborts. Where am I screwing this up?
Anders#
"StrLen $0 $1" means store length of $1 in $0.

You want StrLen $1 $0 and then compare $1 to 0. Or just don't check the length, you can just check the string directly with ${If} $0 == "" ...

A lot of the calculate/getter functions have the return/destination as the first parameter...
toqer#
Anders thank you so much, you are a life/job-saver.

If anyone else needs a similar bit of code,

Section "Checking JAVA_HOME" SEC01
ReadEnvStr $0 JAVA_HOME
${If} $0 == ""
  MessageBox MB_OK 'JAVA_HOME not found'
  Abort
${EndIf}
  MessageBox MB_OK 'Found JAVA_HOME at $0'
 SectionEnd