title: python user32.GetMessage never exits
body: <p>for some of my python applications i'm installing system hooks using ctypes and the windows API, in order for these to work a message loop must be running. because I have other things happening in the foreground this message loop is set to run as a background thread. this part works fine, however when i want to stop this background thread I can't get it to exit, the GetMessage function should return 0 when the WM_QUIT message is received and the body of my message loop should but for some reason it never does.
  I've tried various combinations of SendMessage, PostMessage, PostThreadMessage and PostQuitMessage with no success.</p>
  
  <pre><code>#!python3
  
  import tkinter as tk
  import ctypes
  import ctypes.wintypes
  from threading import Thread
  
  user32 = ctypes.windll.user32
  
  def loop():
      msg = ctypes.wintypes.MSG()
      while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
          print('msg')
          user32.TranslateMessageW(msg)
          user32.DispatchMessageW(msg)
      print('end of loop')
  
  def SendMessage1():
      user32.SendMessageA(0xFFFF, 0x001C, 0, 0) # broadcast handle, app_activate message
  
  def SendMessage2():
      user32.SendMessageA(0, 0x001C, 0, 0) # null handle
  
  def PostMessage1():
      user32.PostMessageA(0xFFFF, 0x001C, 0, 0)
  
  def PostMessage2():
      user32.PostMessageA(0, 0x001C, 0, 0)
  
  def PostThreadMessage1():
      user32.PostThreadMessageA(0xFFFF, 0x001C, 0, 0)
  
  def PostThreadMessage2():
      user32.PostThreadMessageA(0, 0x001C, 0, 0)
  
  def PostQuitMessage1():
      user32.PostQuitMessage(0)
  
  def PostQuitMessage2():
      user32.PostQuitMessage(0)
  
  root = tk.Tk()
  
  t = Thread(target=loop)
  t.daemon=True
  t.start()
  
  tk.Button(root, text='SendMessage 1', command=SendMessage1).pack()
  tk.Button(root, text='SendMessage 2', command=SendMessage2).pack()
  tk.Button(root, text='PostMessage 1', command=PostMessage1).pack()
  tk.Button(root, text='PostMessage 2', command=PostMessage2).pack()
  tk.Button(root, text='PostThreadMessage 1', command=PostThreadMessage1).pack()
  tk.Button(root, text='PostThreadMessage 2', command=PostThreadMessage2).pack()
  tk.Button(root, text='PostQuitMessage 1', command=PostQuitMessage1).pack()
  tk.Button(root, text='PostQuitMessage 2', command=PostQuitMessage2).pack()
  
  root.mainloop()
  </code></pre>
  
  <p>i've also tried both A and W varients of these functions where they exist. I think the problem may be because I can't get a handle  to the thread thats actually listening for messages and i can't find any documentation for this either.</p>
  
  <p>I also looked into raising an exception in that threads context, but because the hang is in a dll rather than in python code the GIL has been release and this doesn't work to cause it to exit.</p>
  
  <p>how can I make this thread exit gracefully?</p>