Setting Environment Variables Examples: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(A/W fix)
 
Line 15: Line 15:
messagebox mb_ok '$R0'
messagebox mb_ok '$R0'
StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("PATH", R0).r2'
System::Call 'KERNEL32::SetEnvironmentVariable(t "PATH", t R0)i.r2'
ReadEnvStr $R0 "PATH"
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
messagebox mb_ok '$R0'
Line 51: Line 51:
;or just this
;or just this
;StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
;StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("PATH", R0).r2'
System::Call 'Kernel32::SetEnvironmentVariable(t "PATH", t R0)i.r2'
ReadEnvStr $R0 "PATH"
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
messagebox mb_ok '$R0'

Latest revision as of 22:56, 14 January 2021

Author: Red Wine (talk, contrib)


Description

The following examples about setting environment variables are based on Setting Environment Variables by KiCHiK and provided for the subject on this forum thread.
Requires KiCHiK's WriteEnvStr.nsh.

The Code

Case 1. If you want to set an environment variable only for the installer process and its sub-processes use:

!define JAVA_HOME "d:\JDK1.6"
!define APP_HOME "d:\application"
 
Section "Add Env Var"
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
System::Call 'KERNEL32::SetEnvironmentVariable(t "PATH", t R0)i.r2'
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
SectionEnd

Case 2. If you want to set an environment variable for the installer process and its sub-processes.Also that will stick for every other process and after reboots too...

!define JAVA_HOME "d:\JDK1.5"
!define APP_HOME "d:\application"
 
!include WriteEnvStr.nsh
 
Section "Add Env Var"
  !ifdef ALL_USERS
    !define ReadEnvStr_RegKey \
       'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
  !else
    !define ReadEnvStr_RegKey 'HKCU "Environment"'
  !endif
 
  Push JAVA_HOME
  Push '${JAVA_HOME}'
  Call WriteEnvStr
  Push APP_HOME
  Push '${APP_HOME}'
  Call WriteEnvStr
 
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
;ensure that is written valid for NT only
ReadRegStr $0 ${ReadEnvStr_RegKey} 'JAVA_HOME'
ReadRegStr $1 ${ReadEnvStr_RegKey} 'APP_HOME'
StrCpy $R0 "$R0;$0;$1"
;or just this
;StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
System::Call 'Kernel32::SetEnvironmentVariable(t "PATH", t R0)i.r2'
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
writeuninstaller '$EXEDIR\uninst.exe'
SectionEnd
# ...
Section uninstall
  # remove the variable
  Push JAVA_HOME
  Call un.DeleteEnvStr
  Push APP_HOME
  Call un.DeleteEnvStr
SectionEnd

License

This function is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this function.

Permission is granted to anyone to use this function for any purpose, including commercial applications, and to alter it and redistribute it freely.