[issue21903] ctypes documentation MessageBoxA example produces error
New submission from Dan O'Donovan: There is an example in the ctypes python3 documentation for producing a native Windows MessageBoxA https://docs.python.org/3.3/library/ctypes.html#ctypes.PYFUNCTYPE Try as I might, I cannot get this example to run under python 3 (it is in the python3 documentation) - it always produces an error Traceback (most recent call last): File "<stdin>", line 1, in <module> ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type I apologise for not attaching a fix, I have tried and cannot find one. ---------- assignee: docs@python components: Documentation messages: 222088 nosy: Dan.O'Donovan, docs@python priority: normal severity: normal status: open title: ctypes documentation MessageBoxA example produces error type: behavior versions: Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21903> _______________________________________
Dan O'Donovan added the comment: Additional note, this code snippet does work in python 2. There is a note that all snippets are tested with 'doctest' so I have added the 'tests' tag to this ticket. ---------- components: +Tests _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21903> _______________________________________
Dan O'Donovan added the comment: Ok, this fail is happening because we're using python3 unicode strings to call the ANSI MessageBoxA function. A possible fix; Encode strings before passing the MessageBoxA (ctypes.txt.diff attached) Alternatively, calls could be made to the unicode MessageBoxW function, but someone who knows about ctypes / Win32 magic numbers would have to look at that. (Inspiration take from this stack overflow question http://stackoverflow.com/questions/18164994/the-ctypes-wraps-messageboxa-exa...) ---------- keywords: +patch Added file: http://bugs.python.org/file35828/ctypes.txt.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21903> _______________________________________
Roundup Robot added the comment: New changeset 11c3d6a8f5fd by Berker Peksag in branch '3.5': Issue #21903: Update ctypes example to use MessageBoxW https://hg.python.org/cpython/rev/11c3d6a8f5fd New changeset 3031e4a95131 by Berker Peksag in branch '3.6': Issue #21903: Merge from 3.5 https://hg.python.org/cpython/rev/3031e4a95131 New changeset cb55aedcc7e8 by Berker Peksag in branch 'default': Issue #21903: Merge from 3.6 https://hg.python.org/cpython/rev/cb55aedcc7e8 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21903> _______________________________________
Berker Peksag added the comment: Thanks for the report! I've now updated the example to use MessageBoxw. Eryk, do you find the second example (GetWindowRect) is useful? Can we make it more usable? Unless I'm missing something, we need to pass the return value of GetActiveWindow() to make it work. ---------- nosy: +berker.peksag, eryksun resolution: -> fixed stage: -> resolved status: open -> closed versions: +Python 3.5, Python 3.6, Python 3.7 -Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21903> _______________________________________
Eryk Sun added the comment: The GetWindowRect example seems fine, for the most part. The docs don't have to show it being called. If I were to call it, I'd use GetForegroundWindow to get a window handle. GetActiveWindow returns the active window attached to the current thread's message queue, but a console application probably doesn't own a window. (The console window is owned by conhost.exe.) A more pressing matter is the GetModuleHandle examples, which need to either be fixed or replaced. GetModuleHandle returns a module's base address, and in a 64-bit process the result could be truncated when returned as the default C int type. GetModuleHandleW.restype has to be set to a pointer type. Setting it to a Python function, such as the ValidHandle example, has the same truncation problem. In general, a ValidHandle checker would have to be implemented as an errcheck function. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21903> _______________________________________
participants (4)
-
Berker Peksag
-
Dan O'Donovan
-
Eryk Sun
-
Roundup Robot