javawindows-installerinno-setupinno-setup-v6

Environment Variable Set Successfully but Not Updating After Setting in Inno Setup Script


Hello Stack Overflow community,

I'm facing an issue with an Inno Setup script and need some assistance. During the installation of an executable (Exe), I'm required to set up an environment variable for MongoDB. My objective is to create a Windows service for MongoDB, but before doing so, I need to add an environment variable for it. To achieve this, I've followed the following steps in my Inno Setup script:

  1. In the CurStepChanged event for ssInstall, I'm adding the MongoDB environment variable using EnvRemoveAddPath function. The environment variable appears to be set correctly.

  2. In the CurStepChanged event for ssPostInstall, I'm refreshing the environment variables using the RefreshEnvironment function.

  3. I proceed to perform the necessary logic, including creating the Windows service for MongoDB.

However, when I attempt to configure MongoDB using the command mongod --dbpath <{app}\database\path> during the service configuration, I encounter the error message: 'mongod' is not recognized as an internal or external command, operable program or batch file.

Strangely, after this error occurs, I manually check the environment variable settings for MongoDB, and they appear to be set correctly. Furthermore, when I use the echo %path% command in the CMD, I can see that the MongoDB variable is indeed present. Despite this, during the execution of the installer, the error persists.

Can anyone help me understand why this issue is occurring and how I can resolve it? Thank you for your assistance.

I provided my code please check it.

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "envVarIssue"
#define MyAppVersion "1.5"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "https://www.example.com/"
#define MyAppExeName "MyProg.exe"
#define MyAppAssocName MyAppName + " File"
#define MyAppAssocExt ".myp"
#define MyAppAssocKey StringChange(MyAppAssocName, " ", "") + MyAppAssocExt

#define MyAppParentPath "."

[Setup]
SetupLogging=yes
ChangesEnvironment=yes

; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{D2C055CB-72E7-4DB9-83EE-34D399939D0C}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
ChangesAssociations=yes
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
PrivilegesRequired=admin
OutputBaseFilename=envVarIssue App
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Program Files (x86)\Inno Setup 6\Examples\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#MyAppParentPath}\thirdparty\mongodb\*"; DestDir: "{app}\mongodb"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Registry]
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocExt}\OpenWithProgids"; ValueType: string; ValueName: "{#MyAppAssocKey}"; ValueData: ""; Flags: uninsdeletevalue
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}"; ValueType: string; ValueName: ""; ValueData: "{#MyAppAssocName}"; Flags: uninsdeletekey
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName},0"
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\{#MyAppExeName}"" ""%1"""
Root: HKA; Subkey: "Software\Classes\Applications\{#MyAppExeName}\SupportedTypes"; ValueType: string; ValueName: ".myp"; ValueData: ""

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
; Run MongoDB mongod command
Filename: "{cmd}"; Parameters: "/C mongod >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: waituntilterminated runascurrentuser;

[Code]
const
  SMTO_ABORTIFHUNG = 2;
  WM_WININICHANGE = $001A;
  WM_SETTINGCHANGE = WM_WININICHANGE;

type
  WPARAM = UINT_PTR;
  LPARAM = INT_PTR;
  LRESULT = INT_PTR;

function SendTextMessageTimeout(hWnd: HWND; Msg: UINT;
  wParam: WPARAM; lParam: PAnsiChar; fuFlags: UINT;
  uTimeout: UINT; out lpdwResult: DWORD): LRESULT;
  external 'SendMessageTimeoutA@user32.dll stdcall';

procedure RefreshEnvironment;
var
  S: AnsiString;
  MsgResult: DWORD;
begin
  S := 'Environment';
  SendTextMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    PAnsiChar(S), SMTO_ABORTIFHUNG, 5000, MsgResult);
end;

const EnvironmentKey = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
procedure EnvRemoveAddPath(Path: string);
var
    Paths: string;
 
begin
         { Retrieve current path (use empty string if entry not exists) }
    if not RegQueryStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths)
    then Paths := '';

    { Skip if string already found in path }
    if Pos(';' + Uppercase(Path) + ';', ';' + Uppercase(Paths) + ';') > 0 then exit;

    { App string to the end of the path variable }
    Paths := Paths + ';' + Path;

    { Overwrite (or create if missing) path environment variable }
    if RegWriteStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths)
    then Log(Format('The [%s] added to PATH: [%s]', [Path, Paths]))
    else Log(Format('Error while adding the [%s] to PATH: [%s]', [Path, Paths]));
end;

procedure DebugOutput(Msg: string);
begin
  Log('Debug: ' + Msg);
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    DebugOutput('before set env PATH: ' + GetEnv('Path'));
    EnvRemoveAddPath(ExpandConstant('{app}\mongodb\bin'));
  end
  else if CurStep = ssPostInstall then
  begin
    // Refresh environment variables
    RefreshEnvironment;
    DebugOutput('after set env PATH: ' + GetEnv('Path'));
  end;
end;

additionally you can see my debug logs

2023-08-31 18:08:37.671   Log opened. (Time zone: UTC+05:30)
2023-08-31 18:08:37.674   Setup version: Inno Setup version 6.2.2
2023-08-31 18:08:37.674   Original Setup EXE: C:\Users\MyUser\Downloads\env-var-issue\Output\envVarIssue App.exe
2023-08-31 18:08:37.675   Setup command line: /SL5="$310A3C,70759185,832512,C:\Users\MyUser\Downloads\env-var-issue\Output\envVarIssue App.exe" /SPAWNWND=$31051C /NOTIFYWND=$340A92 /DEBUGWND=$2062A 
2023-08-31 18:08:37.676   Windows version: 10.0.19045  (NT platform: Yes)
2023-08-31 18:08:37.678   64-bit Windows: Yes
2023-08-31 18:08:37.679   Processor architecture: x64
2023-08-31 18:08:37.680   User privileges: Administrative
2023-08-31 18:08:37.684   Administrative install mode: Yes
2023-08-31 18:08:37.685   Install mode root key: HKEY_LOCAL_MACHINE
2023-08-31 18:08:37.688   64-bit install mode: No
2023-08-31 18:08:37.694   Created temporary directory: C:\Users\MyUser\AppData\Local\Temp\is-2S2FL.tmp
2023-08-31 18:08:37.698   -- DLL function import --
2023-08-31 18:08:37.700   Function name: SendMessageTimeoutA
2023-08-31 18:08:37.701   DLL name: user32.dll
2023-08-31 18:08:37.704   Dest DLL name: user32.dll
2023-08-31 18:08:37.706   Importing the DLL function.
2023-08-31 18:08:37.707   Successfully imported the DLL function. Delay loaded? No
2023-08-31 18:08:40.591   Found 12 files to register with RestartManager.
2023-08-31 18:08:40.594   Calling RestartManager's RmGetList.
2023-08-31 18:08:40.606   RmGetList finished successfully.
2023-08-31 18:08:40.609   RestartManager found no applications using one of our files.
2023-08-31 18:08:40.629   Debug: before set env PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Java\jdk-14.0.2\bin;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_241\bin;C:\Program Files\dotnet\;C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps
2023-08-31 18:08:40.653   The [C:\Program Files (x86)\envVarIssue\mongodb\bin] added to PATH: [%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files\Java\jdk-14.0.2\bin;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_241\bin;C:\Program Files\dotnet\;;C:\Program Files (x86)\envVarIssue\mongodb\bin]
2023-08-31 18:08:41.004   Starting the installation process.
2023-08-31 18:08:41.015   Creating directory: C:\Program Files (x86)\envVarIssue
2023-08-31 18:08:41.018   Directory for uninstall files: C:\Program Files (x86)\envVarIssue
2023-08-31 18:08:41.020   Creating new uninstall log: C:\Program Files (x86)\envVarIssue\unins000.dat
2023-08-31 18:08:41.023   -- File entry --
2023-08-31 18:08:41.024   Dest filename: C:\Program Files (x86)\envVarIssue\unins000.exe
2023-08-31 18:08:41.026   Time stamp of our file: 2023-08-31 18:08:37.149
2023-08-31 18:08:41.029   Installing the file.
2023-08-31 18:08:41.034   Successfully installed the file.
2023-08-31 18:08:41.036   -- File entry --
2023-08-31 18:08:41.041   Dest filename: C:\Program Files (x86)\envVarIssue\MyProg.exe
2023-08-31 18:08:41.042   Time stamp of our file: 2020-07-04 05:30:00.000
2023-08-31 18:08:41.045   Installing the file.
2023-08-31 18:08:41.048   Successfully installed the file.
2023-08-31 18:08:41.050   -- File entry --
2023-08-31 18:08:41.058   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\LICENSE-Community.txt
2023-08-31 18:08:41.060   Time stamp of our file: 2020-11-16 15:25:26.000
2023-08-31 18:08:41.063   Installing the file.
2023-08-31 18:08:41.065   Creating directory: C:\Program Files (x86)\envVarIssue\mongodb
2023-08-31 18:08:41.071   Successfully installed the file.
2023-08-31 18:08:41.074   -- File entry --
2023-08-31 18:08:41.076   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\MPL-2
2023-08-31 18:08:41.079   Time stamp of our file: 2020-11-16 15:25:26.000
2023-08-31 18:08:41.081   Installing the file.
2023-08-31 18:08:41.084   Successfully installed the file.
2023-08-31 18:08:41.087   -- File entry --
2023-08-31 18:08:41.090   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\README
2023-08-31 18:08:41.091   Time stamp of our file: 2020-11-16 15:25:26.000
2023-08-31 18:08:41.093   Installing the file.
2023-08-31 18:08:41.094   Successfully installed the file.
2023-08-31 18:08:41.096   -- File entry --
2023-08-31 18:08:41.098   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\THIRD-PARTY-NOTICES
2023-08-31 18:08:41.099   Time stamp of our file: 2020-11-16 15:25:34.000
2023-08-31 18:08:41.101   Installing the file.
2023-08-31 18:08:41.104   Successfully installed the file.
2023-08-31 18:08:41.105   -- File entry --
2023-08-31 18:08:41.107   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\bsondump.exe
2023-08-31 18:08:41.108   Time stamp of our file: 2020-11-12 22:03:14.000
2023-08-31 18:08:41.109   Installing the file.
2023-08-31 18:08:41.110   Creating directory: C:\Program Files (x86)\envVarIssue\mongodb\bin
2023-08-31 18:08:41.622   Successfully installed the file.
2023-08-31 18:08:41.625   -- File entry --
2023-08-31 18:08:41.628   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\Install-Compass.ps1
2023-08-31 18:08:41.631   Time stamp of our file: 2020-11-16 16:37:00.000
2023-08-31 18:08:41.633   Installing the file.
2023-08-31 18:08:41.637   Successfully installed the file.
2023-08-31 18:08:41.639   -- File entry --
2023-08-31 18:08:41.641   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongo.exe
2023-08-31 18:08:41.642   Time stamp of our file: 2020-11-16 16:32:42.000
2023-08-31 18:08:41.643   Installing the file.
2023-08-31 18:08:42.232   Successfully installed the file.
2023-08-31 18:08:42.234   -- File entry --
2023-08-31 18:08:42.237   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongod.exe
2023-08-31 18:08:42.240   Time stamp of our file: 2020-11-16 16:32:04.000
2023-08-31 18:08:42.243   Installing the file.
2023-08-31 18:08:43.370   Successfully installed the file.
2023-08-31 18:08:43.373   -- File entry --
2023-08-31 18:08:43.374   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongodump.exe
2023-08-31 18:08:43.376   Time stamp of our file: 2020-11-12 22:03:20.000
2023-08-31 18:08:43.377   Installing the file.
2023-08-31 18:08:44.038   Successfully installed the file.
2023-08-31 18:08:44.040   -- File entry --
2023-08-31 18:08:44.042   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongoexport.exe
2023-08-31 18:08:44.050   Time stamp of our file: 2020-11-12 22:03:38.000
2023-08-31 18:08:44.055   Installing the file.
2023-08-31 18:08:44.720   Successfully installed the file.
2023-08-31 18:08:44.724   -- File entry --
2023-08-31 18:08:44.727   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongofiles.exe
2023-08-31 18:08:44.729   Time stamp of our file: 2020-11-12 22:03:54.000
2023-08-31 18:08:44.732   Installing the file.
2023-08-31 18:08:45.374   Successfully installed the file.
2023-08-31 18:08:45.377   -- File entry --
2023-08-31 18:08:45.384   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongoimport.exe
2023-08-31 18:08:45.386   Time stamp of our file: 2020-11-12 22:03:32.000
2023-08-31 18:08:45.388   Installing the file.
2023-08-31 18:08:46.027   Successfully installed the file.
2023-08-31 18:08:46.031   -- File entry --
2023-08-31 18:08:46.032   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongorestore.exe
2023-08-31 18:08:46.034   Time stamp of our file: 2020-11-12 22:03:26.000
2023-08-31 18:08:46.035   Installing the file.
2023-08-31 18:08:46.715   Successfully installed the file.
2023-08-31 18:08:46.720   -- File entry --
2023-08-31 18:08:46.722   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongos.exe
2023-08-31 18:08:46.724   Time stamp of our file: 2020-11-16 16:06:30.000
2023-08-31 18:08:46.725   Installing the file.
2023-08-31 18:08:47.483   Successfully installed the file.
2023-08-31 18:08:47.487   -- File entry --
2023-08-31 18:08:47.490   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongostat.exe
2023-08-31 18:08:47.493   Time stamp of our file: 2020-11-12 22:03:44.000
2023-08-31 18:08:47.497   Installing the file.
2023-08-31 18:08:48.194   Successfully installed the file.
2023-08-31 18:08:48.197   -- File entry --
2023-08-31 18:08:48.200   Dest filename: C:\Program Files (x86)\envVarIssue\mongodb\bin\mongotop.exe
2023-08-31 18:08:48.203   Time stamp of our file: 2020-11-12 22:03:48.000
2023-08-31 18:08:48.206   Installing the file.
2023-08-31 18:08:48.866   Successfully installed the file.
2023-08-31 18:08:48.870   -- Icon entry --
2023-08-31 18:08:48.879   Dest filename: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\envVarIssue.lnk
2023-08-31 18:08:48.881   Creating the icon.
2023-08-31 18:08:48.918   Successfully created the icon.
2023-08-31 18:08:48.938   -- Registry entry --
2023-08-31 18:08:48.942   Key: HKEY_LOCAL_MACHINE\Software\Classes\.myp\OpenWithProgids
2023-08-31 18:08:48.945   Value name: envVarIssueFile.myp
2023-08-31 18:08:48.946   Creating or opening the key.
2023-08-31 18:08:48.947   Creating or setting the value.
2023-08-31 18:08:48.948   Successfully created or set the value.
2023-08-31 18:08:48.950   -- Registry entry --
2023-08-31 18:08:48.955   Key: HKEY_LOCAL_MACHINE\Software\Classes\envVarIssueFile.myp
2023-08-31 18:08:48.957   Creating or opening the key.
2023-08-31 18:08:48.958   Creating or setting the value.
2023-08-31 18:08:48.959   Successfully created or set the value.
2023-08-31 18:08:48.962   -- Registry entry --
2023-08-31 18:08:48.966   Key: HKEY_LOCAL_MACHINE\Software\Classes\envVarIssueFile.myp\DefaultIcon
2023-08-31 18:08:48.968   Creating or opening the key.
2023-08-31 18:08:48.969   Creating or setting the value.
2023-08-31 18:08:48.970   Successfully created or set the value.
2023-08-31 18:08:48.972   -- Registry entry --
2023-08-31 18:08:48.978   Key: HKEY_LOCAL_MACHINE\Software\Classes\envVarIssueFile.myp\shell\open\command
2023-08-31 18:08:48.979   Creating or opening the key.
2023-08-31 18:08:48.981   Creating or setting the value.
2023-08-31 18:08:48.983   Successfully created or set the value.
2023-08-31 18:08:48.985   -- Registry entry --
2023-08-31 18:08:48.990   Key: HKEY_LOCAL_MACHINE\Software\Classes\Applications\MyProg.exe\SupportedTypes
2023-08-31 18:08:48.991   Value name: .myp
2023-08-31 18:08:48.995   Creating or opening the key.
2023-08-31 18:08:48.997   Creating or setting the value.
2023-08-31 18:08:48.999   Successfully created or set the value.
2023-08-31 18:08:49.002   Saving uninstall information.
2023-08-31 18:08:49.004   Creating new uninstall key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\{D2C055CB-72E7-4DB9-83EE-34D399939D0C}_is1
2023-08-31 18:08:49.005   Writing uninstall key values.
2023-08-31 18:08:49.007   Detected previous non administrative install? No
2023-08-31 18:08:49.011   Detected previous administrative 64-bit install? No
2023-08-31 18:08:49.042   Installation process succeeded.
2023-08-31 18:08:49.054   -- Run entry --
2023-08-31 18:08:49.059   Run as: Current user
2023-08-31 18:08:49.061   Type: Exec
2023-08-31 18:08:49.063   Filename: C:\Windows\system32\cmd.exe
2023-08-31 18:08:49.064   Parameters: /C mongod >> "C:\Program Files (x86)\envVarIssue\mongo_logfile.txt" 2>&1
2023-08-31 18:08:49.141   Process exit code: 1
2023-08-31 18:08:49.356   Debug: after set env PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Java\jdk-14.0.2\bin;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_241\bin;C:\Program Files\dotnet\;C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps
2023-08-31 18:08:49.594   Need to restart Windows? No
2023-08-31 18:08:55.852   -- Run entry --
2023-08-31 18:08:55.858   Run as: Original user
2023-08-31 18:08:55.860   Type: Exec
2023-08-31 18:08:55.861   Filename: C:\Program Files (x86)\envVarIssue\MyProg.exe
2023-08-31 18:08:55.914   Deinitializing Setup.
2023-08-31 18:08:55.935   Log closed.


I'm tries to create windows service by below way:

; Install MongoDB and configure its service
Filename: "{cmd}"; Parameters: "/C mongod --dbpath ""{app}\database\data\db"" --logpath ""{app}\database\log\mongo.log"" --wiredTigerCacheSizeGB 0.65 --auth --install --serviceName mytestappdatabase >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: waituntilterminated runascurrentuser;
Filename: "{cmd}"; Parameters: "/C sc start mytestappdatabase >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: waituntilterminated runascurrentuser;
Filename: "{cmd}"; Parameters: "/C sc config mytestappdatabase DisplayName=mytestapp-database-server >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: waituntilterminated runascurrentuser;


Script - 2

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "envVarIssue"
#define MyAppVersion "1.5"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "https://www.example.com/"
#define MyAppExeName "MyProg.exe"
#define MyAppAssocName MyAppName + " File"
#define MyAppAssocExt ".myp"
#define MyAppAssocKey StringChange(MyAppAssocName, " ", "") + MyAppAssocExt

#define MyAppParentPath "."

[Setup]
SetupLogging=yes
ChangesEnvironment=yes

; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{D2C055CB-72E7-4DB9-83EE-34D399939D0C}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
ChangesAssociations=yes
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
PrivilegesRequired=admin
OutputBaseFilename=envVarIssue App
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Program Files (x86)\Inno Setup 6\Examples\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#MyAppParentPath}\thirdparty\mongodb\*"; DestDir: "{app}\mongodb"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Registry]
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocExt}\OpenWithProgids"; ValueType: string; ValueName: "{#MyAppAssocKey}"; ValueData: ""; Flags: uninsdeletevalue
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}"; ValueType: string; ValueName: ""; ValueData: "{#MyAppAssocName}"; Flags: uninsdeletekey
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName},0"
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\{#MyAppExeName}"" ""%1"""
Root: HKA; Subkey: "Software\Classes\Applications\{#MyAppExeName}\SupportedTypes"; ValueType: string; ValueName: ".myp"; ValueData: ""
Root: HKLM; \
    Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; \
    ValueType: string; ValueName: "PATH"; ValueData: "{olddata};{app}\mongodb\bin"; \
    AfterInstall: RefreshEnvironment;

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
; Run MongoDB mongod command
Filename: "{cmd}"; Parameters: "/C mongod >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: waituntilterminated runascurrentuser;

[Code]
const
  SMTO_ABORTIFHUNG = 2;
  WM_WININICHANGE = $001A;
  WM_SETTINGCHANGE = WM_WININICHANGE;

type
  WPARAM = UINT_PTR;
  LPARAM = INT_PTR;
  LRESULT = INT_PTR;

function SendTextMessageTimeout(hWnd: HWND; Msg: UINT;
  wParam: WPARAM; lParam: PAnsiChar; fuFlags: UINT;
  uTimeout: UINT; out lpdwResult: DWORD): LRESULT;
  external 'SendMessageTimeoutA@user32.dll stdcall';

procedure RefreshEnvironment;
var
  S: AnsiString;
  MsgResult: DWORD;
begin
  S := 'Environment';
  SendTextMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    PAnsiChar(S), SMTO_ABORTIFHUNG, 5000, MsgResult);
end;

procedure DebugOutput(Msg: string);
begin
  Log('Debug: ' + Msg);
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    DebugOutput('before set env PATH: ' + GetEnv('Path'));
  end
  else if CurStep = ssPostInstall then
  begin
    DebugOutput('after set env PATH: ' + GetEnv('Path'));
  end;
end;

Log of Script - 2

you can see here. I can't share it here because of Stackoverflow's max limit reach for body


Solution

  • Adding an environment variable path isn't mandatory when creating a Windows service. If you encounter an issue such as "'mongod' is not recognized as an internal or external command" even after adding environment variables during application installation, you can create a Windows service using the following approach:

    [Files]
    Source: "{#MyAppParentPath}\thirdparty\mongodb\*"; DestDir: "{app}\mongodb"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
    Source: "{#MyAppParentPath}\mongod.cfg"; DestDir: "{app}\mongodb\bin"; Flags: ignoreversion
    
    [Run]
    Filename: "{cmd}"; \
        Parameters: "/C mongod --config ""{app}\mongodb\bin\mongod.cfg"" --wiredTigerCacheSizeGB 0.65 --auth --install --serviceName=envVarIssueAppdatabase --logpath ""{app}\database\log\mongo.log"" && sc start envVarIssueAppdatabase && sc config envVarIssueAppdatabase DisplayName=envVarIssueApp-database-server"; \
        WorkingDir: "{app}\mongodb\bin"; \
        Description: "Installing MongoDB service"; \
        StatusMsg: "Installing MongoDB service..."; \
        Flags: postinstall runascurrentuser runhidden;
    

    This script installs MongoDB as a Windows service without relying on environment variables for the 'mongod' executable. It copies necessary files and configurations to the appropriate directories and then uses the 'sc' command to create and configure the Windows service for MongoDB.

    This approach ensures that the 'mongod' command is accessible within the service context without relying on environment variables, addressing the "'mongod' is not recognized as an internal or external command" issue.


    You can refer full code (create windows service for Mongodb and tomcat)

    ; Script generated by the Inno Setup Script Wizard.
    ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
    
    #define MyAppName "envVarIssue"
    #define MyAppVersion "1.5"
    #define MyAppPublisher "My Company, Inc."
    #define MyAppURL "https://www.example.com/"
    #define MyAppExeName "MyProg.exe"
    #define MyAppAssocName MyAppName + " File"
    #define MyAppAssocExt ".myp"
    #define MyAppAssocKey StringChange(MyAppAssocName, " ", "") + MyAppAssocExt
    
    #define MyAppParentPath "."
    
    [Setup]
    SetupLogging=yes
    ChangesEnvironment=yes
    
    ; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
    ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
    AppId={{D2C055CB-72E7-4DB9-83EE-34D399939D0C}
    AppName={#MyAppName}
    AppVersion={#MyAppVersion}
    ;AppVerName={#MyAppName} {#MyAppVersion}
    AppPublisher={#MyAppPublisher}
    AppPublisherURL={#MyAppURL}
    AppSupportURL={#MyAppURL}
    AppUpdatesURL={#MyAppURL}
    DefaultDirName={autopf}\{#MyAppName}
    ChangesAssociations=yes
    DisableProgramGroupPage=yes
    ; Uncomment the following line to run in non administrative install mode (install for current user only.)
    PrivilegesRequired=admin
    OutputBaseFilename=envVarIssue App
    Compression=lzma
    SolidCompression=yes
    WizardStyle=modern
    
    [Languages]
    Name: "english"; MessagesFile: "compiler:Default.isl"
    
    [Tasks]
    Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
    
    [Files]
    Source: "C:\Program Files (x86)\Inno Setup 6\Examples\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
    Source: "{#MyAppParentPath}\thirdparty\database\*"; DestDir: "{app}\database"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
    Source: "{#MyAppParentPath}\thirdparty\mongodb\*"; DestDir: "{app}\mongodb"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
    Source: "{#MyAppParentPath}\database\*"; DestDir: "{app}\database"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
    Source: "{#MyAppParentPath}\mongod.cfg"; DestDir: "{app}\mongodb\bin"; Flags: ignoreversion
    Source: "{#MyAppParentPath}\thirdparty\tomcat\*"; DestDir: "{app}\tomcat"; Flags: ignoreversion recursesubdirs createallsubdirs uninsneveruninstall onlyifdoesntexist;
    ; NOTE: Don't use "Flags: ignoreversion" on any shared system files
    
    [Registry]
    Root: HKA; Subkey: "Software\Classes\{#MyAppAssocExt}\OpenWithProgids"; ValueType: string; ValueName: "{#MyAppAssocKey}"; ValueData: ""; Flags: uninsdeletevalue
    Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}"; ValueType: string; ValueName: ""; ValueData: "{#MyAppAssocName}"; Flags: uninsdeletekey
    Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName},0"
    Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\{#MyAppExeName}"" ""%1"""
    Root: HKA; Subkey: "Software\Classes\Applications\{#MyAppExeName}\SupportedTypes"; ValueType: string; ValueName: ".myp"; ValueData: ""
    
    [Icons]
    Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
    Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
    
    [Run]
    ; Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
    Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: postinstall skipifsilent;
    ;Filename: "{cmd}"; Parameters: "/C set PATH=%PATH%;{app}\mongodb\bin"; Flags: runascurrentuser waituntilterminated;
    ; Run MongoDB mongod command
    ;Filename: "{cmd}"; Parameters: "/K mongod >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: postinstall waituntilterminated runascurrentuser;
    
    ; Install MongoDB and configure its service
    Filename: "{cmd}"; \
        Parameters: "/C mongod --config ""{app}\mongodb\bin\mongod.cfg"" --wiredTigerCacheSizeGB 0.65 --auth --install --serviceName=envVarIssueAppdatabase --logpath ""{app}\database\log\mongo.log"" && sc start envVarIssueAppdatabase && sc config envVarIssueAppdatabase DisplayName=envVarIssueApp-database-server"; \
        WorkingDir: "{app}\mongodb\bin"; \
        Description: "Installing MongoDB service"; \
        StatusMsg: "Installing MongoDB service..."; \
        Flags: postinstall runascurrentuser runhidden;
    
    ; Install Tomcat and configure its service
    Filename: "{cmd}"; \
        Parameters: "/K service.bat install && sc config Tomcat9 start=auto && sc description envVarIssueApp-tomcat-server && sc start Tomcat9 && sc config Tomcat9 obj=LocalSystem"; \
        WorkingDir: "{app}\tomcat\bin"; \
        Description: "Installing Tomcat service"; \
        StatusMsg: "Installing Tomcat service..."; \
        Flags: postinstall runascurrentuser;
    
    
    [Code]
    procedure DebugOutput(Msg: string);
    begin
      Log('Debug: ' + Msg);
    end;
    
    [Code]