Setting an environment variable in the shortcut target - inno-setup

I'm writing a Inno Setup script where I need to create a shortcut that sets an environment variable in the shortcut target. The target string is something like:
cmd.exe /c set ENV_VAR=%CD% && "C:\Program Files\MyApp\MyApp.exe"
But for some reason I cannot fantom the shortcut is never created. I checked the length of the target string: 162 characters, which seems to be below the Windows (?) limit. If I create the shortcut with a target string without the "... &&" prefix and then add it by hand, it works as expected, however. Any help is appreciated.

If you want to set ENV_VAR using inno setup as %CD% and "C:\Program Files\MyApp\MyApp.exe"
you can use ; as like below
C:\Program Files;C:\Winnt;C:\Winnt\System32
this might help you
set ENV_VAR=%CD%;C:\Program Files\MyApp\MyApp.exe
I have used
set ENV_VAR=%PATH%;C:\program files
Checked after listing all,there I found
ENV_VAR=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Linux For Windows\bin;C:\Program Files (x86)\Universal Extractor;C:\Program Files (x86)\Universal Extractor\bin;C:\Program Files (x86)\Calibre2\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Subversion\bin;C:\Program Files (x86)\WinMerge;C:\Program Files
the above environment variable length is 390 characters , so I believe in your case no issue with length.

Related

Signing with full path with spaces to signtool.exe fails with “Sign Tool failed with exit code 0x1” in Inno Script Studio

In Inno Setup, my sign tool is defined as:
cmd /k "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\signtool.exe" sign /t http://timestamp.comodoca.com /n "My Company" $f
Prepending cmd /k (as suggested at Inno Setup - Signing fails with "Sign Tool failed with exit code 0x1") reveals that there is not actually a problem with the sign tool itself, but rather how Inno Setup is calling it, generating the following message:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Clearly, it's chopping off the path to signtool at the first space, even though I put it in quotes. I even tried replacing the quotes with $q to see if that made any difference. (It didn't.) I would think this is improper behavior for the parser to dishonor the quotes, since the instructions clearly say to paste the exact text you'd use on the command line, and the example itself includes quotation marks (https://revolution.screenstepslive.com/s/revolution/m/10695/l/563371-signing-installers-you-create-with-inno-setup).
Everything works beautifully from the command line. For some reason, Inno Setup just isn't properly calling signtool.
ADDED 2020-07-14:
Inno Setup's example, copied from their website:
"C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin\signtool.exe" sign /f "C:\MY_CODE_SIGNING.PFX" /t http://timestamp.comodoca.com/authenticode /p MY_PASSWORD $f
My version, used without the cmd /k:
"C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\signtool.exe" sign /t http://timestamp.comodoca.com /n "My Company" $f
As you can see, we use quotes in the same manner. I will check for updates - I probably should have done that first. When I get some time, I'll revert the signtool definition and see if maybe the update fixes it.
Also, I should have been more clear. I am new to Inno Setup and it didn't occur to me to clarify this. I am compiling via Inno Script Studio. It occurs to me that there's some possibility this has to do with a behavior in a specific version of Script Studio and not the compiler itself, but I don't know enough about the inner workings to do any more than speculate.
Having done a bit more tinkering, I'm now fairly certain this must be a bug with Inno Setup. I implemented a work-around as follows:
Open a command window with elevated privileges
Change to a directory I created with no space in the name: cd \bin
Create a 'spaceless' symbolic link to the directory where signtool.exe is located: mklink /d "Windows10SDK" "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64"
Enter new sign tool definition that uses the symbolic link:
C:\bin\Windows10SDK\signtool.exe sign /t http://timestamp.comodoca.com /n "My Company" $f
And that's it! Because there is no longer a space character in the path to signtool.exe, Inno Setup now performs the signing correctly and everything works beautifully.
I'll report this as a bug and update if needed.
I use:
#define SignedDesc "$q" + MSA + "$q"
#define SignedPfx "$qd:\My Certificate\2018\My Certificate.pfx$q"
#define SignedTimeStamp "$qhttp://timestamp.verisign.com/scripts/timestamp.dll$q"
#define SignedPw "$q~~~~~~~$q"
SignTool=SignTool /d {#SignedDesc} /du $q{#AppURL}$q /f {#SignedPfx} /p {#SignedPw} /t {#SignedTimeStamp} /v $f
My sign tool is configured as:
$qC:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x86\Signtool.exe$q sign $p
You have to use $q which is a quote mark.

How to write Batch script to do something based on windows version

I am fairly new to writing batch scripts and our system has just thrown me a slight curve ball.
We are getting new computers with Windows 10 and Office 16. We also still have Windows 7 and Office 14 PCs are the network that have not been upgraded yet.
I currently have a batch file that does an xcopy on execution that checks an excel file on a server against a version of the same file on the users local machine. If the server version is newer, it copies over the local version then opens and runs the excel file.
I need to update my batch to accomodate both versions of excel when it needs to open and run the file.
I can check either on windows or excel version as they are married together, but I'm open to whichever method is more reliable.
I was playing with WMIC which returns the version of windows reliably on our machines. I tried other scripts from my research on the web and they did not function as I would have liked.
Here is my current code:
set "source=\\s001\FOLDER\EXCELFILE TOOL.xlsm"
set "target=C:\Apps\EXCELFILE TOOL PROGRAM\"
xcopy "%source%" "%target%" /y /d /h /k /r
START "" "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.exe" /e "%target%\EXCELFILE TOOL.xlsm"
EXIT
Before the "START" line, I need to check which version of windows or office is on the machine, then modify the start to be one of these two options:
Option 1
START "" "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.exe" /e "%target%\EXCELFILE TOOL.xlsm"
Option 2
START "" "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.exe" /e "%target%\EXCELFILE TOOL.xlsm"
Any help would be really appreciated, thanks!
If you're just wanting to start Excel, searching directories should be unnecessary.
Once installed Excel would have a registry sub-key created under:
"[HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER]\Software\Microsoft\Windows\CurrentVersion\App Paths"
Most likely named:
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Excel.exe"
Where the Default value data holds the fully qualified path to the application.
With entries under this key, simply entering the applications name should run it:
Start Excel.exe
The REG_SZ data entry can be used with or without its .exe extension.(If necessary, the ShellExecuteEx function adds the extension when searching App Paths subkey).
You could therefore simply use:
Start Excel
If using the method suggested throughout the majority of the comments then you'd need to use Excel.exe as part of your search mask!
The example below, utilises an additional For loop, to allow for checking in both Program Files and Program Files (x86)
#For %%A In ("%ProgramFiles%" "%ProgramFiles(x86)%" 2^>Nul
) Do #For /F "Delims=" %%B In ('Where /R %%A Excel.exe 2^>Nul'
) Do #Start "" "%%~B"
You could of course ignore the outer loop if you're sure that the installed version is always installed to Program Files (x86).
#For /F "Delims=" %%A In ('Where /R "%ProgramFiles(x86)%" Excel.exe 2^>Nul'
) Do #Start "" "%%~A"
The above two examples could be left as one line, if you're not interseted in maintaining lines of no more than 80 characters.

Perforce Diff Excel Files with SSCL

this is a continuation of the following stack overflow question: Perforce diff for excel files
I'd like to use the solution proposed by domalb, which is to use SSCL. I've downloaded the SSCL solution and am able to successfully diff excel files when using the cmd prompt. However, I'm having trouble getting the arguments to work correctly in Perforce.
In perforce, I go to Edit -> Preferences and select "Diff" from the left menu. I then "specify diff application by extension (overrides default). I set the extension to .xlsx, set the application to sscl.exe, and set the arguments to %1 %2 -d="C:\Program Files (x86)\Microsoft Office\Office16\DCF"
However, this does not work. When I turn on verbose logging in the argument list, I see the following error message:
How can I resolve this error when passing in the SSCL arguments? It looks like it cuts off the directory at the first space, so it is not respecting the double quotes? Thanks for your help!

Start Excel file from Windows batch script in safemode, use default file association

Question Summary:
Can I start Excel file Installer.xlsm from Windows batch script in safemode, without providing EXCEL.EXE installation path?
Details
I have a windows batch script which downloads the latest versions of a family of Excel Add-ins from a remote server, places them in a directory (C:\appname\AddIns) and calls the Excel file Installer.xlsm.
Upon loading, Installer.xlsm executes a VBA macro, which uninstalls older versions of the add-ins and installs their newer version.
Currently I start Installer.xlsm using the command:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm"
What's great about it is that it uses Windows' file association to open Excel, and I don't have to provide the EXCEL.EXE installation path (multiple users with different machine images and MS Office versions).
Now I'd like to load Installer.xlsm in safemode, to make sure that no add-ins are loaded and no other code is run while Installer.xlsm tries to work with the add-ins.
I know I can use "<PathToExcel>excel" /safemode "<PathToXls>Installer.xlsm" as described in this answer, but this method doesn't use Windows' file association and requires that I provide a path.
We have users with various machine images, using different versions of MS Office, so I do not want to get into hardcoding all possible Excel installation locations.
Can I do something of the following form:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm /safemode"
I tried different possible combinations without success. How would you do it?
First I suggest to read the Microsoft documentation page Application Registration. It explains how the installer of an application or an application suite like Microsoft Office should register the installed application(s) so that the executable(s) of the application(s) can be found by other applications.
Recommended is creating under registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
a subkey with name of the executable file like excel.exe with default string value being name of the executable with full path and optionally adding one more string value with name Path containing just the path to the executable. The Path string can but most not exist and it can but must not end with a backslash.
The command START uses also this key to find an application as explained in answer on Where is “START” searching for executables?
The installers of the various versions of Microsoft Office register excel.exe key under this key too.
So the easiest method on Windows Vista and later Windows versions to get installation location of Microsoft Excel is:
#echo off
for /F "skip=1 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
But on Windows XP the output of reg.exe is different and requires for that reason this batch code:
#echo off
for /F "skip=3 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
The different outputs are explained in answer on Read words separated by space & string value also contains space in a batch script in batch code written to get string value of a default string of a registry key containing spaces.
And it is good coding practice to add extra code which handles an error case like registry key does not exist at all because Microsoft Excel is not installed at all.
But is it possible with batch code to do what command START respectively the Windows shell function ShellExecuteEx does on using in a command prompt window the command line?
start "Launching installer file" "C:\Path to file\Installer.xlsm"
Yes, it is possible as the commented batch code below demonstrates.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem First query default string value of HKEY_CLASSES_ROOT\.xlsm from registry.
call :GetDefaultRegValue "HKCR\.xlsm"
rem Is there no key HKEY_CLASSES_ROOT\.xlsm or was the default string empty?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem Get the shell command line used for opening a *.xlsm file.
call :GetDefaultRegValue "HKCR\%RegValue%\shell\open\command"
rem Could the command line not read successfully from Windows registry?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem The command line contains as first string usually enclosed in double
rem quotes EXCEL.EXE with full path enclosed in double quotes. And there
rem can be even more arguments on the command line which are not needed
rem here. The command line below is used to get just first string of
rem the command line which should be EXCEL.EXE with full path.
for %%I in (%RegValue%) do set "RegValue=%%~I" & goto CheckExcelExistence
rem It is not good when both registry queries above fail. This means
rem either Microsoft Excel is not installed at all or a version of
rem Excel is installed which does not support *.xlsm files like Excel
rem of MS Office 2003, MS Office 2000 or MS Office 97.
rem However, perhaps just *.xlsm is not correct registered and therefore
rem get full path to excel.exe from application registration key.
:GetFromAppPaths
call :GetDefaultRegValue "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe"
if defined RegValue goto CheckExcelExistence
echo Failed to determine installation location of Microsoft Excel.
echo/
endlocal
pause
goto :EOF
:CheckExcelExistence
SET RegValue
rem Remove surrounding double quotes if the Excel executable file name
rem read from Windows registry is still enclosed in double quotes.
set "RegValue=%RegValue:"=%"
if exist "%RegValue%" goto :RunInstall
echo Registered "%RegValue%" does not exist.
echo/
endlocal
pause
goto :EOF
:RunInstall
SET RegValue
ECHO start "Launching installer file" /wait "%RegValue%" "%~dp0Installer.xlsm" /safemode
endlocal
goto :EOF
rem This subroutine queries from Windows registry the default string value of
rem the key passed to the subroutine as first and only parameter and assigns
rem this value to environment variable RegValue. Environment variable RegValue
rem is deleted and therefore is not defined after subroutine exits on failure
rem to get the registry value or when the default value is an empty string.
rem This subroutine works for Windows XP and all later versions of Windows.
:GetDefaultRegValue
set "TypeToken=2"
:Reg3Run
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if not "%%~B" == "" (
set "RegValue=%%B"
goto :EOF
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto Reg3Run
)
)
set "RegValue="
goto :EOF
This batch code is just a demonstration. It does not start Excel when really found. Instead it just outputs the command line which would start Excel because of ECHO left of start ... in block below label RunInstall.
Further this batch code contains 4 lines with just SET RegValue. Those 4 lines output just the string value queried successfully from Windows registry and stored in environment variable RegValue. Those 4 commands help to understand what happens on execution of the batch file. Those four command lines should be deleted finally from batch file and also the single ECHO written in upper case.
Note: It is quite easy to test what happens if an expected registry key does not exist or its default value is an empty string. Just insert a single character like # before last double quote on a line starting with call :GetDefaultRegValue and the modified registry key is not found anymore.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
setlocal /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg.exe command line with using a separate command process started in background.

How to add a default folder always with target installation using NSIS

I want to append a default directory to my INSTALL directory always internally.
I have set it by default but if user change the directory then it not works and installed the files in the folder that user selected.
So I need append a folder ( Product name ) always internally and it can be append with the user's selected path.
Like if user selected "C:\Program Files\My Folder" then the installation should be in path "C:\Program Files\My Folder\ProductName"
Should work with the silent as well.
Can someone please advise on this.
Section MyFirstSection
StrCpy $InstDir "$InstDir\ProductName" ; Force extra sub-directory
; ...
SectionEnd
However, the recommended way to do this is to just use InstallDir without a ending backslash:
Note that the part of this string following the last \ will be used if the user selects 'browse', and may be appended back on to the string at install time (to disable this, end the directory with a \ (which will require the entire parameter to be enclosed with quotes).

Resources