Refresh Desktop (Powershell hybrid)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Refresh Desktop (Powershell hybrid)

#1 Post by aGerman » 05 Sep 2018 10:44

In that thread the question came up how to refresh the desktop programmatically.
viewtopic.php?f=3&t=8805
Using platform invokation you can use Windows API functions in a little Powershell snippet. A lot of different API calls can be found in the internet to simulate the behavior of pressing F5 but none of them do all of the stuff that F5 invokes. Only one of the solutions that I found does direcly simulate F5 by sending the desired accelerator ID.

Code: Select all

@echo off &setlocal

set refresh_desktop=powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^"try{$c=Add-Type -Name WinAPI -PassThru -MemberDefinition '^
[DllImport(\"user32.dll\")] public static extern IntPtr GetShellWindow();^
[DllImport(\"user32.dll\")] public static extern int SendMessageW(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);';^
$dsktp=$c::GetShellWindow(); $WM_COMMAND=273; $accel_F5=New-Object UIntPtr(41504); $nullptr=[IntPtr]::Zero;^
exit [int](($dsktp -eq $nullptr) -or ($c::SendMessageW($dsktp, $WM_COMMAND, $accel_F5, $nullptr) -ne 0));}catch{exit 1;}^"

:: Send command message 41504 to the Shell desktop window and call its window procedure. Number 41504 is the reverse-engineered accelerator
:: ID of the F5 virtual key in explorer windows. Thus, executing this macro simulates the same behavior as if you would have pressed F5 to
:: refresh the desktop. The advantage is that the desktop doesn't need to have the keyboard focus to make it work.
%refresh_desktop%
echo %errorlevel%
pause
I already tested on Win10 32 and 64 bit. For a couple of days I don't have any access to older versions anymore. Our IT dept. decided to begin with upgrading all of the company's computers because the support for Win7 will expire in January 2020.
I'd really like you to test the code on other versions and give feedback because I don't know if the accelerator ID ever changed.

Steffen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Refresh Desktop (Powershell hybrid)

#2 Post by ShadowThief » 05 Sep 2018 13:42

Working on my XP Pro virtual machine (once I installed the Windows Management Framework Core package), as well as Windows 7 32-bit and 64-bit.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Refresh Desktop (Powershell hybrid)

#3 Post by aGerman » 05 Sep 2018 14:24

Thank you for testing ShadowThief!

Now I suspect it will work from XP onwards and I'm fully aware that it doesn't make any sense to try on even older versions. As you said Powershell did not belong to XP. It was introduced in 2006 and became a default installation in Win7. However once Powershell was installed the code will work and your reply answered my question and eased my doubt :D
FWIW I'm also aware that the performance is rather bad. Powershell.exe is an external process that the OS has to schedule before it gets loaded. Furthermore platform invokations in Powershell require a compiled temporary DLL (you'll find it for a few milliseconds in the %temp% directory) which wastes even more time. But I didn't find any better solution yet.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Refresh Desktop (Powershell hybrid)

#4 Post by Squashman » 05 Sep 2018 18:17

I am not a .net programmer but didn't someone show an example of creating and compiling quick and dirty .net code. Not sure if that would be faster.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Refresh Desktop (Powershell hybrid)

#5 Post by ShadowThief » 05 Sep 2018 22:36

Squashman wrote:
05 Sep 2018 18:17
I am not a .net programmer but didn't someone show an example of creating and compiling quick and dirty .net code. Not sure if that would be faster.
You mean viewtopic.php?t=4762#p27555?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Refresh Desktop (Powershell hybrid)

#6 Post by Squashman » 06 Sep 2018 07:01

ShadowThief wrote:
05 Sep 2018 22:36
Squashman wrote:
05 Sep 2018 18:17
I am not a .net programmer but didn't someone show an example of creating and compiling quick and dirty .net code. Not sure if that would be faster.
You mean viewtopic.php?t=4762#p27555?
This one viewtopic.php?t=5260

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Refresh Desktop (Powershell hybrid)

#7 Post by aGerman » 06 Sep 2018 10:34

Squashman wrote:
05 Sep 2018 18:17
Not sure if that would be faster.
I'm pretty sure it would be faster but only if you reuse the compiled utility :wink:
I just tried to keep it a scripting solution.

Steffen

Post Reply