source: branches/t3e-tools/t3q/nsis/WriteEnvStr.nsh @ 16

Last change on this file since 16 was 7, checked in by phdmakk, 15 years ago
File size: 3.1 KB
Line 
1!ifndef _WriteEnvStr_nsh
2!define _WriteEnvStr_nsh
3 
4!include WinMessages.nsh
5 
6!ifndef WriteEnvStr_RegKey
7  !ifdef ALL_USERS
8    !define WriteEnvStr_RegKey \
9       'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
10  !else
11    !define WriteEnvStr_RegKey 'HKCU "Environment"'
12  !endif
13!endif
14 
15#
16# WriteEnvStr - Writes an environment variable
17# Note: Win9x systems requires reboot
18#
19# Example:
20#  Push "HOMEDIR"           # name
21#  Push "C:\New Home Dir\"  # value
22#  Call WriteEnvStr
23#
24Function WriteEnvStr
25  Exch $1 ; $1 has environment variable value
26  Exch
27  Exch $0 ; $0 has environment variable name
28  Push $2
29 
30  Call IsNT
31  Pop $2
32  StrCmp $2 1 WriteEnvStr_NT
33    ; Not on NT
34    StrCpy $2 $WINDIR 2 ; Copy drive of windows (c:)
35    FileOpen $2 "$2\autoexec.bat" a
36    FileSeek $2 0 END
37    FileWrite $2 "$\r$\nSET $0=$1$\r$\n"
38    FileClose $2
39    SetRebootFlag true
40    Goto WriteEnvStr_done
41 
42  WriteEnvStr_NT:
43      WriteRegExpandStr ${WriteEnvStr_RegKey} $0 $1
44      SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} \
45        0 "STR:Environment" /TIMEOUT=5000
46 
47  WriteEnvStr_done:
48    Pop $2
49    Pop $0
50    Pop $1
51FunctionEnd
52 
53#
54# un.DeleteEnvStr - Removes an environment variable
55# Note: Win9x systems requires reboot
56#
57# Example:
58#  Push "HOMEDIR"           # name
59#  Call un.DeleteEnvStr
60#
61Function un.DeleteEnvStr
62  Exch $0 ; $0 now has the name of the variable
63  Push $1
64  Push $2
65  Push $3
66  Push $4
67  Push $5
68 
69  Call un.IsNT
70  Pop $1
71  StrCmp $1 1 DeleteEnvStr_NT
72    ; Not on NT
73    StrCpy $1 $WINDIR 2
74    FileOpen $1 "$1\autoexec.bat" r
75    GetTempFileName $4
76    FileOpen $2 $4 w
77    StrCpy $0 "SET $0="
78    SetRebootFlag true
79 
80    DeleteEnvStr_dosLoop:
81      FileRead $1 $3
82      StrLen $5 $0
83      StrCpy $5 $3 $5
84      StrCmp $5 $0 DeleteEnvStr_dosLoop
85      StrCmp $5 "" DeleteEnvStr_dosLoopEnd
86      FileWrite $2 $3
87      Goto DeleteEnvStr_dosLoop
88 
89    DeleteEnvStr_dosLoopEnd:
90      FileClose $2
91      FileClose $1
92      StrCpy $1 $WINDIR 2
93      Delete "$1\autoexec.bat"
94      CopyFiles /SILENT $4 "$1\autoexec.bat"
95      Delete $4
96      Goto DeleteEnvStr_done
97 
98  DeleteEnvStr_NT:
99    DeleteRegValue ${WriteEnvStr_RegKey} $0
100    SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} \
101      0 "STR:Environment" /TIMEOUT=5000
102 
103  DeleteEnvStr_done:
104    Pop $5
105    Pop $4
106    Pop $3
107    Pop $2
108    Pop $1
109    Pop $0
110FunctionEnd
111 
112!ifndef IsNT_KiCHiK
113!define IsNT_KiCHiK
114 
115#
116# [un.]IsNT - Pushes 1 if running on NT, 0 if not
117#
118# Example:
119#   Call IsNT
120#   Pop $0
121#   StrCmp $0 1 +3
122#     MessageBox MB_OK "Not running on NT!"
123#     Goto +2
124#     MessageBox MB_OK "Running on NT!"
125#
126!macro IsNT UN
127Function ${UN}IsNT
128  Push $0
129  ReadRegStr $0 HKLM \
130    "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
131  StrCmp $0 "" 0 IsNT_yes
132  ; we are not NT.
133  Pop $0
134  Push 0
135  Return
136 
137  IsNT_yes:
138    ; NT!!!
139    Pop $0
140    Push 1
141FunctionEnd
142!macroend
143!insertmacro IsNT ""
144!insertmacro IsNT "un."
145 
146!endif ; IsNT_KiCHiK
147 
148!endif ; _WriteEnvStr_nsh
Note: See TracBrowser for help on using the repository browser.