gpt4 book ai didi

environment-variables - 如何从Inno Setup安装程序设置全局环境变量?

转载 作者:行者123 更新时间:2023-12-04 08:40:11 24 4
gpt4 key购买 nike

如何在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, 0,
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_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.

关于environment-variables - 如何从Inno Setup安装程序设置全局环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25289056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com