Question

Ok, I have a simplified version of my nsi script (see below). In the A2 section, I copy a single executable to the specified installation path, create environment variables, then do a SendMessage which is supposed to make all currently running processes aware of the env change. However, it seems that the NSIS installer process itself doesn't get updated because the shortcut I create in the Links section doesn't work

installer.nsi:

SetCompressor /FINAL zlib

!include LogicLib.nsh
!include WinMessages.nsh
!include x64.nsh

!define ENGINE "TEST"
!define DERIV "A2"
!define LIB_VER "v43"
!define RELEASE "v3dev2"

Name "${ENGINE}${DERIV} DTE ${RELEASE}"
OutFile "${ENGINE}${DERIV}-DTE.exe"

; display the installation directory page
; note that NSIS places the selected directory in $INSTDIR
; DirText ""
Page directory setDefaultInstallDirectory
Function setDefaultInstallDirectory
    ;check for an existing sim root and set it as 
     the default installation directory     if it exists
    ReadEnvStr $1 SIM_ROOT
    ${If} $1 != ""
        StrCpy $INSTDIR $1
    ${EndIf}
FunctionEnd

; display the installation page and show a message 
; when the installation is complete
Page instfiles "" "" finished
Function finished
    MessageBox MB_OK "Installation Complete."
FunctionEnd

section "A2"
SetOutPath $INSTDIR\${ENGINE}\${DERIV}
File alt_control.exe

; Environment Variables
WriteRegStr HKCU "Environment" "SIM_ROOT" "$INSTDIR"

WriteRegStr HKCU "Environment" "ENGINE" "${ENGINE}"

WriteRegStr HKCU "Environment" "DERIV" "${DERIV}"

WriteRegExpandStr HKCU "Environment" "RUN_DIR" "%SIM_ROOT%\%ENGINE%\%DERIV%"

; Broadcast to all processes that they need to update their environment
; http://forums.winamp.com/showthread.php?t=118501
SendMessage ${HWND_BROADCAST} 
         ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
sectionEnd

section "Links"
; create the start menu directory & shortcuts
CreateDirectory $SMPROGRAMS\DTE
SetOutPath "$INSTDIR"
CreateShortCut "$SMPROGRAMS\DTE\AltControl.lnk" 
           "$INSTDIR\%ENGINE%\%DERIV%\alt_control.exe"

; Broadcast to all processes that they need to update their environment
; http://forums.winamp.com/showthread.php?t=118501
SendMessage ${HWND_BROADCAST} 
         ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
sectionEnd

After you run the installer, then attempt to run the start menu shortcut AltControl.lnk, you get a Windows missing shortcut error. Specifically: "Windows is searching for alt_control.exe. To locate the file yourself, click Browse."

If the environment variables already exist when you perform the installation, then the link works. Even weirder, if you click on the start menu shortcut properties and make some trivial change (like add and remove a space in the comment field) then click apply, Windows seems to regenerate the AltControl.lnk file (I know because the .lnk file increases in size even though no functional change was made through the dialog!?) and it works! So it seems like NSIS or some background Windows process in charge of generating/resolving .lnk files is not picking up on the newly created environment variables during the installation process. I've scoured the web and everything seems to indicate that the SendMessage I'm using should force everything to be aware of the newly created env variables. Thanks in advance to whoever can solve this mystery. You can use the included installer.nsi script to replicate my situation. Also note that it exhibits this behavior in multiple environments (XP, Vista, 7, with/without admin, etc.).

Was it helpful?

Solution

Most applications do not handle that message broadcast, it is mostly just for explorer.exe.

You can update the variables directly in your installer process and that will also be inherited by child processes:

System::Call 'Kernel32::SetEnvironmentVariable(t "ENGINE", t "v8")'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top