From f141fccd86ded08e503fb45463cd7d35d139a0f2 Mon Sep 17 00:00:00 2001
From: Athanasius <Athanasius@miggy.org>
Date: Thu, 17 Nov 2022 17:39:09 +0000
Subject: [PATCH] protocol.py: Change definition of DefWindowProcW to work on
 64-bit

Given this is the form of definition in the official Python docs I'm
wondering if this only ever worked on 32-bit by accident.

So, it was nothing to do with the type needing to be changed for 64-bit.
The error:

    ctypes.ArgumentError: argument 4: <class 'OverflowError'>: int too long to convert

was a red herring in those terms.
---
 protocol.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/protocol.py b/protocol.py
index 290a7031..eddc6391 100644
--- a/protocol.py
+++ b/protocol.py
@@ -106,12 +106,12 @@ if sys.platform == 'darwin' and getattr(sys, 'frozen', False):  # noqa: C901 # i
 
         def handleEvent_withReplyEvent_(self, event, replyEvent) -> None:  # noqa: N802 N803 # Required to override
             """Actual event handling from NSAppleEventManager."""
-            protocolhandler.lasturl = urllib.parse.unquote(  # noqa: F821: type: ignore # Its going to be a DPH in
+            protocolhandler.lasturl = urllib.parse.unquote(  # noqa: F821: type: ignore # It's going to be a DPH in
                 # this code
                 event.paramDescriptorForKeyword_(keyDirectObject).stringValue()
             ).strip()
 
-            protocolhandler.master.after(DarwinProtocolHandler.POLL, protocolhandler.poll)  # noqa: F821: type: ignore
+            protocolhandler.master.after(DarwinProtocolHandler.POLL, protocolhandler.poll)  # noqa: F821 # type: ignore
 
 
 elif (config.auth_force_edmc_protocol
@@ -157,7 +157,13 @@ elif (config.auth_force_edmc_protocol
     CreateWindowExW.restype = HWND
     RegisterClassW = windll.user32.RegisterClassW
     RegisterClassW.argtypes = [POINTER(WNDCLASS)]
-    DefWindowProcW = windll.user32.DefWindowProcW
+    # DefWindowProcW
+    # Ref: <https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-defwindowprocw>
+    # LRESULT DefWindowProcW([in] HWND   hWnd,[in] UINT   Msg,[in] WPARAM wParam,[in] LPARAM lParam);
+    # As per example at <https://docs.python.org/3/library/ctypes.html#ctypes.WINFUNCTYPE>
+    prototype = WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM)
+    paramflags = (1, "hWnd"), (1, "Msg"), (1, "wParam"), (1, "lParam")
+    DefWindowProcW = prototype(("DefWindowProcW", windll.user32), paramflags)
     GetParent = windll.user32.GetParent
     SetForegroundWindow = windll.user32.SetForegroundWindow