How to set a global environment variable from Inno Setup installer?

ees

How do I set a global environment variable in Inno Setup?

Background: I am using the Inno install utility and need to set a global environment variable before I do the actual install.

Adrian

Try this:

[Registry]
Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName: "VARIABLE_NAME"; \
    ValueData: "new_value"; Flags: preservestringtype

You might need to add this:

[Setup]
; Tell Windows Explorer to reload the environment
ChangesEnvironment=yes

Alternatively try:

[Run]
Filename: "{app}\MyProg.exe"; BeforeInstall: SetEnvPath

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

function SetEnvironmentVariable(lpName: string; lpValue: string): BOOL;
  external 'SetEnvironmentVariable{#AW}@kernel32.dll stdcall';

procedure SetEnvPath;
begin
  if not SetEnvironmentVariable('VARIABLE_NAME', 'new_value') then
    MsgBox(SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;

Reference: Inno Setup Frequently Asked Questions - Setting Environment Variables

If the variable change is not propagated (see Environment variable not recognized [not available] for [Run] programs in Inno Setup)

[Run]
...; AfterInstall: RefreshEnvironment

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

More details:

Inno Setup: Setting a System Environment Variable

Under more modern (in other words, proper) operating systems, such as Windows 2000, XP, and Windows 2003 Server, environment variables are stored in the Registry under the following key:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\ Environment

Variables are added by creating a new value under this key or by modifying a value if it already exists. To delete a variable, you simply delete its Registry value, unless you are removing part of an expanded value, such as PATH, in which case you only remove the part you want.

At this point, Windows will not be aware of your changes unless you log off or reboot. To get around this, SetEnv will broadcast a WM_SETTINGCHANGE to all of the windows in the system. This allows other running applications—for example, Explorer.exe—to be notified of your change. If you run SetEnv from a command prompt, this will not update the environment variable for the current DOS window. This is mainly due to the fact that a process (SetEnv) cannot change the environment of its parent (The Command Prompt). However, any new DOS/Command Prompts that you open will show the new variable/value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set registry key (environment variable) with value from [Code] section of Inno Setup?

From Dev

How to extract a Inno Setup Installer

From Dev

Getting environment variable using Inno setup

From Dev

How to set inno setup script parameters from final builder?

From Dev

How to set inno setup script parameters from final builder?

From Dev

Inno Setup: How to pass variable from [Code] to [Run] (or other section)

From Dev

Inno Setup: How to pass variable from [Code] to [Run] (or other section)

From Dev

How to take the Office's install directory from registry for an Inno Setup installer

From Dev

Inno Setup - Remove path from PATH environment variable while uninstalling a program

From Dev

Inno Setup: Reading a file from installer during uninstallation

From Dev

Using global string script variable in Run or other section in Inno Setup

From Dev

Using global string script variable in Run or other section in Inno Setup

From Dev

Inno Setup: How to put the installer itself in the installed program

From Dev

How do I minimize my installer in Inno Setup?

From Dev

Inno Setup - How to Install Windows Update Offline Installer

From Dev

How to provide an input to a batch file ran using Inno Setup installer?

From Dev

Inno Setup - Installer for multiple installers

From Dev

Inno Setup - Installer for multiple installers

From Dev

Environment variable not recognized [not available] for [Run] programs in Inno Setup

From Dev

Setting PGPASSWORD environment variable for Postgres (psql) process executed by Inno Setup

From Dev

How to set Jenkins environment variable from script

From Dev

How to set `screen` environment variable from bash?

From Dev

Inno Setup - How to set permissions of installation folder

From Dev

How to use a Pascal variable in Inno Setup?

From Dev

How to call "npm install" from Inno Setup?

From Dev

How can I set the exit code from return code of my Execute File in Inno Setup?

From Dev

How to set a variable with an environment variable read from a text file

From Dev

Microsoft SmartScreen - suspended using Inno Setup installer?

From Dev

Inno Setup specify log name within the installer

Related Related

  1. 1

    How to set registry key (environment variable) with value from [Code] section of Inno Setup?

  2. 2

    How to extract a Inno Setup Installer

  3. 3

    Getting environment variable using Inno setup

  4. 4

    How to set inno setup script parameters from final builder?

  5. 5

    How to set inno setup script parameters from final builder?

  6. 6

    Inno Setup: How to pass variable from [Code] to [Run] (or other section)

  7. 7

    Inno Setup: How to pass variable from [Code] to [Run] (or other section)

  8. 8

    How to take the Office's install directory from registry for an Inno Setup installer

  9. 9

    Inno Setup - Remove path from PATH environment variable while uninstalling a program

  10. 10

    Inno Setup: Reading a file from installer during uninstallation

  11. 11

    Using global string script variable in Run or other section in Inno Setup

  12. 12

    Using global string script variable in Run or other section in Inno Setup

  13. 13

    Inno Setup: How to put the installer itself in the installed program

  14. 14

    How do I minimize my installer in Inno Setup?

  15. 15

    Inno Setup - How to Install Windows Update Offline Installer

  16. 16

    How to provide an input to a batch file ran using Inno Setup installer?

  17. 17

    Inno Setup - Installer for multiple installers

  18. 18

    Inno Setup - Installer for multiple installers

  19. 19

    Environment variable not recognized [not available] for [Run] programs in Inno Setup

  20. 20

    Setting PGPASSWORD environment variable for Postgres (psql) process executed by Inno Setup

  21. 21

    How to set Jenkins environment variable from script

  22. 22

    How to set `screen` environment variable from bash?

  23. 23

    Inno Setup - How to set permissions of installation folder

  24. 24

    How to use a Pascal variable in Inno Setup?

  25. 25

    How to call "npm install" from Inno Setup?

  26. 26

    How can I set the exit code from return code of my Execute File in Inno Setup?

  27. 27

    How to set a variable with an environment variable read from a text file

  28. 28

    Microsoft SmartScreen - suspended using Inno Setup installer?

  29. 29

    Inno Setup specify log name within the installer

HotTag

Archive