环境变量设置成功但在Inno setup脚本中设置后没有更新

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

Stack Overflow 社区您好,

我遇到了 Inno Setup 脚本的问题,需要一些帮助。在安装可执行文件 (Exe) 期间,我需要为 MongoDB 设置环境变量。我的目标是为 MongoDB 创建一个 Windows 服务,但在此之前,我需要为其添加一个环境变量。为了实现这一目标,我在 Inno Setup 脚本中执行了以下步骤:

  1. ssInstall
    的 CurStepChanged 事件中,我使用 EnvRemoveAddPath 函数添加 MongoDB 环境变量。环境变量似乎设置正确。

  2. ssPostInstall
    的 CurStepChanged 事件中,我使用 RefreshEnvironment 函数刷新环境变量。

  3. 我继续执行必要的逻辑,包括为 MongoDB 创建 Windows 服务。

但是,当我在服务配置期间尝试使用命令

mongod --dbpath <{app}\database\path>
配置MongoDB时,遇到错误消息:
'mongod' is not recognized as an internal or external command, operable program or batch file

奇怪的是,出现这个错误后,我手动检查MongoDB的环境变量设置,它们似乎设置正确。此外,当我在 CMD 中使用

echo %path%
命令时,我可以看到 MongoDB 变量确实存在。尽管如此,在执行安装程序期间,错误仍然存在。

任何人都可以帮助我理解为什么会出现此问题以及如何解决它?谢谢您的协助。

我提供了我的代码,请检查。

; 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 '[email protected] 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;

此外你还可以看到我的调试日志

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.


我尝试通过以下方式创建Windows服务:

; 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;


脚本 - 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 '[email protected] 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;

脚本日志 - 2

您可以看到这里。由于 Stackoverflow 对 body 的最大限制,我无法在这里分享它

java windows-installer inno-setup inno-setup-v6
1个回答
0
投票

创建 Windows 服务时,不强制添加环境变量路径。如果在安装应用程序时添加环境变量后遇到“'mongod' 未被识别为内部或外部命令”等问题,您可以使用以下方法创建 Windows 服务:

[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;

此脚本将 MongoDB 安装为 Windows 服务,而不依赖于“mongod”可执行文件的环境变量。它将必要的文件和配置复制到适当的目录,然后使用“sc”命令为 MongoDB 创建和配置 Windows 服务。

这种方法确保了“mongod”命令可以在服务上下文中访问,而无需依赖环境变量,解决“'mongod'不被识别为内部或外部命令”的问题。


可以参考完整代码(为Mongodb和tomcat创建windows服务)

; 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]

© www.soinside.com 2019 - 2024. All rights reserved.