Old 7th October 2011, 23:14   #1
sethradio
Junior Member
 
Join Date: Oct 2011
Posts: 20
Path

How do you make an NSIS installer ad the installation directory to the Windows PATH environment variable?
sethradio is offline   Reply With Quote
Old 7th October 2011, 23:19   #2
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
The default environment variables for the current user are stored at:
HKEY_CURRENT_USER\Environment

For the system (all users) they are stored at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

You can use NSIS' registry functions to read the current value of "PATH", add your directory to it and write the new value back.

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 8th October 2011, 23:11   #3
sethradio
Junior Member
 
Join Date: Oct 2011
Posts: 20
Uh....

Could you show me some example code please?
sethradio is offline   Reply With Quote
Old 9th October 2011, 01:56   #4
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
You should start here:
http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2

Maybe try like this:
code:
ReadRegStr $0 HKCU "Environment" "Path" #read current PATH into $0
StrCpy $0 "C:\SomePath;$0" #prepend your path to the current PATH value
WriteRegStr HKCU "Environment" "Path" '$0' #write back the new PATH



(Note that the new PATH may become effective only after a reboot)

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 11th October 2011, 20:01   #5
sethradio
Junior Member
 
Join Date: Oct 2011
Posts: 20
Thanks!! You really are a major dude!! I didn't know environment variables were stored in the registry. I need to read up on the registry documentation in the NSIS users manual.
sethradio is offline   Reply With Quote
Old 11th October 2011, 20:09   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,197
This is not a good idea:

A) Why fill the global path with your crap, there is a total limit, what if all apps did this? It is possible to add per application path info in the registry under "App Paths"

B) Using normal NSIS strings to manipulate global variables is a very bad idea, if the user has a %path% longer than 1024 you will corrupt their setup!

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 11th October 2011, 22:56   #7
sethradio
Junior Member
 
Join Date: Oct 2011
Posts: 20
I will only use this if I am writing a command line tool. This allows users of a program to not have to type the full path to the executable.
sethradio is offline   Reply With Quote
Old 11th October 2011, 23:03   #8
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
Quote:
Originally Posted by sethradio View Post
I will only use this if I am writing a command line tool. This allows users of a program to not have to type the full path to the executable.
You should at least ask the user whether he wants your tool to be added to his PATH.

As an alternative you can add a shortcut to the Startmenu that opens a console with your tool added to the PATH, instead of adding your tool to the PATH globally. Many applications, like Visual Studio or GIT, use this technique.

The command for the Startmenu shortcut could be something like:
%comspec% /k ""$INSTDIR\setpath.bat""

And the "setpath.bat" could be created on-the-fly by the installer and should look like:
code:
@echo off
echo Setting up environment for Super Duper Tool...
set "PATH=C:\Path To Your Tool;%PATH%"


My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 16th November 2011, 03:34   #9
sethradio
Junior Member
 
Join Date: Oct 2011
Posts: 20
How would I remove them

How do I remove the install path from the environment variable in the uninstaller?
sethradio is offline   Reply With Quote
Old 16th November 2011, 12:01   #10
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
Option 1:
Backup the "old" path at install time. Restore it at un-install time. Chnages that have been made since install will be lost!

Option 2:
Read the current PATH at un-install time and use string functions to find "your" path in the current PATH. If found, remove only that part and write the modified PATH back. Harder to do than Option 1, but no changes made by other apps will be lost. However this solution again has the problem that "standard" NSIS can not cope with strings longer than 1024 characters!

Option 3:
Avoid modifying the global PATH environment variable from the very beginning, as has been suggested before in this thread. No problem with un-install and no NSIS string limitations. Probably the cleanest solution that you can go for...

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 22nd November 2011, 01:00   #11
sethradio
Junior Member
 
Join Date: Oct 2011
Posts: 20
I know I shouldn't use nsis string functions to edit the pah variable at this point. But for the sake of it, how could you use string function to find the install path?
sethradio is offline   Reply With Quote
Old 22nd November 2011, 10:42   #12
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
Quote:
Originally Posted by sethradio View Post
I know I shouldn't use nsis string functions to edit the pah variable at this point. But for the sake of it, how could you use string function to find the install path?
You don't have to.

At install time, simply save your $INSTDIR to some (unique) registry key of your own.

At un-install time, read that registry key. No string manipulations will be required.

And, as on Windows the MAX_PATH is 260, the 1024 characters of an NISIS string are sufficient - for a single path.

(Actually the uninstaller will already have $INSTDIR set to the path it was started from)

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Tags
nsis, path

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