title: How to get list of all window handles in Java (Using JNA)?
body: <p>I am novice for JNA. I am trying to get handles for all the windows including minimised ones. I need <code>HWND</code> of all the windows. I have gone thro the question <a href="https://stackoverflow.com/questions/3188484/windows-how-to-get-a-list-of-all-visible-windows">Windows: how to get a list of all visible windows?</a> which helped me to get list of windows, but it has <code>hWnd</code> type as int. I can't use it with <code>com.sun.jna.platform.win32.User32</code> functions which asks for <code>hWnd</code> of type <code>com.sun.jna.platform.win32.WinDef.HWND</code>. So, Is there any way to get all the window handles of type <code>com.sun.jna.platform.win32.WinDef.HWND</code> rather than int pointer? Finally, why is the difference <code>int</code> and <code>HWND</code>? How does it accept both? I am bit confused. Thanks.</p>
  
  <p>I have the following code(edited from Hovercreft's answer):</p>
  
  <pre><code>    import com.sun.jna.Native;
      import com.sun.jna.Pointer;
      import com.sun.jna.platform.win32.User32;
      import com.sun.jna.platform.win32.WinDef.HWND;
      import com.sun.jna.platform.win32.WinDef.RECT;
      import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
  
      public class TryWithHWND {
  
      public static void main(String[] args) {
          final User32 user32 = User32.INSTANCE;
          user32.EnumWindows(new WNDENUMPROC() {
              int count = 0;
              public boolean callback(HWND hWnd, Pointer arg1) {
                  char[] windowText = new char[512];
                  user32.GetWindowText(hWnd, windowText, 512);
                  String wText = Native.toString(windowText);
                  RECT rectangle = new RECT();
                  user32.GetWindowRect(hWnd, rectangle);
                  // get rid of this if block if you want all windows regardless
                  // of whether
                  // or not they have text
                  // second condition is for visible and non minimised windows
                  if (wText.isEmpty() || !(User32.INSTANCE.IsWindowVisible(hWnd)
                          &amp;&amp; rectangle.left &gt; -32000)) {
                      return true;
                  }
                  System.out.println("Found window with text " + hWnd
                          + ", total " + ++count + " Text: " + wText);
                  return true;
              }
          }, null);
      }
  }
  </code></pre>
  
  <p>I tried to use only(not custom interface) the default <code>User32</code> class. It is working fine. I have doubt, why we are using userdefined interface instead of already existing one? One more thing, There is always difference between userdefined method signature and already existing ones. For instance, the variable <code>windowText</code> is <code>char[]</code>, whereas Hovercraft's variable is of type <code>byte[]</code>. Can anyone explain me? Thanks.</p>