Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh-Desktop is not refreshing the global environment block #37

Closed
appPackager opened this issue May 21, 2015 · 7 comments
Closed

Refresh-Desktop is not refreshing the global environment block #37

appPackager opened this issue May 21, 2015 · 7 comments
Assignees
Labels
Status: Complete Issue resolved / work complete Type: Issue / Bug Something isn't working as expected
Milestone

Comments

@appPackager
Copy link

According to help the Refresh-Desktop method refreshes the desktop icons, however, AppDeployToolkitMain.cs also mentions it will update environment variables (i.e. public static void RefreshDesktopAndEnvironmentVariables()). In performing testing I do not find indication that the environment block is actually refreshing. I have very similar code I added to my extensions which does refresh the environment. I was wondering if we can fix the existing SendMessageTimeout to pass the proper params to also allow refresh of the environment block? See my extension code below:

 #region Function Refresh-GlobalEnvironment
Function Refresh-GlobalEnvironment {
<#
.SYNOPSIS
    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.
.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.
.EXAMPLE
    Refresh-GlobalEnvironment
.LINK
    Http://poshcode.org/2049
#>
    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
Write-Log ("Notifying all windows of global environment block change...")
[win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [uintptr]::Zero, "Environment", 2, 5000, [ref]$result);
}
#endregion
@appPackager appPackager changed the title Refresh-Desktop routines have conflicting information Does Refresh-Desktop also refresh the global environment block? May 21, 2015
@mmashwani
Copy link
Contributor

Please post code in code blocks so it's easier to read. You can do so by surround the code block with three back ticks before and after (I edited your post to add the code block).

Refresh-Desktop will refresh desktop icons and also update the environment block for Windows Explorer so that new processes that are spawned reflect the newly created environment variables. Processes load environment variables at initial start and then it is impossible to update the environment block for that process unless you restart the process. If you need to use an environment variable you just created in the same process that created it, then make a call to Refresh-SessionEnvironmentVariables.

@mmashwani
Copy link
Contributor

Please explain in detail what behaviour you notice when using the custom code you posted above vs the behaviour you notice when you use the code in the toolkit. That will give me a better understanding of the issue and what I need to fix.

@appPackager appPackager changed the title Does Refresh-Desktop also refresh the global environment block? Refresh-Desktop is not refreshing the global environment block May 21, 2015
@appPackager
Copy link
Author

Perform your test as follows:

  1. Open Regedit and change the PATH value located at: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment;Path
  2. Open a command prompt and type SET. Note the value for PATH. It will not reflect your change.
  3. Run Refresh-Desktop
  4. Open a new command prompt and type SET. Note the value still reflects the value you saw in step Show-InstallationWelcome dialog window text is offset to the left #2.
  5. Run Refresh-GlobalEnvironment
  6. Open a new command prompt and type SET. Note the value for PATH now reflects the change you made in step 1.

@appPackager
Copy link
Author

The change required to resolve the issue is to make the following change to AppDeployToolkitMain.cs:
new:

public static void RefreshDesktopAndEnvironmentVariables()
        {
            // Update desktop icons
            SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
            // Update environment variables
            SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, "Environment", SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
        }

old:

public static void RefreshDesktopAndEnvironmentVariables()
        {
            // Update desktop icons
            SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
            // Update environment variables
            SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
        }

@mmashwani
Copy link
Contributor

Thanks for the update. I've implemented the fix in the latest beta.

@mmashwani mmashwani added Type: Issue / Bug Something isn't working as expected Status: Complete Issue resolved / work complete labels May 22, 2015
@mmashwani mmashwani added this to the v3.6.5 milestone May 22, 2015
@mmashwani mmashwani self-assigned this May 22, 2015
@twahab
Copy link

twahab commented Aug 6, 2020

Hello, we are running version 3.8.1 of the toolkit and seen where the explorer.exe process is stopped during the installation step using Execute-Msi function. It sounds to me it's happening during the [Installation] :: Refresh the Desktop and the Windows Explorer environment process block. Update-Desktop 8/6/2020 3:51:08 PM 16600 (0x40D8) Step. The package completes running to post-installation step, however then user has to sign in and out to get explorer back or launch taskmgr and initiate explorer.exe. Would appreciate any feedback, thanks

@Garg0yle216
Copy link

The package completes running to post-installation step, however then user has to sign in and out to get explorer back or launch taskmgr and initiate explorer.exe. Would appreciate any feedback, thanks

I am using 3.8.4 and I started receiving this same issue in about March 2022. Did you ever get a response on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Complete Issue resolved / work complete Type: Issue / Bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

4 participants