使用 rundll32 user32.dll 来指示用户环境已更改

发布于 2024-09-08 13:18:20 字数 531 浏览 4 评论 0原文

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

但是当我进行更改时,更改直到下次重新启动才会生效。

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

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

条件:

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

I do not have the ability to modify environment variables in windows 7.
However I have been granted permission to modify the registry settings.
So for example I can modify:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Key: BPATH
which is appended to the end of my System's pathEnv.

but when I make the changes the change does not take affect until the next reboot.

I would like to find a command line that is "Always Installed" that can signal a change in BPATH such that "PATH" is always updated for subsequent programs and windows.

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

Conditions:

  1. I can only edit the path and bpath via the registry, not from the System, Advanced tab...
  2. I do not want to install any software that is not part of a standard out of the box windows 7 install.
  3. Typically this should just be done from the CMD prompt of the PowerShell prompt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-09-15 13:18:20

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

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

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

In Powershell, the following command will set the System variable 'variableName' the value of variableValue.
The last value can be Machine,User or Process

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

The shell sends an update environment broadcast to the system. Any open shells will not receive the new environment variable.

疏忽 2024-09-15 13:18:20

这听起来很困难,并且不能用 rundll32 来完成 - 我知道因为我已经尝试过。这里有很多问题,rundll的文档是从哪里链接的。这基本上只调用以下形式的函数:

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

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

这意味着 - 您可以调用专门为使用此工具调用而设计的特殊函数,以及不带参数的简单函数,或者可能只是一个 HWND。

要刷新环境,您需要广播(即 SendMessageTimeoutHWND_BROADCASTWM_SETTINGCHANGE 消息,第四个参数 (wparam) 应该是 0LPARAM 应该是 L"Environment" (注意 L - 它有是一个宽字符串,否则变量将不会刷新!)。

这是一些有效的 PS 代码,我不记得是谁写的(实际上是
从 C++ 翻译的 WM_SETTINGCHANGE 的 MSDN 示例):

    #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);

它不止 1 个命令,但适合一个小脚本。

我还没有尝试过使用变量(PATH)中的变量来尝试你的技巧,但可能是,
这样的配置需要两次后续刷新(第一个允许更新 BPATH,第二个 PATH 使用更新的 BPATH 值。

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.

挽容 2024-09-15 13:18:20

Powershell:

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

给你,
将路径机器变量设置为其自身

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

Powershell:

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

There you go,
Set the Path Machine Variable to it's self

(Be aware that if you changed 'path', and it didn't update for Explorer.exe, Powershell would probably reset the current environment again!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文