Old 10th April 2014, 21:11   #1
toqer
Winamp's Little Stalker
(Member)
 
Join Date: Mar 2004
Location: under a bridge
Posts: 796
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.

PHP 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 is offline   Reply With Quote
Old 10th April 2014, 21:18   #2
toqer
Winamp's Little Stalker
(Member)
 
Join Date: Mar 2004
Location: under a bridge
Posts: 796
Got it.

ReadEnvStr $0 JAVA_HOME

and call it a day.
toqer is offline   Reply With Quote
Old 18th April 2014, 02:01   #3
toqer
Winamp's Little Stalker
(Member)
 
Join Date: Mar 2004
Location: under a bridge
Posts: 796
Trying to verify if an environment variable exists part II

So this is what my code looks like.

PHP Code:
Section "Checking JAVA_HOME" SEC01
ReadEnvStr 
$0 JAVA_HOME
StrLen 
$$1
${If} $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?
toqer is offline   Reply With Quote
Old 18th April 2014, 03:11   #4
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,181
"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...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 21st April 2014, 19:46   #5
toqer
Winamp's Little Stalker
(Member)
 
Join Date: Mar 2004
Location: under a bridge
Posts: 796
Anders thank you so much, you are a life/job-saver.

If anyone else needs a similar bit of code,

PHP Code:
Section "Checking JAVA_HOME" SEC01
ReadEnvStr 
$0 JAVA_HOME
${If} $== ""
  
MessageBox MB_OK 'JAVA_HOME not found'
  
Abort
${EndIf}
  
MessageBox MB_OK 'Found JAVA_HOME at $0'
 
SectionEnd 
toqer is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump