Results 1 to 2 of 2

Thread: [VB6/twinBASIC] Code snippet: Close Explorer window by path

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,269

    [VB6/twinBASIC] Code snippet: Close Explorer window by path

    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).

    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. tbShellLib can be downloaded via the twinBASIC Package Manager; you don't need to download it yourself.

    (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.
    Last edited by fafalone; Nov 27th, 2022 at 02:44 PM.

  2. #2
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,512

    Re: [VB6/twinBASIC] Code snippet: Close Explorer window by path

    Thanks Faf! Very useful once again.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width