Old 2nd August 2010, 11:53   #1
nv_rajanikanth
Junior Member
 
Join Date: Jun 2010
Posts: 20
Not able to delete ENV variable & its value

Hi

Using installer i am successfully adding the ENV variable. but not able to delete it.
please let me know if anything chage is required in the following code to make this working fine.(giving parital code)

Var /GLOBAL AcsRoot

Function .onInit
StrCpy $AcsRoot "C:\Rajani\home\Administrator"
FunctionEnd
;-----in install----
Push "ACS_ROOT"
Push $AcsRoot
Call AddToEnvVar
;-----------------


;-------in uninstall--
Push "ACS_ROOT_DIR"
Push $AcsRootDir
Call un.RemoveFromEnvVar
;-------------------




Function un.RemoveFromEnvVar

Exch $1 ; $1 has environment variable value
Exch
Exch $0 ; $0 has environment variable name

DetailPrint "Removing $1 from $0"
Push $2
Push $3
Push $4
Push $5
Push $6
Push $7

IntFmt $7 "%c" 26 # DOS EOF
;DetailPrint "Print 10"
Call un.IsNT
Pop $2
StrCmp $2 1 unRemoveFromEnvVar_NT
; Not on NT
StrCpy $2 $WINDIR 2
;DetailPrint "Print 11"
FileOpen $2 "$2\autoexec.bat" r
GetTempFileName $5
FileOpen $3 $5 w
GetFullPathName /SHORT $1 $1
StrCpy $1 "SET $0=%$0%;$1"
Goto unRemoveFromEnvVar_dosLoop

unRemoveFromEnvVar_dosLoop:
;DetailPrint "Print 12"
FileRead $2 $4
StrCpy $6 $4 1 -1 # read last char
StrCmp $6 $7 0 +2 # if DOS EOF
StrCpy $4 $4 -1 # remove DOS EOF so we can compare
StrCmp $4 "$1$\r$\n" unRemoveFromEnvVar_dosLoopRemoveLine
StrCmp $4 "$1$\n" unRemoveFromEnvVar_dosLoopRemoveLine
StrCmp $4 "$1" unRemoveFromEnvVar_dosLoopRemoveLine
StrCmp $4 "" unRemoveFromEnvVar_dosLoopEnd
FileWrite $3 $4
Goto unRemoveFromEnvVar_dosLoop
unRemoveFromEnvVar_dosLoopRemoveLine:
;DetailPrint "Print 13"
SetRebootFlag true
Goto unRemoveFromEnvVar_dosLoop

unRemoveFromEnvVar_dosLoopEnd:
;DetailPrint "Print 14"
FileClose $3
FileClose $2
StrCpy $2 $WINDIR 2
Delete "$2\autoexec.bat"
CopyFiles /SILENT $5 "$2\autoexec.bat"
Delete $5
Goto unRemoveFromEnvVar_done

unRemoveFromEnvVar_NT:
;DetailPrint "Print 15"
ReadRegStr $2 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" $0
;DetailPrint $2
StrCpy $6 $2 1 -1 # copy last char
;DetailPrint $6
StrCmp $6 ";" +2 # if last char != ;
StrCpy $2 "$2;" # append ;
Push $2
Push "$1;"
Call un.StrStr ; Find `$1;` in $2
Pop $3 ; pos of our dir
StrCmp $3 "" unRemoveFromEnvVar_done
; else, it is in path
# $1 - path to add
# $2 - path var
StrLen $4 "$1;"
StrLen $5 $3
StrCpy $6 $2 -$5 # $6 is now the part before the path to remove
StrCpy $7 $3 "" $4 # $7 is now the part after the path to remove
StrCpy $4 $6$7

StrCpy $6 $4 1 -1 # copy last char
StrCmp $6 ";" 0 +2 # if last char == ;
StrCpy $4 $4 -1 # remove last char

WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" $0 $4
;DetailPrint "Print 16"
; delete reg value if null
StrCmp $4 "" 0 +2 # if null delete reg
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" $0

;SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000

unRemoveFromEnvVar_done:
;DetailPrint "Print 17"
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd
nv_rajanikanth is offline   Reply With Quote
Old 2nd August 2010, 12:31   #2
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Please use pastebin or an attached nsi file for sharing large amounts of code.
MSG is offline   Reply With Quote
Old 2nd August 2010, 19:14   #3
redxii
Senior Member
 
Join Date: Nov 2005
Posts: 115
Try EnvVarUpdate.nsh

http://nsis.sourceforge.net/Environm...tries#Examples
redxii 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