none
How to get the path Folder window that is active?

    Question

  • in this topic: "How to display Explorer.exe in a Form?" kaymaf answer me with this code:

    *The following code allows me to display the folders that opens in form, but how can I get the path / name of the windows that my form display ?

    Imports System.Runtime.InteropServices
    
    'API
    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
       
     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
     Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
     End Function
    
    'ENUM
    Public Enum ShowWindowCommands
            SW_HIDE = 0
            SW_SHOWNORMAL = 1
            SW_NORMAL = 1
            SW_SHOWMINIMIZED = 2
            SW_SHOWMAXIMIZED = 3
            SW_MAXIMIZE = 3
            SW_SHOWNOACTIVATE = 4
            SW_SHOW = 5
            SW_MINIMIZE = 6
            SW_SHOWMINNOACTIVE = 7
            SW_SHOWNA = 8
            SW_RESTORE = 9
            SW_SHOWDEFAULT = 10
            SW_FORCEMINIMIZE = 11
            SW_MAX = 11
        End Enum
    
    Sub HostExplorerOnForm()
            Dim ExplorerHandle As IntPtr = FindWindow("CabinetWClass", Nothing)
            If Not ExplorerHandle.Equals(IntPtr.Zero) Then
                SetParent(ExplorerHandle, Me.Panel1.Handle)
                ShowWindow(ExplorerHandle, ShowWindowCommands.SW_NORMAL)
            End If
    
     End Sub









    • Edited by RonDavis Sunday, January 06, 2013 8:24 PM
    Sunday, January 06, 2013 6:26 PM

Answers

  • You could try this

    Option Strict On
    
    Imports System.Collections.Generic
    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Const WM_GETTEXT As Integer = &HD
    
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                         ByVal childAfter As IntPtr, _
                                         ByVal lclassName As String, _
                                         ByVal windowTitle As String) As IntPtr
        End Function
    
        Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Dim FolderPath As String = ""
        Dim FolderPathFixed As String = ""
        
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            FolderPath = ""
            FolderPathFixed = ""
            RichTextBox1.Text = ""
            Dim enumerator As New WindowsEnumerator()
            For Each top As ApiWindow In enumerator.GetTopLevelWindows()
                For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
                    FolderPath += top.MainWindowTitle & "/" & child.ClassName & "/" & child.hWnd.ToString & "/" & child.MainWindowTitle & "////" & vbCrLf & vbCrLf
                Next child
            Next top
            Dim FolderPathData As MatchCollection = Regex.Matches(FolderPath, "Address:(.*?)////", RegexOptions.IgnoreCase)
            For Each Item As Match In FolderPathData
                FolderPath = Item.ToString & vbCrLf
                FolderPath = Regex.Replace(FolderPath, "Address: ", "")
                FolderPath = Regex.Replace(FolderPath, "////", "")
                RichTextBox1.AppendText(FolderPath)
            Next
        End Sub
    
        Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    
        End Sub
    
    End Class
    
    
    Public Class ApiWindow
        Public MainWindowTitle As String = ""
        Public ClassName As String = ""
        Public hWnd As Int32
    End Class
    
    Public Class WindowsEnumerator
    
        Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
    
        Private Declare Function EnumWindows Lib "user32" _
         (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        Private Declare Function EnumChildWindows Lib "user32" _
         (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        Private Declare Function GetClassName _
         Lib "user32" Alias "GetClassNameA" _
         (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    
        Private Declare Function IsWindowVisible Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        Private Declare Function GetParent Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32
    
        Private _listChildren As New List(Of ApiWindow)
        Private _listTopLevel As New List(Of ApiWindow)
    
        Private _topLevelClass As String = ""
        Private _childClass As String = ""
    
        Public Overloads Function GetTopLevelWindows() As List(Of ApiWindow)
            EnumWindows(AddressOf EnumWindowProc, &H0)
            Return _listTopLevel
        End Function
    
        Public Overloads Function GetTopLevelWindows(ByVal className As String) As List(Of ApiWindow)
            _topLevelClass = className
            Return Me.GetTopLevelWindows()
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As List(Of ApiWindow)
            _listChildren = New List(Of ApiWindow)
            EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)
            Return _listChildren
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As List(Of ApiWindow)
            _childClass = childClass
            Return Me.GetChildWindows(hwnd)
        End Function
    
        Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
            If GetParent(hwnd) = 0 AndAlso CBool(IsWindowVisible(hwnd)) Then
                Dim window As ApiWindow = GetWindowIdentification(hwnd)
                If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
                    _listTopLevel.Add(window)
                End If
            End If
            Return 1
        End Function
    
        Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
            Dim window As ApiWindow = GetWindowIdentification(hwnd)
            If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
                _listChildren.Add(window)
            End If
            Return 1
        End Function
    
        Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow
            Const WM_GETTEXT As Int32 = &HD
            Const WM_GETTEXTLENGTH As Int32 = &HE
            Dim window As New ApiWindow()
            Dim title As New StringBuilder()
            Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
            If size > 0 Then
                title = New StringBuilder(size + 1)
                SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
            End If
            Dim classBuilder As New StringBuilder(64)
            GetClassName(hwnd, classBuilder, 64)
            window.ClassName = classBuilder.ToString()
            window.MainWindowTitle = title.ToString()
            window.hWnd = hwnd
            Return window
        End Function
    
    End Class


    You've taught me everything I know but not everything you know.

    Monday, January 07, 2013 6:52 AM

All replies

  • I'm not sure what you are asking. Do you want the path of the folder that explorer is showing? Or do you want the name of a window for some reason?

    You've taught me everything I know but not everything you know.

    Sunday, January 06, 2013 7:25 PM
  • In the image below is my application above an instance of explorer. If you look at explorer you will see it is pointing to C:\Windows\System32\Drivers\Etc and listing the files in that location. My application at the top has retrieved the path information that explorer is pointing to. Is that what you want?

    Note that if you were to click on a file in explorer that my application would not add the filename to the path information.


    You've taught me everything I know but not everything you know.

    Sunday, January 06, 2013 7:36 PM
  • Yes, but I need it to return the path displayed in my form.

    I need the code for your application and an image of your application so I can duplicate your application (unless you can upload your application somewhere for me to download which would be much easier). Then I need to know if you want the path returned manually (via button press and which button to use) or automatically (automatic may not be possible) and where in your form to put it (in a textbox or something). And I won't be able to work on it for some hours because I have some other things to do for awhile.

    Another issue is that it will return a path for any instance of explorer the user is running whether that instance is embedded in your panel or is another window running on the users desktop. Is that a problem? There is no way for me to segregate between those.


    You've taught me everything I know but not everything you know.


    Sunday, January 06, 2013 7:54 PM
  • Sorry,  Forget everything.
    Simple:
    How can I get the path of folder window that is activte? 
    Sunday, January 06, 2013 8:23 PM
  • Sorry,  Forget everything.
    Simple:
    How can I get the path of folder window that is activte? 

    I think you're taking a difficult road to do this, have you looked into articles that explain how to use the treeview control and the IO namespace?

    Take a look at this tutorial.

    http://glassocean.net/how-to-create-a-treeview-file-browser-component-in-vb-net/


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Sunday, January 06, 2013 8:56 PM
  • Sorry,  Forget everything.
    Simple:
    How can I get the path of folder window that is activte? 

    Perhaps this code will work for you. I forgot I had it. All you need is a blank form and copy and paste this code into it. Run the application and double click on a folder or use explorer to open a folder and it will display the path. Sorry about the delay.

    Imports Shell32 ' Add reference browse C:\Windows\System32\Shell32.dll or com Microsoft Shell Controls and Automation
    
    'http://xkom.blogspot.com/2011/06/get-opened-folder-location-in-explorer.html
    
    Public Class Form1
    
        Dim Lb As New ListBox
    
        Dim WithEvents RoutineTimer As New Timer
    
        Private Sub Form1_Load() Handles MyBase.Load
    
            Lb.Parent = Me
            Lb.Dock = DockStyle.Fill
            Me.Text = "Opened Folder by user (explorer)"
            RoutineTimer.Interval = 1000
            RoutineTimer.Start()
    
        End Sub
    
        Sub GetOpenedFolder()
    
            Dim MShell As New Shell
            Dim SFV As ShellFolderView
    
            Lb.Items.Clear()
            On Error Resume Next
            For Each o In MShell.Windows
                If TypeName(o.document) <> "HTMLDocument" Then
                    SFV = o.document
                    If SFV.Folder.Items.Count > 0 Then
                        Lb.Items.Add(TrimPath(CType(SFV.Folder.Items(0), ShellFolderItem).Path))
                    End If
                End If
            Next
    
        End Sub
    
        Sub Timer_Job() Handles RoutineTimer.Tick
            GetOpenedFolder()
        End Sub
    
        Function TrimPath(ByRef s As String) As String
            Return s.Remove(InStrRev(s, "\"))
        End Function
    
    End Class


    You've taught me everything I know but not everything you know.

    Sunday, January 06, 2013 9:13 PM
  • Sorry,  Forget everything.
    Simple:
    How can I get the path of folder window that is activte? 

    I think you're taking a difficult road to do this, have you looked into articles that explain how to use the treeview control and the IO namespace?

    Take a look at this tutorial.

    http://glassocean.net/how-to-create-a-treeview-file-browser-component-in-vb-net/


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Paul

    He wanted explorer embedded in his application per this thread How to display Explorer.exe in a Form? so he would have explorers other capabilities too such as burn, etc. But I suppose he now wants to retrieve the folder path of any folder that explorer has opened. I like that treeview tutorial though, it's pretty awesome.


    You've taught me everything I know but not everything you know.

    Sunday, January 06, 2013 9:46 PM
  • The code below will get all open folder paths and add them to listview. 

    Imports System.Runtime.InteropServices
    
    <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
        End Function
    
     <DllImport("user32.dll", SetLastError:=False)> _
      Private Shared Function GetDesktopWindow() As IntPtr
        End Function
       
     Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr
     Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
     
    <DllImport("user32.dll", EntryPoint:="FindWindowEx")> _
         Private Shared Function FindWindowEx(ByVal ParentHandle As IntPtr, ByVal ChildAfter As Integer, ByVal ClassName As String, ByVal WindowName As String) As IntPtr
        End Function
    
    <DllImport("user32.dll", EntryPoint:="SendMessage")> _
       Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal sb As StringBuilder) As Integer
        End Function
    
    Enum GetWindow_Cmd As UInteger
            GW_HWNDFIRST = 0
            GW_HWNDLAST = 1
            GW_HWNDNEXT = 2
            GW_HWNDPREV = 3
            GW_OWNER = 4
            GW_CHILD = 5
            GW_ENABLEDPOPUP = 6
        End Enum
    
     Sub getAllOpenFolderPathOnVistaAndAbove(ByVal lxb As ListView)
            Dim buff As StringBuilder
            Dim FirstChildHandle As IntPtr = GetWindow(GetDesktopWindow(), GetWindow_Cmd.GW_CHILD)
            Do While Not FirstChildHandle.Equals(IntPtr.Zero)
                Dim wStyle As Integer = GetWindowLong(FirstChildHandle, GWL_STYLE)
                If wStyle And WS_VISIBLE Then
                    buff = New StringBuilder(256)
                    GetClassName(FirstChildHandle, buff, buff.Capacity)
                    Dim WclassName As String = buff.ToString
                    If Not String.IsNullOrEmpty(WclassName) Then
                        If WclassName = "CabinetWClass" Then
                            Dim workerWindow As IntPtr = FindWindowEx(FirstChildHandle, 0, "WorkerW", Nothing)
                            If Not workerWindow.Equals(IntPtr.Zero) Then
                                ' MsgBox("WorkerW: " & workerWindow.ToString)
                                Dim rbWindow32 As IntPtr = FindWindowEx(workerWindow, 0, "ReBarWindow32", Nothing)
                                If Not rbWindow32.Equals(IntPtr.Zero) Then
                                    Dim abr As IntPtr = FindWindowEx(rbWindow32, 0, "Address Band Root", Nothing)
                                    If Not abr.Equals(IntPtr.Zero) Then
                                        Dim msp As IntPtr = FindWindowEx(abr, 0, "msctls_progress32", Nothing)
                                        If Not msp.Equals(IntPtr.Zero) Then
                                            Dim cmb32 As IntPtr = FindWindowEx(msp, 0, "ComboBoxEx32", Nothing)
                                            If Not cmb32.Equals(IntPtr.Zero) Then
                                                ' MsgBox("Edit: " & edit.ToString)
                                                Dim returnValue As Integer = SendMessage(cmb32, WM_GETTEXTLENGTH, 0, 0)
                                                If returnValue > 0 Then
                                                    buff = New StringBuilder(256)
                                                    SendMessage(cmb32, WM_GETTEXT, buff.Capacity, buff)
                                                    lxb.Items.Add(buff.ToString)
                                                End If
                                            End If 'Not cmb32.Equals(IntPtr.Zero)
                                        End If 'Not msp.Equals(IntPtr.Zero)
                                    End If 'Not abr.Equals(IntPtr.Zero)
                                End If 'Not rbWindow32.Equals(IntPtr.Zero)
                            End If 'Not workerWindow.Equals(IntPtr.Zero)
                        End If 'WclassName = "CabinetWClass"
                    End If 'Not String.IsNullOrEmpty(WclassName)
                End If ' end of wStyle And WS_VISIBLE
                FirstChildHandle = GetWindow(FirstChildHandle, GetWindow_Cmd.GW_HWNDNEXT)
            Loop 'end of loop
        End Sub
    
    '==========USAGE===
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            getAllOpenFolderPathOnVistaAndAbove(ListView1)
    End Sub
    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Sunday, January 06, 2013 10:19 PM
  • The code below will get all open folder paths and add them to listview. 

    Imports System.Runtime.InteropServices
    
    <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
        End Function
    
     <DllImport("user32.dll", SetLastError:=False)> _
      Private Shared Function GetDesktopWindow() As IntPtr
        End Function
       
     Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr
     Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
     
    <DllImport("user32.dll", EntryPoint:="FindWindowEx")> _
         Private Shared Function FindWindowEx(ByVal ParentHandle As IntPtr, ByVal ChildAfter As Integer, ByVal ClassName As String, ByVal WindowName As String) As IntPtr
        End Function
    
    <DllImport("user32.dll", EntryPoint:="SendMessage")> _
       Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal sb As StringBuilder) As Integer
        End Function
    
    Enum GetWindow_Cmd As UInteger
            GW_HWNDFIRST = 0
            GW_HWNDLAST = 1
            GW_HWNDNEXT = 2
            GW_HWNDPREV = 3
            GW_OWNER = 4
            GW_CHILD = 5
            GW_ENABLEDPOPUP = 6
        End Enum
    
     Sub getAllOpenFolderPathOnVistaAndAbove(ByVal lxb As ListView)
            Dim buff As StringBuilder
            Dim FirstChildHandle As IntPtr = GetWindow(GetDesktopWindow(), GetWindow_Cmd.GW_CHILD)
            Do While Not FirstChildHandle.Equals(IntPtr.Zero)
                Dim wStyle As Integer = GetWindowLong(FirstChildHandle, GWL_STYLE)
                If wStyle And WS_VISIBLE Then
                    buff = New StringBuilder(256)
                    GetClassName(FirstChildHandle, buff, buff.Capacity)
                    Dim WclassName As String = buff.ToString
                    If Not String.IsNullOrEmpty(WclassName) Then
                        If WclassName = "CabinetWClass" Then
                            Dim workerWindow As IntPtr = FindWindowEx(FirstChildHandle, 0, "WorkerW", Nothing)
                            If Not workerWindow.Equals(IntPtr.Zero) Then
                                ' MsgBox("WorkerW: " & workerWindow.ToString)
                                Dim rbWindow32 As IntPtr = FindWindowEx(workerWindow, 0, "ReBarWindow32", Nothing)
                                If Not rbWindow32.Equals(IntPtr.Zero) Then
                                    Dim abr As IntPtr = FindWindowEx(rbWindow32, 0, "Address Band Root", Nothing)
                                    If Not abr.Equals(IntPtr.Zero) Then
                                        Dim msp As IntPtr = FindWindowEx(abr, 0, "msctls_progress32", Nothing)
                                        If Not msp.Equals(IntPtr.Zero) Then
                                            Dim cmb32 As IntPtr = FindWindowEx(msp, 0, "ComboBoxEx32", Nothing)
                                            If Not cmb32.Equals(IntPtr.Zero) Then
                                                ' MsgBox("Edit: " & edit.ToString)
                                                Dim returnValue As Integer = SendMessage(cmb32, WM_GETTEXTLENGTH, 0, 0)
                                                If returnValue > 0 Then
                                                    buff = New StringBuilder(256)
                                                    SendMessage(cmb32, WM_GETTEXT, buff.Capacity, buff)
                                                    lxb.Items.Add(buff.ToString)
                                                End If
                                            End If 'Not cmb32.Equals(IntPtr.Zero)
                                        End If 'Not msp.Equals(IntPtr.Zero)
                                    End If 'Not abr.Equals(IntPtr.Zero)
                                End If 'Not rbWindow32.Equals(IntPtr.Zero)
                            End If 'Not workerWindow.Equals(IntPtr.Zero)
                        End If 'WclassName = "CabinetWClass"
                    End If 'Not String.IsNullOrEmpty(WclassName)
                End If ' end of wStyle And WS_VISIBLE
                FirstChildHandle = GetWindow(FirstChildHandle, GetWindow_Cmd.GW_HWNDNEXT)
            Loop 'end of loop
        End Sub
    
    '==========USAGE===
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            getAllOpenFolderPathOnVistaAndAbove(ListView1)
    End Sub
    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    I get the following errors:

    Error 1 'GWL_STYLE' is not declared. It may be inaccessible due to its protection level.

    Error 2 'WS_VISIBLE' is not declared. It may be inaccessible due to its protection level.

    Error 3 Value of type 'System.Text.StringBuilder' cannot be converted to 'String'.

    Error 4 'WM_GETTEXTLENGTH' is not declared. It may be inaccessible due to its protection level.

    Error 5 'WM_GETTEXT' is not declared. It may be inaccessible due to its protection level.

    :)
    Sunday, January 06, 2013 10:38 PM
  • Private Const WM_GETTEXTLENGTH As Integer = &HE
        Private Const WM_GETTEXT As Integer = &HD
        Private Const GWL_STYLE As Integer = (-16)
        Private Const WS_VISIBLE As Integer = &H10000000   'Visible

    kaymaf

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Sunday, January 06, 2013 10:46 PM
  • Error 3

    Value of type 'System.Text.StringBuilder' cannot be converted to 'String'.

    Try to change the


    SendMessage(cmb32, WM_GETTEXT, buff.Capacity, buff)


    To


    SendMessage(cmb32, WM_GETTEXT, DirectCast(buff.Capacity, Integer), buff)

    kaymaf

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Sunday, January 06, 2013 11:08 PM
  • Error 3

    Value of type 'System.Text.StringBuilder' cannot be converted to 'String'.

    Try to change the


    SendMessage(cmb32, WM_GETTEXT, buff.Capacity, buff)


    To


    SendMessage(cmb32, WM_GETTEXT, DirectCast(buff.Capacity, Integer), buff)

    kaymaf

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    http://i45.tinypic.com/27z9cif.png

    Was not a problem at all with next line: "SendMessage(cmb32, WM_GETTEXT, buff.Capacity, buff)"

    My mistake, I did not say where the error.

    anyway, look the pic above to see the errors (2 only)

    thx.

    Sunday, January 06, 2013 11:27 PM
  • Both errors are due to wrong API. The code needs 2 overloads SendMessage. Try to add the code below.

      ==ADD==THIS TO THE PROJECT
     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function

    =========REPLACE THIS API WITH PREVIOUS GETCLASSNAME====

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
         Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
            ' Leave function empty     
        End Sub

    =======The above SENDMESSAGE API will fix this block==
    =======Try to replace this block 

     Dim returnValue As IntPtr = SendMessage(cmb32, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
     If Not returnValue.Equals(IntPtr.Zero) Then
      buff = New StringBuilder(256)
      SendMessage(cmb32, WM_GETTEXT, DirectCast(buff.Capacity, Integer), buff)
      lxb.Items.Add(buff.ToString)
     End If

      
    kaymaf

     


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Monday, January 07, 2013 12:03 AM
  • You could try this

    Option Strict On
    
    Imports System.Collections.Generic
    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Const WM_GETTEXT As Integer = &HD
    
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                         ByVal childAfter As IntPtr, _
                                         ByVal lclassName As String, _
                                         ByVal windowTitle As String) As IntPtr
        End Function
    
        Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Dim FolderPath As String = ""
        Dim FolderPathFixed As String = ""
        
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            FolderPath = ""
            FolderPathFixed = ""
            RichTextBox1.Text = ""
            Dim enumerator As New WindowsEnumerator()
            For Each top As ApiWindow In enumerator.GetTopLevelWindows()
                For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
                    FolderPath += top.MainWindowTitle & "/" & child.ClassName & "/" & child.hWnd.ToString & "/" & child.MainWindowTitle & "////" & vbCrLf & vbCrLf
                Next child
            Next top
            Dim FolderPathData As MatchCollection = Regex.Matches(FolderPath, "Address:(.*?)////", RegexOptions.IgnoreCase)
            For Each Item As Match In FolderPathData
                FolderPath = Item.ToString & vbCrLf
                FolderPath = Regex.Replace(FolderPath, "Address: ", "")
                FolderPath = Regex.Replace(FolderPath, "////", "")
                RichTextBox1.AppendText(FolderPath)
            Next
        End Sub
    
        Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    
        End Sub
    
    End Class
    
    
    Public Class ApiWindow
        Public MainWindowTitle As String = ""
        Public ClassName As String = ""
        Public hWnd As Int32
    End Class
    
    Public Class WindowsEnumerator
    
        Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
    
        Private Declare Function EnumWindows Lib "user32" _
         (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        Private Declare Function EnumChildWindows Lib "user32" _
         (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        Private Declare Function GetClassName _
         Lib "user32" Alias "GetClassNameA" _
         (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    
        Private Declare Function IsWindowVisible Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        Private Declare Function GetParent Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32
    
        Private _listChildren As New List(Of ApiWindow)
        Private _listTopLevel As New List(Of ApiWindow)
    
        Private _topLevelClass As String = ""
        Private _childClass As String = ""
    
        Public Overloads Function GetTopLevelWindows() As List(Of ApiWindow)
            EnumWindows(AddressOf EnumWindowProc, &H0)
            Return _listTopLevel
        End Function
    
        Public Overloads Function GetTopLevelWindows(ByVal className As String) As List(Of ApiWindow)
            _topLevelClass = className
            Return Me.GetTopLevelWindows()
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As List(Of ApiWindow)
            _listChildren = New List(Of ApiWindow)
            EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)
            Return _listChildren
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As List(Of ApiWindow)
            _childClass = childClass
            Return Me.GetChildWindows(hwnd)
        End Function
    
        Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
            If GetParent(hwnd) = 0 AndAlso CBool(IsWindowVisible(hwnd)) Then
                Dim window As ApiWindow = GetWindowIdentification(hwnd)
                If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
                    _listTopLevel.Add(window)
                End If
            End If
            Return 1
        End Function
    
        Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
            Dim window As ApiWindow = GetWindowIdentification(hwnd)
            If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
                _listChildren.Add(window)
            End If
            Return 1
        End Function
    
        Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow
            Const WM_GETTEXT As Int32 = &HD
            Const WM_GETTEXTLENGTH As Int32 = &HE
            Dim window As New ApiWindow()
            Dim title As New StringBuilder()
            Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
            If size > 0 Then
                title = New StringBuilder(size + 1)
                SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
            End If
            Dim classBuilder As New StringBuilder(64)
            GetClassName(hwnd, classBuilder, 64)
            window.ClassName = classBuilder.ToString()
            window.MainWindowTitle = title.ToString()
            window.hWnd = hwnd
            Return window
        End Function
    
    End Class


    You've taught me everything I know but not everything you know.

    Monday, January 07, 2013 6:52 AM
  • kaymaf: 

    the code blow does not display any errors, but when I press the button nothing happens.
    Any event, the solution of Mr. Monkeyboy works great.
    But again, thank you very much for your help!

    Mr. Monkeyboy's: Thank you very much, it works great.

    kaymaf code:

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public Class Form1
        Private Const WM_GETTEXTLENGTH As Integer = &HE
        Private Const WM_GETTEXT As Integer = &HD
        Private Const GWL_STYLE As Integer = (-16)
        Private Const WS_VISIBLE As Integer = &H10000000   'Visible
        <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
        End Function
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=False)> _
        Private Shared Function GetDesktopWindow() As IntPtr
        End Function
    
        Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr
        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
            ' Leave function empty     
        End Sub
        <DllImport("user32.dll", EntryPoint:="FindWindowEx")> _
        Private Shared Function FindWindowEx(ByVal ParentHandle As IntPtr, ByVal ChildAfter As Integer, ByVal ClassName As String, ByVal WindowName As String) As IntPtr
        End Function
    
        <DllImport("user32.dll", EntryPoint:="SendMessage")> _
        Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal sb As StringBuilder) As Integer
        End Function
    
        Enum GetWindow_Cmd As UInteger
            GW_HWNDFIRST = 0
            GW_HWNDLAST = 1
            GW_HWNDNEXT = 2
            GW_HWNDPREV = 3
            GW_OWNER = 4
            GW_CHILD = 5
            GW_ENABLEDPOPUP = 6
        End Enum
    
        Sub getAllOpenFolderPathOnVistaAndAbove(ByVal lxb As ListView)
            Dim buff As StringBuilder
            Dim FirstChildHandle As IntPtr = GetWindow(GetDesktopWindow(), GetWindow_Cmd.GW_CHILD)
            Do While Not FirstChildHandle.Equals(IntPtr.Zero)
                Dim wStyle As Integer = GetWindowLong(FirstChildHandle, GWL_STYLE)
                If wStyle And WS_VISIBLE Then
                    buff = New StringBuilder(256)
                    GetClassName(FirstChildHandle, buff, buff.Capacity)
                    Dim WclassName As String = buff.ToString
                    If Not String.IsNullOrEmpty(WclassName) Then
                        If WclassName = "CabinetWClass" Then
                            Dim workerWindow As IntPtr = FindWindowEx(FirstChildHandle, 0, "WorkerW", Nothing)
                            If Not workerWindow.Equals(IntPtr.Zero) Then
                                ' MsgBox("WorkerW: " & workerWindow.ToString)
                                Dim rbWindow32 As IntPtr = FindWindowEx(workerWindow, 0, "ReBarWindow32", Nothing)
                                If Not rbWindow32.Equals(IntPtr.Zero) Then
                                    Dim abr As IntPtr = FindWindowEx(rbWindow32, 0, "Address Band Root", Nothing)
                                    If Not abr.Equals(IntPtr.Zero) Then
                                        Dim msp As IntPtr = FindWindowEx(abr, 0, "msctls_progress32", Nothing)
                                        If Not msp.Equals(IntPtr.Zero) Then
                                            Dim cmb32 As IntPtr = FindWindowEx(msp, 0, "ComboBoxEx32", Nothing)
                                            If Not cmb32.Equals(IntPtr.Zero) Then
                                                ' MsgBox("Edit: " & edit.ToString)
                                                Dim returnValue As IntPtr = SendMessage(cmb32, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
                                                If Not returnValue.Equals(IntPtr.Zero) Then
                                                    buff = New StringBuilder(256)
                                                    SendMessage(cmb32, WM_GETTEXT, DirectCast(buff.Capacity, Integer), buff)
                                                    lxb.Items.Add(buff.ToString)
                                                End If
                                            End If 'Not cmb32.Equals(IntPtr.Zero)
                                        End If 'Not msp.Equals(IntPtr.Zero)
                                    End If 'Not abr.Equals(IntPtr.Zero)
                                End If 'Not rbWindow32.Equals(IntPtr.Zero)
                            End If 'Not workerWindow.Equals(IntPtr.Zero)
                        End If 'WclassName = "CabinetWClass"
                    End If 'Not String.IsNullOrEmpty(WclassName)
                End If ' end of wStyle And WS_VISIBLE
                FirstChildHandle = GetWindow(FirstChildHandle, GetWindow_Cmd.GW_HWNDNEXT)
            Loop 'end of loop
        End Sub
    
        '==========USAGE===
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            getAllOpenFolderPathOnVistaAndAbove(ListView1)
    
    
        End Sub
    
    
    End Class
    



    Monday, January 07, 2013 7:59 AM
  • You're welcome. If you'll note I accidentally left a variable (FolderPathFixed) within the code which is never used I believe. :(

    Thanks for marking me as the answer.


    You've taught me everything I know but not everything you know.

    Monday, January 07, 2013 12:51 PM