NSIS - EnvVarUpdate overwrites system path when path is too long, is there a workaround?

StackOverflow https://stackoverflow.com/questions/21897103

  •  13-10-2022
  •  | 
  •  

Here is my simple code:

!include "EnvVarUpdate.nsh"

Outfile "text.exe"

Section

${EnvVarUpdate} $0 "PATH" "A" "HKLM" "C:\Program Files\something"

SectionEnd

I understand that the "A" argument means this should APPEND the last argument to system path. However, testing this revealed that it overwrote my Path variable. Further tests reveal this is because Path was too long (>1024 chars, per the tutorial).

Is there a "safe" way to append to Path then? I am looking for a function that will append if Path is short enough, otherwise do nothing and report an error, something of that sort. I'm wondering if a standard method of doing this already exists. Thanks!

有帮助吗?

解决方案

We have encountered some problems with path modifications from NSIS installers due to the fact that the default string management is limited to 1024 bytes and that string manipulations involved in path modification is truncating strings to 1024 and that is sometimes braking workstation environment (especially in development hosts where many tools are installed). BTW, There are many nsis built setups in the wild that are suffering from this problem.

We are using some different code derived from the AddToPath function from Path manipulation but the problem stays similar.

The best workaround that we are using until now is by using the special build of NSIS that provides the large string support (of 8kB instead of 1 kB). That build is available in the special builds page of the NSIS wiki.

其他提示

Can you try this?

Section 

ReadEnvStr $0 PATH
StrCpy $0 "$0;C:\Program Files\something"
StrLen $1 $0

${if} $1 < 1024
${EnvVarUpdate} $0 "PATH" "A" "HKLM" "C:\Program Files\something"
${else}
messagebox mb_ok "error writing environment variable"
${endIf}

SectionEnd
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top