NSIS 安装程序说运行时环境变量为空

问题描述 投票:0回答:1

创建 basic.nsi 以使用 ReadEnvStr 在运行时读取环境变量:

!include "LogicLib.nsh"
Outfile "TestInstaller.exe"
Name "Test Installer"
InstallDir $DESKTOP\MyApp

Section "Install"
    ReadEnvStr $R0 "MY_VAR"
    ${If} $R0 == ""
        MessageBox MB_OK "MY_VAR is not set or is empty"
    ${Else}
        MessageBox MB_OK "MY_VAR value: $R0"
    ${EndIf}
SectionEnd

然后在powershell中

> makensis basic.nsi
> set MY_VAR=1
> ./TestInstaller.exe

给出“MY_VAR 未设置或为空”。同样,如果我尝试使用系统调用:

Outfile "TestInstaller.exe"
Name "Test Installer"
InstallDir $DESKTOP\MyApp

Section "Install"
    System::Call 'kernel32::GetEnvironmentVariableA(t "MY_VAR", t .R0, i ${NSIS_MAX_STRLEN}) i .r1'

    IntCmp $R1 0 env_not_found env_found

    env_not_found:
        MessageBox MB_OK "MK_UPDATE_INSTALLER is not set or is empty"
        Goto end

    env_found:
        MessageBox MB_OK "MK_UPDATE_INSTALLER value: $R0"

    end:
SectionEnd

如果我尝试循环每个环境变量,它会说没有:

Outfile "PrintEnvVarsInstaller.exe"
Name "Print Env Vars Installer"
InstallDir $DESKTOP\MyApp

Section "Install"
    ; Get a pointer to the environment block
    System::Call 'kernel32::GetEnvironmentStringsA() p .r0'

    ; Initialize a pointer variable to iterate over the environment block
    StrCpy $R1 $R0

    loop:
    ; Get the current string pointed to by $R1
    System::Call '*$R1(&t0 .R2)'

    ; Check if we reached the end of the environment block (null string)
    StrCmp $R2 "" done

    ; Display the current environment variable
    MessageBox MB_OK "$R2"

    ; Move the pointer to the next string (environment variable)
    System::Call 'kernel32::lstrlenA(t $R2) i .r3'
    IntOp $R1 $R1 + $R3
    IntOp $R1 $R1 + 1

    ; Repeat the loop
    Goto loop

    done:
    ; Free the environment strings block
    System::Call 'kernel32::FreeEnvironmentStringsA(p $R0) i .r1'

    MessageBox MB_OK "done"

SectionEnd

立即给出“完成”。 Python

os.getenv
Write-Host "MY_VAR is set to: $env:MY_VAR"
可以很好地检测环境变量。

windows powershell environment-variables environment nsis
1个回答
0
投票

奇怪的是没有错误。

$env:MY_VAR=1
就是这样。否则,使用
set-variable
,您将创建一个名为
MY_VAR=1
的没有值的变量。添加
-verbose
有帮助,并且
dir variable:

set my_var=1 -verbose

VERBOSE: Performing the operation "Set variable" on target "Name: my_var=1 Value: ".


dir variable:my_*

Name                           Value
----                           -----
my_var=1


${my_var=1}

<# no output #>
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.