Skip to content
⌘ NSIS Forum Archive

Set environnement variable problem

3 posts

Fadjerx#

Set environnement variable problem

Hello friends,

I have the need to create an environnement variable for my app. I went to the following link and I did exactly what is described in paragraph "Your own variable". It works fine, and the varibale is really created after installation.



Now I propose to the user to launch the program after installation finished :
+ If the user choose to launch the program later it continues to work fine, and my added variable is well recognised
+ But if the user choose to launch the program immediately after installation finished it does not work ! :-( I don't khnow why ?

Here is snippet of the code I use to propose launching after installation (found on NSIS site too):

.
.
.
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_TEXT "Start $(^Name)"
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
.
.
.
Function LaunchLink
  ExecShell "" "$SMPROGRAMS\$StartMenuGroup\MyApp.lnk"
FunctionEnd 
Thanks in advance for your help.
demiller9#
You should also do the piece of code that sets the variable temporarily (call the API Kernel32::SetEnvironmentVariableA as shown in the wiki you found). The permanent change loads the variables when the user starts the app from Explorer. The installer's variables are used when NSIS starts a process.
Fadjerx#
Hi demiller9,

Thanks a lot it works fine !

This is a summary who to do to add an environment variable permanently, while to be able to launch application immediately after installation finished :

# To add a environment var permanently
!include "winmessages.nsh"
!define env_hklm 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
!define env_hkcu 'HKCU "Environment"'    
WriteRegExpandStr ${env_hklm} MY_VAR MY_VAR_VALUE
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=10000 
# To launch app aftrer install
.
.
.
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_TEXT "Start $(^Name)"
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
.
.
.
Function LaunchLink
    System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("MY_VAR", "MY_VAR_VALUE").r0'
    StrCmp $0 0 error
    ExecWait ProgThatReadsEnv.exe
    Goto done
    error:
    MessageBox MB_OK "Can't set environment variable"
    done:
    ExecShell "" "$SMPROGRAMS\$StartMenuGroup\MyApp.lnk"
FunctionEnd 
Good luck to you.