EnVar plug-in: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 10: Line 10:
== Description ==
== Description ==


Basically this plugin allows you to check for environment variables, check for paths in those variables, add and remove paths, and delete environment variables.
Basically this plugin allows you to check for environment variables, check for paths in those variables, add and remove paths, delete environment variables, and update the installer environment if the windows environment has changed.


Just extract the contents to your nsis directory (usually '$PROGRAMFILES\NSIS').
Just extract the contents to your nsis directory (usually '$PROGRAMFILES\NSIS').
Line 46: Line 46:
   DetailPrint "EnVar::SetHKCU"
   DetailPrint "EnVar::SetHKCU"


   ; Check for a 'temp' variable
   ; Check if the 'temp' variable exists in EnVar::SetHKxx
   EnVar::Check "temp" "NULL"
   EnVar::Check "temp" "NULL"
   Pop $0
   Pop $0
   DetailPrint "EnVar::Check returned=|$0|"
   DetailPrint "EnVar::Check returned=|$0|"


   ; Add a value
   ; Append a value
   EnVar::AddValue "ZTestVariable" "C:\Test"
   EnVar::AddValue "ZTestVariable" "C:\Test"
   Pop $0
   Pop $0
Line 60: Line 60:
   DetailPrint "EnVar::AddValue returned=|$0|"
   DetailPrint "EnVar::AddValue returned=|$0|"


  ; Add an expanded value
   EnVar::AddValue "ZTestVariable1" "C:\Test"
   EnVar::AddValue "ZTestVariable1" "C:\Test"
   Pop $0
   Pop $0
   DetailPrint "EnVar::AddValue returned=|$0|"
   DetailPrint "EnVar::AddValue returned=|$0|"


  ; Append an expanded value (REG_EXPAND_SZ)
   EnVar::AddValueEx "ZTestVariable1" "C:\Test"
   EnVar::AddValueEx "ZTestVariable1" "C:\Test"
   Pop $0
   Pop $0
   DetailPrint "EnVar::AddValue returned=|$0|"
   DetailPrint "EnVar::AddValueEx returned=|$0|"


   EnVar::AddValueEx "ZTestVariable1" "C:\TestVariable"
   EnVar::AddValueEx "ZTestVariable1" "C:\TestVariable"
   Pop $0
   Pop $0
   DetailPrint "EnVar::AddValue returned=|$0|"
   DetailPrint "EnVar::AddValueEx returned=|$0|"


   ; Delete a value from a variable
   ; Delete a value from a variable
Line 85: Line 85:
   Pop $0
   Pop $0
   DetailPrint "EnVar::DeleteValue returned=|$0|"
   DetailPrint "EnVar::DeleteValue returned=|$0|"
  ; Set a value (overwrites everything in the value)
  EnVar::SetValue "ZTestVariable1" "C:\Test1"
  Pop $0
  DetailPrint "EnVar::SetValue returned=|$0|"
  EnVar::SetValue "ZTestVariable2" "C:\Test2"
  Pop $0
  DetailPrint "EnVar::SetValue returned=|$0|"
  EnVar::SetValue "ZTestVariable3" "C:\Test3"
  Pop $0
  DetailPrint "EnVar::SetValue returned=|$0|"
  ; Set an expanded value (REG_EXPAND_SZ)
  EnVar::SetValueEx "ZTestVariable2" "C:\Test4"
  Pop $0
  DetailPrint "EnVar::SetValueEx returned=|$0|"
  EnVar::SetValueEx "ZTestVariable3" "C:\Test5"
  Pop $0
  DetailPrint "EnVar::SetValueEx returned=|$0|"


   ; Delete a variable
   ; Delete a variable
Line 91: Line 113:
   DetailPrint "EnVar::Delete returned=|$0|"
   DetailPrint "EnVar::Delete returned=|$0|"


   ; Try deleting "path", this should give an error
  EnVar::Delete "ZTestVariable1"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
  EnVar::Delete "ZTestVariable2"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
  EnVar::Delete "ZTestVariable3"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
   ; Try deleting "path", this should give an error (%path% is a shared resource)
   EnVar::Delete "path"
   EnVar::Delete "path"
   Pop $0
   Pop $0
   DetailPrint "EnVar::Delete returned=|$0|"
   DetailPrint "EnVar::Delete returned=|$0|"  


SectionEnd
SectionEnd

Latest revision as of 06:09, 30 January 2026

Author: JasonFriday13 (talk, contrib)


Links

Plugin package:
EnVar_plugin.zip (37 KB)

Forum thread

Description

Basically this plugin allows you to check for environment variables, check for paths in those variables, add and remove paths, delete environment variables, and update the installer environment if the windows environment has changed.

Just extract the contents to your nsis directory (usually '$PROGRAMFILES\NSIS').

Sample Usage

Name "EnVar Example"
OutFile "EnVarExample.exe"
 
RequestExecutionLevel User
ShowInstDetails Show
 
Page InstFiles
 
Unicode True
 
Section
 
  ; Check for write access
  EnVar::Check "NULL" "NULL"
  Pop $0
  DetailPrint "EnVar::Check write access HKCU returned=|$0|"
 
  ; Set to HKLM
  EnVar::SetHKLM

  ; Check for write access
  EnVar::Check "NULL" "NULL"
  Pop $0
  DetailPrint "EnVar::Check write access HKLM returned=|$0|"
 
  ; Set back to HKCU
  EnVar::SetHKCU
  DetailPrint "EnVar::SetHKCU"
 
  ; Check if the 'temp' variable exists in EnVar::SetHKxx
  EnVar::Check "temp" "NULL"
  Pop $0
  DetailPrint "EnVar::Check returned=|$0|"
 
  ; Append a value
  EnVar::AddValue "ZTestVariable" "C:\Test"
  Pop $0
  DetailPrint "EnVar::AddValue returned=|$0|"
 
  EnVar::AddValue "ZTestVariable" "C:\TestJas"
  Pop $0
  DetailPrint "EnVar::AddValue returned=|$0|"
 
  EnVar::AddValue "ZTestVariable1" "C:\Test"
  Pop $0
  DetailPrint "EnVar::AddValue returned=|$0|"
 
  ; Append an expanded value (REG_EXPAND_SZ)
  EnVar::AddValueEx "ZTestVariable1" "C:\Test"
  Pop $0
  DetailPrint "EnVar::AddValueEx returned=|$0|"
 
  EnVar::AddValueEx "ZTestVariable1" "C:\TestVariable"
  Pop $0
  DetailPrint "EnVar::AddValueEx returned=|$0|"
 
  ; Delete a value from a variable
  EnVar::DeleteValue "ZTestVariable1" "C:\Test"
  Pop $0
  DetailPrint "EnVar::DeleteValue returned=|$0|"
 
  EnVar::DeleteValue "ZTestVariable1" "C:\Test"
  Pop $0
  DetailPrint "EnVar::DeleteValue returned=|$0|"
 
  EnVar::DeleteValue "ZTestVariable1" "C:\TestJason"
  Pop $0
  DetailPrint "EnVar::DeleteValue returned=|$0|"
 
  ; Set a value (overwrites everything in the value)
  EnVar::SetValue "ZTestVariable1" "C:\Test1"
  Pop $0
  DetailPrint "EnVar::SetValue returned=|$0|"
 
  EnVar::SetValue "ZTestVariable2" "C:\Test2"
  Pop $0
  DetailPrint "EnVar::SetValue returned=|$0|"
 
  EnVar::SetValue "ZTestVariable3" "C:\Test3"
  Pop $0
  DetailPrint "EnVar::SetValue returned=|$0|"
 
  ; Set an expanded value (REG_EXPAND_SZ)
  EnVar::SetValueEx "ZTestVariable2" "C:\Test4"
  Pop $0
  DetailPrint "EnVar::SetValueEx returned=|$0|"
 
  EnVar::SetValueEx "ZTestVariable3" "C:\Test5"
  Pop $0
  DetailPrint "EnVar::SetValueEx returned=|$0|"
 
  ; Delete a variable
  EnVar::Delete "ZTestVariable"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
  EnVar::Delete "ZTestVariable1"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
  EnVar::Delete "ZTestVariable2"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
  EnVar::Delete "ZTestVariable3"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|"
 
  ; Try deleting "path", this should give an error (%path% is a shared resource)
  EnVar::Delete "path"
  Pop $0
  DetailPrint "EnVar::Delete returned=|$0|" 
 
SectionEnd