Setting Environment Variables Examples: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
{{PageAuthor|Red Wine}} | {{PageAuthor|Red Wine}} | ||
== | == == | ||
The following examples about setting environment variables are based on [[Setting Environment Variables]] by [[user:kichik|KiCHiK]] and provided for the subject [http://forums.winamp.com/showthread.php?s=&postid=2084244 on this forum thread].<BR> | The following examples about setting environment variables are based on [[Setting Environment Variables]] by [[user:kichik|KiCHiK]] and provided for the subject [http://forums.winamp.com/showthread.php?s=&postid=2084244 on this forum thread].<BR> | ||
Requires [[user:kichik|KiCHiK]]'s [http://nsis.sourceforge.net/Setting_Environment_Variables WriteEnvStr.nsh].<BR> | Requires [[user:kichik|KiCHiK]]'s [http://nsis.sourceforge.net/Setting_Environment_Variables WriteEnvStr.nsh].<BR> |
Revision as of 13:21, 3 March 2010
Author: Red Wine (talk, contrib) |
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.5" !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::SetEnvironmentVariableA(t, t) i("PATH", R0).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::SetEnvironmentVariableA(t, t) i("PATH", R0).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.