hendrikhe Posted August 17, 2017 Share Posted August 17, 2017 (edited) 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) Edited August 17, 2017 by hendrikhe Link to comment Share on other sites More sharing options...
Deye Posted August 17, 2017 Share Posted August 17, 2017 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 Trong 1 Link to comment Share on other sites More sharing options...
KaFu Posted August 17, 2017 Share Posted August 17, 2017 Try this one: expandcollapse popup#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 Trong 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Trong Posted August 17, 2017 Share Posted August 17, 2017 Restart Windows Explorer: expandcollapse popup#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 More sharing options...
hendrikhe Posted August 17, 2017 Author Share Posted August 17, 2017 Thank you all for fast reply! Much appreciated. Tried Deye solution and worked like a charm! Link to comment Share on other sites More sharing options...
haijie1223 Posted August 24, 2017 Share Posted August 24, 2017 (edited) 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 August 24, 2017 by haijie1223 Link to comment Share on other sites More sharing options...
KaFu Posted August 24, 2017 Share Posted August 24, 2017 0x5b4 = undocumented WM_USER message (WM_USER + 0x1B4) https://stackoverflow.com/questions/5689904/gracefully-exit-explorer-programmatically/6246182#6246182 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
haijie1223 Posted August 25, 2017 Share Posted August 25, 2017 On 2017/8/24 at 11:00 PM, KaFu said: 0x5b4 =未记录的WM_USER消息(WM_USER + 0x1B4) https://stackoverflow.com/questions/5689904/gracefully-exit-explorer-programmatically/6246182#6246182 thanks Link to comment Share on other sites More sharing options...
drmusti Posted March 8, 2019 Share Posted March 8, 2019 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 More sharing options...
Deye Posted March 8, 2019 Share Posted March 8, 2019 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 Deye Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now