RestartExplorer() : Deprecated version

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

RestartExplorer() : Deprecated version

07 Dec 2013, 10:04

Note: Newer version is available @ https://www.autohotkey.com/boards/viewtopic.php?t=81251
 

Code: Select all

ReStartExplorer( WaitSecs = 10 ) {   ; SKAN, CD:07-Dec-2013 LM:14-Dec-2014 | goo.gl/UnS2rl
 ;  Credit: Gracefully Exit Explorer (Programmatically) - Stack Overflow   | goo.gl/tAA9HY
 ;  Thanks to chaz - http://ahkscript.org/boards/viewtopic.php?p=7099#p7099

 If A_OSVersion not in WIN_XP,WIN_VISTA,WIN_7
  Return 0, ErrorLevel := 1

 If ( A_OSVersion = "WIN_XP" ) {  
  WinGet, ShellPID, PID,     ahk_class Progman
  PostMessage, 0x012, 0, 0,, ahk_class Progman        ; WM_QUIT = 0x12
 } Else {
  WinGet, ShellPID, PID,     ahk_class Shell_TrayWnd
  PostMessage, 0x5B4, 0, 0,, ahk_class Shell_TrayWnd  ; WM_USER + 0x1B4
 }    

 Process, WaitClose, %ShellPID%, %WaitSecs%
 IfEqual, ErrorLevel, 0, Run %A_WinDir%\explorer.exe
Return !ErrorLevel
}
My Scripts and Functions: V1  V2
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: RestartExplorer()

07 Dec 2013, 10:35

This seems to also preserve desktop icons position.
thank you SKAN Image
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: RestartExplorer()

07 Dec 2013, 11:41

how could this script be modified to restart Firefox? :ugeek:
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: RestartExplorer()

07 Dec 2013, 21:45

cool!
this answer also makes some interesting remarks ;) http://stackoverflow.com/a/6246182/883015
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RestartExplorer()

07 Dec 2013, 23:35

Chef wrote:This seems to also preserve desktop icons position.
Yes, in XP also! This method seems to be better than killing the process with Process, Close.
Guest10 wrote:how could this script be modified to restart Firefox? :ugeek:
That is a totally different topic. For any process, it would be like:
1) Get the Remote command line of process for restart
2) Exit process gracefully
3) Wait for process to close
4) Restart it with saved command line + parameters
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RestartExplorer()

07 Dec 2013, 23:40

joedf wrote:this answer also makes some interesting remarks ;) http://stackoverflow.com/a/6246182/883015
You mean the remark made after conclusion?
Do not post the WM_QUIT message using the PostMessage function; use PostQuitMessage.
Well, my machine is dual boot. I read the conclusion - booted into XP - tested - happy - returned back to Win7 and then saw that. :evil:
I will test it later and then update. :)
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: RestartExplorer()

08 Dec 2013, 00:00

alright, hahah :P but if if works, theres no need to break it ;) hehe
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Sidola
Posts: 14
Joined: 10 Oct 2013, 14:14
Contact:

Re: RestartExplorer()

10 Dec 2013, 15:04

Hm, not sure how to explain this, but when the script reaches the Run-part it only opens a folder, it doesn't actually start explorer.exe again. I have to do that manually through the task manager, File -> New Task (Run)

Is that intended? I'm on W7 x64.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: RestartExplorer()

10 Dec 2013, 18:06

that means, that explorer.exe is already running... maybe it was unsuccessful to kill explorer.exe
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RestartExplorer()

10 Dec 2013, 21:34

@Sidola, Did you test manually?

Do a "Ctrl+Shift Mouse Right-Click" on shutdown button.

Image

Keep Task manager open and run the following snippet

Note: The function fetches the PID of explorer.exe which is hosting the Taskbar.

Code: Select all

If ( A_OSVersion <> "WIN_7" ) ; Run this snippet only on Windows 7 
 ExitApp                      
 
  
WinGet, ShellPID, PID, ahk_class Shell_TrayWnd
  
Msgbox explorer.exe PID = %ShellPID%  

SendMessage, 0x5B4, 0, 0,, ahk_class Shell_TrayWnd  ; WM_USER + 0x1B4

Reply := ErrorLevel
  
Process, WaitClose, %ShellPID%, 10 

Exited := !ErrorLevel

If ( Exited )
  Run explorer.exe

Msgbox % "SendMessage Reply = " Reply "`nDid explorer.exe exit? " ( Exited ? "Yes":"No" ) 
Sidola
Posts: 14
Joined: 10 Oct 2013, 14:14
Contact:

Re: RestartExplorer()

11 Dec 2013, 13:29

http://puu.sh/5JspP/4286c04915.png

This is what I get at home running x64, at work where I run x32 it worked perfectly. Can anyone confirm it working on W7 x64?
timeFlies
Posts: 146
Joined: 22 Oct 2013, 20:54
Location: Somewhere in the northern hemisphere.

Re: RestartExplorer()

12 Dec 2013, 18:54

Thanks a lot! I was using a similar but very crude solution to this that didn't always work.

However, like Sidola above, this didn't actually restart the Explorer shell but simply opened an Explorer window. I noticed this behaviour before (which only seemed to occur on my x64 computer), and was able to solve it by running %windir%\explorer.exe instead of explorer.exe.

Note that %windir% is not an AHK variable but a Windows one. I have no idea why this works (I came across it accidently), but it does.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RestartExplorer()

13 Dec 2013, 16:19

chaz wrote:However, like Sidola above, this didn't actually restart the Explorer shell but simply opened an Explorer window. I noticed this behaviour before (which only seemed to occur on my x64 computer), and was able to solve it by running %windir%\explorer.exe instead of explorer.exe.
Many thanks for the tip, chaz :)
Per your suggestion, explorer restarts properly for me in AHK 1.1 Unicode x86/x64 in Windows 7 x64.

PS: However, it should be A_WinDir instead of windir, as windir will be empty when #NoEnv directive is used in a script.

Code: Select all

#NoEnv  
MsgBox %windir%
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RestartExplorer()

13 Dec 2013, 16:24

Sidola wrote:Image

This is what I get at home running x64, at work where I run x32 it worked perfectly. Can anyone confirm it working on W7 x64?
Are you sure that both AutoHotkey.exe and explorer.exe are running with identical privileges?
For me, the explorer exit fails if one of them is elevated while the other is not.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 114 guests