title: "WindowsError: access violation 000001" on DispatchMessageW in EventLoop
body: <p>I research Python internals and write GUI Generic windows use Windows API.</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  import time
  import threading
  import logging
  import uuid
  
  import ctypes
  import ctypes.wintypes
  
  import const
  
  
  INT = ctypes.wintypes.c_int
  LPVOID = ctypes.wintypes.c_void_p
  HCURSOR = ctypes.wintypes.HANDLE
  LRESULT = ctypes.wintypes.LPARAM
  COLORREF = ctypes.wintypes.DWORD
  PVOID = ctypes.wintypes.c_void_p
  WCHAR = ctypes.wintypes.c_wchar
  BCHAR = ctypes.wintypes.c_wchar
  LPRECT = ctypes.wintypes.POINTER(ctypes.wintypes.RECT)
  LPPOINT = ctypes.wintypes.POINTER(ctypes.wintypes.POINT)
  LPMSG = ctypes.wintypes.POINTER(ctypes.wintypes.MSG)
  UINT_PTR = ctypes.wintypes.HANDLE
  LONG_PTR = ctypes.wintypes.HANDLE
  
  
  #WNDPROC = ctypes.wintypes.WINFUNCTYPE(ctypes.c_long, ctypes.wintypes.HWND, ctypes.wintypes.UINT, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM)
  WNDPROC = ctypes.wintypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int)
  
  
  SW_SHOWNORMAL = 1
  SW_SHOW = 5
  
  CS_HREDRAW = 2
  CS_VREDRAW = 1
  
  CW_USEDEFAULT = 0x80000000
  
  WM_DESTROY = 2
  
  
  class WNDCLASSEX(ctypes.wintypes.Structure):
      _fields_ = [("cbSize", ctypes.wintypes.UINT),
                  ("style", ctypes.wintypes.UINT),
                  ("lpfnWndProc", WNDPROC),
                  ("cbClsExtra", ctypes.wintypes.INT),
                  ("cbWndExtra", ctypes.wintypes.INT),
                  ("hInstance", ctypes.wintypes.HANDLE),
                  ("hIcon", ctypes.wintypes.HANDLE),
                  ("hCursor", ctypes.wintypes.HANDLE),
                  ("hBrush", ctypes.wintypes.HANDLE),
                  ("lpszMenuName", ctypes.wintypes.LPCWSTR),
                  ("lpszClassName", ctypes.wintypes.LPCWSTR),
                  ("hIconSm", ctypes.wintypes.HANDLE)]
  
  
  class Window(object):
      def __init__(self, app, wclassName=None, wname=u"Custom Title"):
          self.__app = app
          #
          self.__log = logging.getLogger("win_ui.window")
          #
          self.__hInst = app.getInstance()
          self.__hWnd = None
          #
          if wclassName is None:
              wclassName = u"NewWindowClass-{uid}".format(uid=uuid.uuid4().hex)
          #
          self.__wclassName = wclassName
          self.__wname = wname
          #
          #self.__event_dispatcher = EventDispatcher(app=app)
          #self.__event_dispatcher.start()
          #
          self.__has_exit = threading.Condition()
          #
          self._create()
  
      def wnd_proc(self, hWnd, Msg, wParam, lParam):
          self.__log.debug("WndProc: hWnd={hWnd!r}, Msg={Msg!r}, wParam={wParam!r}, lParam={lParam!r}".format(hWnd=hWnd, Msg=Msg, wParam=wParam, lParam=lParam))
          #self.__event_dispatcher.push((hWnd, Msg, wParam, lParam))
          #
          result = None
  
          #
          self.__has_exit.acquire()
          self.__has_exit.wait(0.1)
          #
          if Msg == WM_DESTROY:
              self.__app.loadLibrary("user32").PostQuitMessage(0)
              result = 0
          #
          if result is None:
              result = self.__app.loadLibrary("user32").DefWindowProcW(hWnd, Msg, wParam, lParam)
          #
          self.__has_exit.release()
          #
          return result
  
  
      def _create(self):
          WndProc = WNDPROC(self.wnd_proc)
  
          hBrush = self.__app.loadLibrary("gdi32").GetStockObject(const.LTGRAY_BRUSH)
          print hBrush
  
          wndClass = WNDCLASSEX()
          wndClass.cbSize = ctypes.sizeof(WNDCLASSEX)
          wndClass.style = CS_HREDRAW | CS_VREDRAW
          wndClass.lpfnWndProc = WndProc
          wndClass.cbClsExtra = 0
          wndClass.cbWndExtra = 0
          wndClass.hInstance = self.__hInst
          wndClass.hIcon = 0 #self.__app.loadIcon()
          wndClass.hCursor = 0 #self.__app.loadCursor()
          wndClass.hBrush = hBrush
          wndClass.lpszMenuName = None
          wndClass.lpszClassName = ctypes.c_wchar_p(self.__wclassName)
          wndClass.hIconSm = 0 #self.__app.loadCursor()
  
          self.__log.debug("wndClass.cbSize = {cbSize!r}".format(cbSize=wndClass.cbSize))
          self.__log.debug("wndClass.lpfnWndProc = {lpfnWndProc!r}".format(lpfnWndProc=wndClass.lpfnWndProc))
          self.__log.debug("wndClass.hInstance = {hInst!r}".format(hInst=wndClass.hInstance))
          self.__log.debug("wndClass.lpszClassName = {lpszClassName!r}".format(lpszClassName=wndClass.lpszClassName))
  
          result = self.__app.loadLibrary("user32").RegisterClassExW(ctypes.byref(wndClass))
          self.__log.debug("0x0{result:x} = RegisterClassExW(...)".format(result=result))
  
          #
          self.__x = CW_USEDEFAULT
          self.__y = CW_USEDEFAULT
          self.__width = 300
          self.__height = 300
          #
          exStyle = 0
          #exStyle = const.WS_EX_APPWINDOW
          style = const.WS_OVERLAPPEDWINDOW
          #style = const.WS_OVERLAPPEDWINDOW | const.WS_CAPTION | const.WS_VISIBLE
          wclassName = ctypes.c_wchar_p(self.__wclassName)
          wname = ctypes.c_wchar_p(self.__wname)
          print wname
          #
          self.__app.loadLibrary("user32").CreateWindowExW.restype = ctypes.wintypes.HWND
          self.__app.loadLibrary("user32").CreateWindowExW.argtypes = [ctypes.wintypes.DWORD, ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.wintypes.DWORD, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.wintypes.HWND, ctypes.wintypes.HMENU, ctypes.wintypes.HINSTANCE, ctypes.wintypes.LPVOID]
          #
          self.__hWnd = self.__app.loadLibrary("user32").CreateWindowExW(exStyle, wclassName, wname, style, self.__x, self.__y, self.__width, self.__height, 0, None, self.__hInst, 0)
          self.__log.debug("0x0{hWnd!r} = CreateWindowExW(...)".format(hWnd=self.__hWnd))
          if not self.__hWnd:
              print self.__app.getLastError()
              raise WindowsError("Failed to create window")
  
          self.__log.debug("ShowWindow(hWnd=0x{hWnd:x})".format(hWnd=self.__hWnd))
          result = self.__app.loadLibrary("user32").ShowWindow(self.__hWnd, SW_SHOW)
          self.__log.debug('0x0{result:x} = ShowWindow(...)'.format(result=result))
  
          self.__log.debug('UpdateWindow(hWnd=0x{hWnd:x})'.format(hWnd=self.__hWnd))
          result = self.__app.loadLibrary("user32").UpdateWindow(self.__hWnd)
          self.__log.debug('0x0{result:x} = UpdateWindow(...)'.format(result=result))
  </code></pre>
  
  <p>GUI generic window create but cause exception "WidnowsError: access violation 000001" on DispatchMessageW in EventLoop.</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  import logging
  import uuid
  
  import ctypes
  import ctypes.wintypes
  
  import const
  
  
  class Application(object):
      def __init__(self):
          self.__log = logging.getLogger("win_ui.application")
          self.__libs = {}
          #
          self._event_thread = self.loadLibrary("kernel32").GetCurrentThreadId()
  
      def loadLibrary(self, name):
          if name in self.__libs:
              result = self.__libs[name]
          else:
              module = getattr(ctypes.windll, name)
              result = self.__libs[name] = module
          return result
  
      def getInstance(self, hModule=None):
          self.__log.debug('GetModuleHandleW(hModule={hModule!r})'.format(hModule=hModule))
          result = self.loadLibrary("kernel32").GetModuleHandleW(None)
          self.__log.debug('0x0{result:x} = GetModuleHandleW(...)'.format(result=result))
          return result
  
      def loadIcon(self, iconName=None):
          if iconName is None:
              iconName = const.IDI_APPLICATION
          hInst = self.getInstance()
          result = self.loadLibrary("user32").LoadIconW(hInst, iconName)
          return result
  
      def loadCursor(self, v=None):
          if v is None:
              v = const.IDI_APPLICATION
          hInst = self.getInstance()
          result = self.loadLibrary("user32").LoadCursorW(hInst, v)
          return result
  
      def getLastError(self):
          self.__log.debug('GetLastError()')
          result = self.loadLibrary("kernel32").GetLastError()
          #print result
          #rs = self.loadLibrary("kernel32").FormatMessageW(result)
          #print rs
          self.__log.debug('0x0{result:x} = GetLastError()'.format(result=result))
          return result
  
      def check(self):
          if self._event_thread != self.loadLibrary("kernel32").GetCurrentThreadId():
              raise RuntimeError("Thread?")
  
      def run(self):
          self.check()
  
          msg = ctypes.wintypes.MSG()
          lpmsg = ctypes.pointer(msg)
  
          self.__log.debug('Entering message loop')
          #
          user32 = self.loadLibrary("user32")
          while user32.GetMessageW(lpmsg, 0, 0, 0) != 0:
          #while self.loadLibrary("user32").PeekMessageW(ctypes.byref(msg), 0, 0, 0, const.PM_REMOVE):
              user32.TranslateMessage(lpmsg)
              print lpmsg
              user32.DispatchMessageW(lpmsg)
          #
          self.__log.debug('done.')
  </code></pre>
  
  <p>When I refactoring and calls RegisterWindowEx and CreateWindowEx on separate Python method CreateWindowEx start return 0x0 and GetLastError retrun 0x0. What problem there?</p>