NSIS CreateShortcut With Environment Variables

时间:2013-07-16 23:43:09

标签: variables nsis shortcut environment

好的,我有一个简体版的nsi脚本(见下文)。在A2部分中,我将单个可执行文件复制到指定的安装路径,创建环境变量,然后执行SendMessage,它应该使所有当前正在运行的进程知道env更改。但是,似乎NSIS安装程序进程本身没有更新,因为我在Links部分创建的快捷方式不起作用

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

运行安装程序后,尝试运行开始菜单快捷方式AltControl.lnk,导致Windows缺少快捷方式错误。具体来说:“Windows正在搜索alt_control.exe。要自己找到该文件,请单击”浏览“。

如果执行安装时环境变量已存在,则链接有效。甚至更奇怪,如果你单击开始菜单快捷方式属性并进行一些微不足道的更改(如添加和删除注释字段中的空格)然后单击应用,Windows似乎重新生成AltControl.lnk文件(我知道因为.lnk文件即使没有通过对话框进行功能更改,大小也会增加!?)并且它有效!所以看起来NSIS或负责生成/解析.lnk文件的后台Windows进程在安装过程中没有接受新创建的环境变量。我已经浏览了网页,一切似乎表明我正在使用的SendMessage应该强制所有人都知道新创建的env变量。在此先感谢能够解开这个谜团的人。您可以使用附带的installer.nsi脚本来复制我的情况。另请注意,它在多种环境(XP,Vista,7,有/无管理员等)中表现出这种行为。

1 个答案:

答案 0 :(得分:0)

大多数应用程序不处理该消息广播,它主要用于explorer.exe。

您可以直接在安装程序进程中更新变量,这些变量也将由子进程继承:

System::Call 'Kernel32::SetEnvironmentVariable(t "ENGINE", t "v8")'