title: Get the title of a window of another program using the process name
body: <p>This question is probably quite basic but I'm having difficulty cracking it. I assume that I will have to use something in <code>ctypes.windll.user32</code>. Bear in mind that I have little to no experience using these libraries or even <code>ctypes</code> as a whole.</p>
  
  <p>I have used this code to list all the window titles, but I have no idea how I am supposed to change this code to get a window title with a process name:</p>
  
  <pre><code>import ctypes
  
  EnumWindows = ctypes.windll.user32.EnumWindows
  EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
  GetWindowText = ctypes.windll.user32.GetWindowTextW
  GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
  IsWindowVisible = ctypes.windll.user32.IsWindowVisible
  
  titles = []
  def foreach_window(hwnd, lParam):
      if IsWindowVisible(hwnd):
          length = GetWindowTextLength(hwnd)
          buff = ctypes.create_unicode_buffer(length + 1)
          GetWindowText(hwnd, buff, length + 1)
          titles.append(buff.value)
      return True
  EnumWindows(EnumWindowsProc(foreach_window), 0)
  
  print(titles)
  </code></pre>
  
  <p>This code is from <a href="https://sjohannes.wordpress.com/2012/03/23/win32-python-getting-all-window-titles/" rel="nofollow">https://sjohannes.wordpress.com/2012/03/23/win32-python-getting-all-window-titles/</a></p>
  
  <p>If my question is unclear, I would like to achieve something like this (just an example - I'm not asking specifically about Spotify):</p>
  
  <pre><code>getTitleOfWindowbyProcessName("spotify.exe") // returns "Avicii - Waiting For Love" (or whatever the title is)
  </code></pre>
  
  <p>A complication that may arise, if there are multiple windows running with the same process name (e.g. multiple chrome windows)</p>
  
  <p>Thank you.</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: To clarify, I want some code that takes a process name and returns a (possibly empty) list of window titles owned by that process as strings.</p>