Force environment refresh using custom action

Discover(request) our samples to help you get started with your project.
Post Reply
Dan
Posts: 4441
Joined: Wed Apr 24, 2013 3:51 pm

Force environment refresh using custom action

Post by Dan » Tue Dec 18, 2018 3:28 pm

Hello,

There are cases when the environment variables are not refreshed during installation, so the modifications to the environment variables do not result in an immediate change. The list of environment variables is being loaded when the process is initialized but not refreshed. In order to have the newly created environment variable you can have a custom action that will force a refresh of the environment variables. Basically you need to broadcast a settings change message to all windows in the system so that any interested application can perform an update.

To overcome this behavior, you can broadcast a WM_SETTINGCHANGE message to all windows to inform them of the change.

Here's a PowerShell script that performs this task:

Code: Select all

This is what is in the inline script:

# Block for declaring the script parameters.

Param()
# Your code goes here.
Add-Type -TypeDefinition @"
    using System;
    using System.Runtime.InteropServices;

    public class NativeMethods
    {
        [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
$SMTO_ABORTIFHUNG = 0x2
$result = [UIntPtr]::Zero

[void] ([Nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, 'Environment', $SMTO_ABORTIFHUNG, 5000, [ref] $result))
For more details, you can check the Invoke-Command to set PSModulePath article.

The sample project is attached to this thread, so if you are interested to take a look directly at my project, you are more than welcome to download the ZIP file.

Of course, you can create your custom action as a custom action written in C# or a custom action written in C++.

Best regards,
Dan
Attachments
Force Envir Refresh.zip
(3.84 KiB) Downloaded 461 times
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Post Reply