PoshCode Archive  Artifact [86b65d8b70]

Artifact 86b65d8b7097e29adf05619ec29505dbf25d1949efc0cd82abb8859ac1fa5ae6:

  • File Invoke-WMSettingsChange.ps1 — part of check-in [07a0a8ebda] at 2018-06-10 13:50:53 on branch trunk — Notifies other processes that the global environment block has changed. This lets other processes pick changes to ENV: without having to reboot or logoff/logon. A non-zero result from SendMessageTimeout indicates success. (user: Oisin Grehan size: 1180)

# encoding: ascii
# api: powershell
# title: Invoke-WMSettingsChange
# description: Notifies other processes that the global environment block has changed. This lets other processes pick changes to ENV: without having to reboot or logoff/logon. A non-zero result from SendMessageTimeout indicates success.
# version: 0.1
# author: Oisin Grehan
# license: CC0
# x-poshcode-id: 5149
# x-archived: 2016-03-22T15:39:03
# x-published: 2016-05-06T08:29:00
#
#
#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);