Here's a small demo of how to use IShellWindows and IWebBrowser2 to close an open Explorer window based on it's path. I wrote it to be compatible with VB6+oleexp and twinBASIC+tbShellLib (64-bit compatible).
Requirements
VB6: oleexp.tlb v5.1 or newer (released the same day as this snippet), as a reference (IDE only, doesn't need to be redistributed), with mIID.bas (included in the oleexp zip) added as a module.
twinBASIC: Beta 167 or newer, and tbShellLib 2.2.26 (added via Settings->References (twinPACK Packages)), updated along with this snippet.
(Note: oleexp.tlb defines a public alias for LongPtr, so that does not need to be changed)
See Also
[VB6] Get extended details about Explorer windows by getting their IFolderView
This project uses the same IShellWindows and related interfaces to enumerate and display far more details about open Explorer windows.
Code:
Private Function LPWSTRtoStr(lPtr As LongPtr, Optional ByVal fFree As Boolean = True) As String
SysReAllocStringW VarPtr(LPWSTRtoStr), lPtr
If fFree Then
Call CoTaskMemFree(lPtr)
End If
End Function
Private Sub CloseExplorerWindowByPath(sPath As String)
On Error GoTo e0
Dim pWindows As ShellWindows
Set pWindows = New ShellWindows
Dim pWB2 As IWebBrowser2
#If TWINBASIC Then
Dim pDisp As IDispatch
#Else
Dim pDisp As oleexp.IDispatch
#End If
Dim pSP As IServiceProvider
Dim pSB As IShellBrowser
Dim pSView As IShellView
Dim pFView As IFolderView2
Dim pFolder As IShellItem
Dim lpPath As LongPtr, sCurPath As String
Dim nCount As Long
Dim i As Long
Dim hr As Long
nCount = pWindows.Count
If nCount < 1 Then
Debug.Print "No open Explorer windows found."
Exit Sub
End If
For i = 0 To nCount - 1
Set pDisp = pWindows.Item(i)
If (pDisp Is Nothing) = False Then
Set pSP = pDisp
If (pSP Is Nothing) = False Then
pSP.QueryService SID_STopLevelBrowser, IID_IShellBrowser, pSB
If (pSB Is Nothing) = False Then
pSB.QueryActiveShellView pSView
If (pSView Is Nothing) = False Then
Set pFView = pSView
If (pFView Is Nothing) = False Then
pFView.GetFolder IID_IShellItem, pFolder
pFolder.GetDisplayName SIGDN_FILESYSPATH, lpPath
sCurPath = LPWSTRtoStr(lpPath)
Debug.Print "CompPath " & sCurPath & "||" & sPath
If LCase$(sCurPath) = LCase$(sPath) Then
Set pWB2 = pDisp
If (pWB2 Is Nothing) = False Then
pWB2.Quit
Exit Sub
Else
Debug.Print "Couldn't get IWebWebrowser2"
End If
End If
Else
Debug.Print "Couldn't get IFolderView"
End If
Else
Debug.Print "Couldn't get IShellView"
End If
Else
Debug.Print "Couldn't get IShellBrowser"
End If
Else
Debug.Print "Couldn't get IServiceProvider"
End If
Else
Debug.Print "Couldn't get IDispatch"
End If
Next
Debug.Print "Couldn't find path."
Exit Sub
e0:
Debug.Print "CloseExplorerPathByWindow.Error->0x" & Hex$(Err.Number) & ", " & Err.Description
End Sub
Requirements
VB6: oleexp.tlb v5.1 or newer (released the same day as this snippet), as a reference (IDE only, doesn't need to be redistributed), with mIID.bas (included in the oleexp zip) added as a module.
twinBASIC: Beta 167 or newer, and tbShellLib 2.2.26 (added via Settings->References (twinPACK Packages)), updated along with this snippet.
(Note: oleexp.tlb defines a public alias for LongPtr, so that does not need to be changed)
See Also
[VB6] Get extended details about Explorer windows by getting their IFolderView
This project uses the same IShellWindows and related interfaces to enumerate and display far more details about open Explorer windows.