好的,我有我的 nsi 脚本的简化版本(见下文)。在 A2 部分,我将单个可执行文件复制到指定的安装路径,创建环境变量,然后执行 SendMessage,这应该让所有当前正在运行的进程都知道环境变化。但是,NSIS 安装程序进程本身似乎没有更新,因为我在“链接”部分创建的快捷方式不起作用
安装程序.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 应该强制所有东西都知道新创建的环境变量。在此先感谢能解开这个谜团的人。您可以使用包含的 installer.nsi 脚本来复制我的情况。另请注意,它在多种环境(XP、Vista、7、有/无管理员等)中表现出这种行为。