Jump to content

restart explorer.exe


Recommended Posts

Hello,

I am changing the Desktop folder location in the registry and I would like to restart explorer.exe to apply changes.

Problem is: after explorer restart only the libraries open and desktop icons, task bar, clock etc are missing (see attached screenshot)

It works after restart, but I want to avoid a restart

 

#include <MsgBoxConstants.au3>

While ProcessExists ("explorer.exe")

    ProcessClose ("explorer.exe")

Wend

sleep(1000)

Run("explorer.exe")

sleep(1000)

If NOT ProcessExists("explorer.exe") Then Run("explorer.exe")
EnvUpdate()
MsgBox(0,"",@UserName)

 

screen.jpg

Edited by hendrikhe
Link to comment
Share on other sites

Hi hendrikhe,

I would do it like so:

#include <WinAPI.au3>
#include <WinAPISys.au3>

;Save icon positions (probably not needed since we are doing a graceful exit ..)
DllCall("shell32.dll", "none", "SHChangeNotify", "long", 0x8000000, "uint", BitOR(0x0, 0x1000), "ptr", 0, "ptr", 0)

;Close the explorer shell gracefully
$hSysTray_Handle = _WinAPI_FindWindow('Shell_TrayWnd', '')
_SendMessage($hSysTray_Handle, 0x5B4, 0, 0)

While WinExists($hSysTray_Handle)
    Sleep(500)
WEnd

_start_explorer()

While Not _WinAPI_GetShellWindow()
    Sleep(500)
WEnd

EnvUpdate()

; continue script

Exit

Func _start_explorer()
    $strComputer = "localhost"
    $objWMI = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    Local $objexplorer = $objWMI.Get("win32_process")
    $objexplorer.create("explorer.exe")
EndFunc   ;==>_start_explorer

 

Link to comment
Share on other sites

Try this one:

#include <WinAPIShellEx.au3>
#include <SendMessage.au3>

_Restart_Windows_Explorer()
ConsoleWrite("+ Result: " & @error & @crlf)

Func _Restart_Windows_Explorer()
    ; By KaFu

    ; Believed to save icon positions just before Shutting down explorer, which comes next
    _WinAPI_ShellChangeNotify($SHCNE_ASSOCCHANGED, 0, 0, 0)

    ;Shutting down explorer gracefully
    Local $hSysTray_Handle = DllCall("user32.dll", "HWND", "FindWindow", "str", "Shell_TrayWnd", "str", "")
    If Not IsHWnd($hSysTray_Handle[0]) Then Return SetError(1)

    Local $iPID_Old = WinGetProcess($hSysTray_Handle[0])

    _SendMessage($hSysTray_Handle[0], 0x5B4, 0, 0)

    #cs
        Local $i_Timer = TimerInit()
        While IsHWnd($hSysTray_Handle[0])
            Sleep(10)
            If TimerDiff($i_Timer) > 5000 Then Return SetError(2)
        WEnd
    #ce

    Local $i_Timer = TimerInit()
    While ProcessExists($iPID_Old)
        Sleep(10)
        If TimerDiff($i_Timer) > 5000 Then Return SetError(3)
    WEnd

    Sleep(500)

    Return ShellExecute(@WindowsDir & "\Explorer.exe")

EndFunc   ;==>_Restart_Windows_Explorer

 

Link to comment
Share on other sites

:> Restart Windows Explorer:

#include <WinAPI.au3>
;~ #include <WinAPISys.au3>
#include <WinAPIShellEx.au3>
#include <SendMessage.au3>
;~ Global Const $shcne_AssocChanged = 134217728

ConsoleWrite("! Explorer PID: " & _Restart_Explorer() & "  *  Error: " & @error & @CRLF)

Func _Restart_Explorer()
    Local $ifailure = 100, $zfailure = 100, $rPID = 0, $iExplorerPath = @WindowsDir & "\Explorer.exe"
    _WinAPI_ShellChangeNotify($shcne_AssocChanged, 0, 0, 0) ; Save icon positions
    Local $hSystray = _WinAPI_FindWindow("Shell_TrayWnd", "")
    _SendMessage($hSystray, 1460, 0, 0) ; Close the Explorer shell gracefully
    While ProcessExists("Explorer.exe") ; Try Close the Explorer
        Sleep(10)
        $ifailure -= ProcessClose("Explorer.exe") ? 0 : 1
        If $ifailure < 1 Then Return SetError(1, 0, 0)
    WEnd
;~  _WMI_StartExplorer()
    While (Not ProcessExists("Explorer.exe")) ; Start the Explorer
        If Not FileExists($iExplorerPath) Then Return SetError(-1, 0, 0)
        Sleep(500)
        $rPID = ShellExecute($iExplorerPath)
        $zfailure -= $rPID ? 0 : 1
        If $zfailure < 1 Then Return SetError(2, 0, 0)
    WEnd
    Return $rPID
EndFunc   ;==>_Restart_Explorer

;~ Func _WMI_StartExplorer()
;~  Local $strComputer = "localhost"
;~  Local $objWMI = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
;~  Local $objExplorer = $objWMI.get("win32_process")
;~  Return $objExplorer.create("explorer.exe")
;~ EndFunc   ;==>_WMI_StartExplorer


;~ Func _WinAPI_ShellChangeNotify($ievent, $iflags, $iitem1 = 0, $iitem2 = 0)
;~  Local $stypeofitem1 = "dword_ptr", $stypeofitem2 = "dword_ptr"
;~  If IsString($iitem1) Then
;~      $stypeofitem1 = "wstr"
;~  EndIf
;~  If IsString($iitem2) Then
;~      $stypeofitem2 = "wstr"
;~  EndIf
;~  DllCall("shell32.dll", "none", "SHChangeNotify", "long", $ievent, "uint", $iflags, $stypeofitem1, $iitem1, $stypeofitem2, $iitem2)
;~  If @error Then Return SetError(@error, @extended, 0)
;~  Return 1
;~ EndFunc   ;==>_WinApi_ShellChangeNotify

;~ Func _SendMessage($hwnd, $imsg, $wparam = 0, $lparam = 0, $ireturn = 0, $wparamtype = "wparam", $lparamtype = "lparam", $sreturntype = "lresult")
;~  Local $aresult = DllCall("user32.dll", $sreturntype, "SendMessageW", "hwnd", $hwnd, "uint", $imsg, $wparamtype, $wparam, $lparamtype, $lparam)
;~  If @error Then Return SetError(@error, @extended, "")
;~  If $ireturn >= 0 And $ireturn <= 4 Then Return $aresult[$ireturn]
;~  Return $aresult
;~ EndFunc   ;==>_SendMessage

;~ Func _WinAPI_FindWindow($sclassname, $swindowname)
;~  Local $aresult = DllCall("user32.dll", "hwnd", "FindWindowW", "wstr", $sclassname, "wstr", $swindowname)
;~  If @error Then Return SetError(@error, @extended, 0)
;~  Return $aresult[0]
;~ EndFunc   ;==>_WinApi_FindWindow

 

Regards,
 

Link to comment
Share on other sites

On 2017/8/17 at 7:29 PM, KaFu said:

 

What's the mean of this code please,Not find the message 0x5b4 in MSDN. by the way,how to at other pepole in this forum.

_SendMessage($hSysTray_Handle[0], 0x5B4, 0, 0)
Edited by haijie1223
Link to comment
Share on other sites

  • 1 year later...

dear great script but how can reopen last opened folders.

example  opened  windows explorer folders   c:\test   d:\docs    g:\excel   ..... etc

but never recover restart explorer.

solution script au3 ?

Sincerely.

Link to comment
Share on other sites

drmusti,

First thing you should know is that you wont need to reopen any folders if you have set this option first

After that is set, Any windows Open@d get their own explorer process, Exclusive to the explorer shell process ..And wont get closed the next time you run the script above


launch-file-explorer-seperate-process.pn

Deye

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...