加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

environment-variables – 如何从Inno Setup安装程序设置全局环

发布时间:2020-12-15 09:49:46 所属栏目:大数据 来源:网络整理
导读:如何在Inno Setup中设置全局环境变量? 背景:我正在使用Inno安装实用程序,需要在进行实际安装之前设置全局环境变量. 解决方法 试试这个: [Registry]Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName: "VARIABLE_NAME"; ValueData: "new_
如何在Inno Setup中设置全局环境变量?

背景:我正在使用Inno安装实用程序,需要在进行实际安装之前设置全局环境变量.

解决方法

试试这个:

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

您可能需要添加以下内容:

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

或者尝试:

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

参考:Inno Setup Frequently Asked Questions – Setting Environment Variables

如果不传播变量变化(见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,PAnsiChar(S),SMTO_ABORTIFHUNG,5000,MsgResult);
end;

更多细节:

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_MACHINESystemCurrentControlSetControlSession 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读