title: Windows Hwnd Handle Choose File
body: <p>I have a web app that successfully gets to a page and clicks an "Upload File" button.</p>
  
  <p>My app also successfully handles pop-up windows by monitoring and hooking them.  For the most part, it's just to click "OK" or "Cancel" buttons.  Buttons are easy.</p>
  
  <p>What I need help with is the Choose-File dialog.  I'm hooking it fine, but there are a lot of controls on it and I need some direction.</p>
  
  <p><img src="https://i.stack.imgur.com/jpsRd.jpg" alt="Choose File Dialog Example"></p>
  
  <p>These are the child controls on it:</p>
  
  <pre><code>DUIViewWndClassName,DirectUIHWND,CtrlNotifySink,NamespaceTreeControl,Static,SysTreeView32,CtrlNotifySink,Shell Preview Extension Host,CtrlNotifySink,SHELLDLL_DefView,DirectUIHWND,CtrlNotifySink,ScrollBar,CtrlNotifySink,ScrollBar,Static,Static,Static,ListBox,Static,Static,ComboBoxEx32,ComboBox,Edit,Static,ComboBox,Button,Button,Button,ScrollBar,WorkerW,ReBarWindow32,TravelBand,ToolbarWindow32,Address Band Root,msctls_progress32,Breadcrumb Parent,ToolbarWindow32,ToolbarWindow32,UniversalSearchBand,Search Box,SearchEditBoxWrapperClass,DirectUIHWND
  </code></pre>
  
  <p>I would be happy with sticking an exact path/file into the File-Name textbox/combobox and clicking "Open".  The button part is easy, but I don't know either how to select files in the window, and/or how to put my path into the File-Name entry field.</p>
  
  <p>Right now I have something like this:</p>
  
  <pre><code>&lt;DllImport("user32.dll")&gt; _
  Private Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Int32) As Int32
  End Function
  
  &lt;DllImport("user32.dll")&gt; _
  Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal text As StringBuilder, ByVal maxLength As Int32) As Int32
  End Function
  
  &lt;DllImport("user32.dll")&gt; _
  Private Shared Function GetDlgCtrlID(ByVal hwndCtl As IntPtr) As Integer
  End Function
  
  ....
  
  Private Shared Function hwndHandler() As Int32
      Dim ptrButtonhwnd As IntPtr
  
      For Each pChild As IntPtr In Interop.ChildWindows(pPopup.hwnd)
          Dim sbControl As New StringBuilder(255)
          GetClassName(pChild, sbControl, sbControl.Capacity)
          If "Button".Equals(sbControl.ToString()) Then
              Dim sbText As New StringBuilder(255)
              GetWindowText(pChildOfDialog, sbText, sbText.Capacity)
              If "&amp;Open".Equals(sbText.ToString()) Then
                  ptrButtonHwnd = pChild
              End If
          End If
      Next
  
      If ptrButtonHwnd &lt;&gt; IntPtr.Zero Then
          Dim ctrlId As Int32 = GetDlgCtrlID(ptrButtonHwnd)
          SendMessage(pPopup.hwnd, WM_COMMAND, New IntPtr(ctrlId), ptrButtonHwnd)
          Return 1
      End If
  
  Return 0
  End Function
  </code></pre>
  
  <p>This works fine, but I need to add something to select a file to open either by inputting it into the text/combo field, or by selecting it in the window.</p>