Archive: StrStr and adding to PATH env var


StrStr and adding to PATH env var
Here's a couple of functions I whipped up for the purpose of allowing me to add something to the PATH environment variable. The StrStr function was implemented so I could see if what I was going to add was already in the path.

If someone has some suggestions for improving them that'd be great .. they were my first working pass as them so I'm sure there's probably some room for improvement.


;===================================================================
; StrStr - Find $0 in $1.
; $0 - String to find.
; $1 - String to search in.
; $2 - [out] Index of where string was found, or -1.
;===================================================================
Function StrStr
Push $3
Push $4
Push $5

StrCpy $2 -1
StrLen $4 $0
StrLen $5 $1
IntOp $3 $5 - $4

loop:
IntOp $2 $2 + 1
IntCmp $2 $3 0 0 fail
StrCpy $5 $1 $4 $2
StrCmp $0 $5 return loop

fail:
StrCpy $2 -1

return:
Pop $5
Pop $4
Pop $3
FunctionEnd


;===================================================================
; AddOUTDIRToSearchPath - Append the given directory to the
; PATH env var.
;===================================================================
Function AddOUTDIRToSearchPath
Push $0
Push $1
Push $2

StrCpy $0 $OUTDIR
ReadRegStr $1 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
StrCmp $1 "" 0 winnt
ReadRegStr $1 HKLM SOFTWARE\Microsoft\Windows\CurrentVersion VersionNumber
StrCmp $1 "" return win9x

winnt:
ReadRegStr $1 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path
Call StrStr
IntCmp $2 -1 0 0 return
StrCpy $1 "$1;$OUTDIR"
WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path $1
Goto return

win9x:
ReadEnvStr $1 PATH
Call StrStr
IntCmp $2 -1 0 0 return
ClearErrors
FileOpen $1 "C:\autoexec.bat" "a"
FileSeek $1 0 SET
loop:
FileRead $1 $2
IfErrors end
StrCmp "PATH=%PATH%;$OUTDIR$\r$\n" $2 return loop
end:
FileSeek $1 0 END
FileWrite $1 "$\r$\nPATH=%PATH%;$OUTDIR$\r$\n"
FileClose $1

return:
Pop $2
Pop $1
Pop $0
FunctionEnd

One thing is missing in adding the PATH to WinNT which is a program that its only line code is:


SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &dwReturnValue);


This will cause all of Windows apps to reload the env and thus you will not need to logoff and on again.

There is also a little problem with Win9x search path addition, the path you have inserted into the autoexec.bat is in Windoes long file name format. Win9x needs 8.3 names. I have implented a new function into NSIS called GetShortPathName, which returns 8.3 names, but it is no longer needed as 1.7 will have GetFullPathName /SHORT.

One thing I don't understand is what is ]. Where did it come from? Is this a normal NSIS var or a changed NSIS?

I have been working on this kind of code too. I must say there are some improvments in your code over mine. Yours is very efficient.
Do you mind telling me what application you are working on?

Good catch on the short file name issue, obviously it hadn't occurred to me. Generally for my purposes I wasn't too concerned about the fact that the user would have to log off/on (or reboot for 9x).

I hadn't noticed the [ all over the place .. if you replace them with 'dollar 1' then you'll see what I see when I look at my code. I suppose this message board software replaces it with [.


Another improvement you can add is usage with the stack instead of set vars like $1 and $2. It will make it usable anywhere.

Attached is the program to refresh the env vars in WinNT, with the source.


One more thing you have forgot:
What if Windows9x is not installed on C:?


.


I can't post! I can't edit! What happened?! Only short messages get posted all else gives me server not found!


Here it is, all togther in one zip file. Remove included.