Deye

Loosing handles

1 post in this topic

#1 ·  Posted

Hi,

I'm trying to use this just as an example to try and better understand what is happening in the background when running this
and how to relink every thing back to normal, optimal state

As you can see this will first retrieve the full path to the open explorer windows 
next it will exit the explorer main shell process "gracefully"

now there are 2 things look for :

1. To bring back the explorer main shell I've uses shellexecute with a termination to its pid the system then did a reload of its own .. ,and by doing this I noticed the system tray icons all came back to normal, as not with the initial shellexecute run ..

2. With second time of retrieving the left over objects (explorer windows) apparently some of their handles are not functional anymore.

Trying to find a better way to reload the shell and find a way to get those handles back , I have tried a few things with various API functions but since I know not much of the options and possibilities I gave it a rest for now. and now, see what is your take about this process.

Thanks

#include <Array.au3>
#include <WinAPI.au3>
#include <WinAPIShellEx.au3>

Local $aWin = WinList("[CLASS:CabinetWClass;]")
If Not IsArray($aWin) Then Exit

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

ReDim $aWin[UBound($aWin)][3]

For $i = 1 To $aWin[0][0]
    $aWin[$i][2] = _ExprWinPath($aWin[$i][1])
Next

;column 2 should show full paths
_ArrayDisplay($aWin)

;Shutting down explorer gracefully
Local $hSysTray_Handle = DllCall("user32.dll", "HWND", "FindWindow", "str", "Shell_TrayWnd", "str", "")
_SendMessage($hSysTray_Handle[0], 0x5B4, 0, 0)
Sleep(1000)

$iPID = ShellExecute(@WindowsDir & "\Explorer.exe", '', @WindowsDir)
Sleep(1000)

; Doing this simply helped restoring things to there original state, like : all tray icons
ProcessClose($iPID)
Sleep(1000)

Local $aWin = WinList("[CLASS:CabinetWClass;]")
ReDim $aWin[UBound($aWin)][3]

For $i = 1 To $aWin[0][0]
    $aWin[$i][2] = _ExprWinPath($aWin[$i][1])
Next

;column 2 , failing handles 
_ArrayDisplay($aWin)

Func _ExprWinPath($hWnd) ;https://www.autoitscript.com/forum/topic/89833-windows-explorer-current-folder/
    Local $sFolder
    $sFolder = _ObjectSHFolder($hWnd)
    If IsObj($sFolder) Then
        If StringInStr($sFolder.Folder.Self.Path, '\0') Then Return False
        Return $sFolder.Folder.Self.Path
    EndIf
    Return False
EndFunc   ;==>_ExprWinPath

Func _ObjectSHFolder($hWnd)
    Local $oShell, $oShellWindows, $oIEObject, $sFolder
    $oShell = ObjCreate("Shell.Application")
    $oShellWindows = $oShell.Windows()
    For $oIEObject In $oShellWindows
        If $oIEObject.HWND = $hWnd Then
            $sFolder = $oIEObject.Document
            If IsObj($sFolder) Then Return $sFolder
        EndIf
    Next
EndFunc   ;==>_ObjectSHFolder

 

Share this post


Link to post
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