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

后端 未结 3 1185
后悔当初
后悔当初 2020-12-16 05:17

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

3条回答
  •  旧时难觅i
    2020-12-16 06:18

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

    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.

提交回复
热议问题