2

我无法在 Windows 7 中修改环境变量。但是我已被授予修改注册表设置的权限。例如,我可以修改:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment Key: BPATH,它附加到我的系统 pathEnv 的末尾。

但是当我进行更改时,更改在下次重新启动之前不会生效。

我想找到一个“始终安装”的命令行,它可以发出 BPATH 更改的信号,以便始终为后续程序和窗口更新“PATH”。

rundll user32.dll, [ BroadcastSystemMessage PATH CHANTGED... or somehting...]

条件:

  1. 我只能通过注册表编辑路径和 bpath,而不是从系统,高级选项卡...
  2. 我不想安装任何不属于开箱即用 Windows 7 安装标准的软件。
  3. 通常,这应该只在 PowerShell 提示符的 CMD 提示符下完成。
4

3 回答 3

2

This sounds difficult, and cannot be done with the rundll32 - I know because I have tried. There are many questions here, where the documentation of rundll is linked from. This basically only calls functions of the form:

 void CALLBACK
  EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);

http://support.microsoft.com/kb/164787

Which means - you can call special functions, which were designed to be called with this tool, AND simple functions, which take no arguments, or maybe just a HWND.

To refresh the environment, you need to broadcast (i.e. SendMessageTimeout to HWND_BROADCAST) the WM_SETTINGCHANGE message, 4th argument (wparam) should be 0, and the LPARAM should be L"Environment" (notice the L - it has to be a wide string, or the variables won't refresh!).

Here's some working PS code, which I don't remember who wrote (it's actually the MSDN example for WM_SETTINGCHANGE translated from C++):

    #requires -version 2

    if (-not ("win32.nativemethods" -as [type])) {
        # import sendmessagetimeout from win32
        add-type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessageTimeout(
        IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
        uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
    "@
    }

    $HWND_BROADCAST = [intptr]0xffff;
    $WM_SETTINGCHANGE = 0x1a;
    $result = [uintptr]::zero

    # notify all windows of environment block change
    [win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE,
            [uintptr]::Zero, "Environment", 2, 5000, [ref]$result);

It's more than 1 command, but fits into a small script.

I haven't tried your trick with a variable within a variable (PATH), but it may be, that such a configuration requires two subsequent refreshes (the first one allows BPATH to be updated, and on the second one PATH uses the updated BPATH value.

于 2012-07-24T00:15:37.553 回答
1

在 Powershell 中,以下命令会将系统变量“variableName”设置为 variableValue 的值。最后一个值可以是Machine,UserProcess

PS C:\>[Environment]::SetEnvironmentVariable("variableName","variableValue","Machine")

shell 向系统发送更新环境广播。任何打开的 shell 都不会收到新的环境变量。

于 2010-11-24T20:17:12.003 回答
1

电源外壳:

[Environment]::SetEnvironmentVariable("path",[Environment]::GetEnvironmentVariable("path"),"Machine")

好了,将路径机器变量设置为它自己

(请注意,如果您更改了“路径”,并且没有为 Explorer.exe 更新,Powershell 可能会再次重置当前环境!)

于 2016-07-29T16:06:29.547 回答