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