PoshCode Archive  Artifact [d02e4890f7]

Artifact d02e4890f75725575e5f8c9bb7fe2c85858f8d5fabedd167d708d994ec0f489c:

  • File Invoke-WMSettingsChange.ps1 — part of check-in [cce4780579] at 2018-06-10 13:04:28 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: 2049
# x-archived: 2017-05-16T12:04:30
# x-published: 2011-08-04T07:50: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);