READ THE ENVIRONMENT VARIABLE DURING THE COMPILE TIME IN NSIS - nsis

HI Everyone As i was working with the NSIS code, i need to get the environment variable value during the compile time, I Found this code written in somewhere But am not getting understand what does 'Foo' means. & How can we execute the below code in compile time, in my compiler Am not getting the environment variable value with the below following code, Can anybody give a brief example about where we can execute this & How it works?
Edit : As you requested i was inserting the link of Code referred from this forum "https://stackoverflow.com/questions/22149007/nsis-how-to-check-at-compile-time-if-an-environment-variable-exists"
; NSIS 2+
!define DOLLAR $
!if "$%foo%" == "${DOLLAR}%foo%"
!echo "%foo% not set"
!endif
; NSIS 3+
!if "$%foo%" == "${U+24}%foo%"
!echo "%foo% not set"
!endif

This code tries to check if a environment variable has been set (on the computer where the compiler is executing). If it has not been set the compiler just returns the raw string (like a batch file does). The code you found escapes the $ to check if the raw string is returned.
Foo is just a placeholder name, meaning the %Foo% variable. If you want to check %windir% for example, use !if "$%windir%" == "${U+24}%windir%".
If you just want the value, just use $%windir%:
ReadEnvStr $0 WINDIR
MessageBox MB_OK "%windir% was $%windir% when this installer was compiled. On this machine it is $0"

Related

Precedence of !define and /D - How can I set an overridable default value?

I'm building an installer for a system that is generally installed by my coworkers, but several things can vary from one target machine to the next, including the locations of the files-to-be-installed on the compiling machine. I thought I could set a "gflag" in the source (with !define) and override it in the call to makensis.exe (with /D), but I can't even get my installer to recognize that a /D flag was passed.
More relevant documentation is behind the /h flag:
PS C:\> &"C:\Program Files (x86)\NSIS\makensis.exe" /h
Usage:
makensis [ option | script.nsi | - ] [...]
Options:
#...
/Ddefine[=value] defines the symbol "define" for the script [to value]
#...
I'm using this NSIS code:
!ifndef test
!define test Foo
!endif
Section
DetailPrint "${test}"
SectionEnd
I compile the installer in PowerShell:
&"C:\Program Files (x86)\NSIS\makensis.exe" "C:\path\to\test.nsi" /Dtest=Bar
Among the output, I see this:
Command line defined: "test=Bar"
The installer is successfully created, but it prints Foo while it should print Bar. If I comment !define test Foo, I get a warning when compiling:
1 warning:
unknown variable/constant "{test}" detected, ignoring (C:\Mach4\Installer\test.nsi:6)
And then ${test} is printed, indicating that the gflag has no value. Why does it not have a value of Bar?
The command line is parsed in order. In your case that means the script is parsed before /Dtest=Bar is parsed. Try this instead:
"C:\Program Files (x86)\NSIS\makensis.exe" /Dtest=Bar "C:\path\to\test.nsi"

Nsis doesn't see environment variables

Here is the part os nsis script (.nsi):
!ifndef QTDIR
!error "Please define QT installation directory via /DQTDIR=C:\qt\4.8.4"
!endif
But after executing this command:
set QTDIR=C:\path\to\qt
the erorr still occurs. The same result on two computers, both windows 7. Nsis version is 2.46 .
!ifdef and !ifndef operate on defines internally in the compiler process. You can set one in your script with !define or use the -D MakeNSIS command line argument.
MakeNSIS can also read Windows environment variables: !echo "The value of QTDIR is $%QTDIR%".
You can also support both:
!ifndef QTDIR
!define QTDIR "$%QTDIR%"
!endif
!if ! /fileexists "${QTDIR}"
!error "QTDIR not valid"
!endif

NSIS concatenate string with slash

I'm trying to build a string with this expression bla://${foo}/bar where ${foo} == "hostname". The expected result is bla://hostname/bar but I get http://${foo}/bar instead.
So I figure there is something special about the combination /$ but I can't figure it out.
Thanks for your help
/$ is not special but a define that does not exist ends up as ${definename}.
DetailPrint "${foo}" ; Prints ${foo}
!define foo example.com
DetailPrint "${foo}" ; Prints example.com
All defines are resolved at compile time, if this hostname is something the user could configure on a custom page you need to use a NSIS variable...

NSIS: environment variable created doesn't work

I am creating a Env Variable and then appending it to the PATH variable in my NSIS installer script on Win7 - 64 bit.
Push "MyPath"
Push "D:\MyFolder\bin;D:\MyFolder\lib"
Call WriteEnvStr
Push "%MyPath%"
Call AddToPath
Now after installation I can see both the variable in cmd prompt
MyPath=D:\MyFolder\bin;D:\MyFolder\lib
Path=%MyPath%;<my existsing path>
But any exe/dll from are not found at run time.
If I run the cmd prompt as Administrator and then run exe, it runs fine.
It also works if I run exe directly as an Administrator.
Also interesting is that if I open Environment editor, double click on MyPath variable and click OK (without changing anything), my exe's run fine without running as Administrator.
And now if I check Path in cmd prompt, MyPath variable is substituted
MyPath=D:\MyFolder\bin;D:\MyFolder\lib
Path=D:\MyFolder\bin;D:\MyFolder\lib;<my existsing path>
I tried to add "Call EnvVarUpdate" after creating the env variable but it doesn't work.
I am using NSIS Unicode version 2.46.3
Your chances of adding/modifying the path without losing data in path by truncating it would be much greater by using the registry.
ReadRegStr $0 HKCU "Environment" Path
StrCpy $1 "D:\MyFolder\bin;D:\MyFolder\lib"
StrCpy $2 "$0;$1"
WriteRegStr HKCU "Environment" Path "$2"
If your system has an AUTOEXEC.BAT file then any PATH setting in AUTOEXEC.BAT will also be appended to the %PATH% environment variable. This is to provide compatibility with old installation routines which need to set the PATH. All other commands in AUTOEXEC.BAT are ignored however. And this is more or less obsolete anyway.
But we could just continue with your method however just try a different means of acomplishing your goal. I have not tested this but you can try something similar to this:
StrCpy $R0 "MYPATH"
StrCpy $R1 "D:\MyFolder\bin;D:\MyFolder\lib"
System::Call `Kernel32::SetEnvironmentVariable(t"$R0",t"$R1")`
Now include your new variable in the path like the following:
ReadEnvStr $R0 COMSPEC
ReadEnvStr $R1 MYPATH
ExecDos::Exec /TOSTACK `"$R0" /c "SetX PATH=%PATH%;$R1 -m"`
You can now easily change that one variable %MYPATH% at any time in the future and the PATH will reflect the new value.
${EnvVarUpdate} $0 "PATH" "A" "HKLM" "C:\Program Files\Java\jre6\bin"
StrCpy $R0 "$0"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("PATH", R0).r2'
ReadEnvStr $R0 "PATH"
ExecWait "$INSTDIR\tmp\batchfile.bat

How do I conditionally compile an NSIS script based on commandline parameters?

I'm trying to generalize a setup file by externally passing the version number. I want to be able to do this:
makensis myscript.nsi parameter=value
and then read the parameter within the script, so that using the same script I can generate differently versioned executables. I found this & this, but they seem to be for passing commandline parameters to the generated setup.exe. Is this possible, and how?
You can add symbols to the globally defined list from the command line using the /D switch:
makensis /DMyVersion="1.0.1" install.nsi
Then you can use them using the ${} syntax:
!ifdef MyVersion
StrCpy $Version "${MyInstallerName}"
!else
StrCpy $Version "1.0.0"
!endif
Also of possible interest is the GetVersion plugin discussed in this SO question: NSIS - put EXE version into name of installer

Resources