Title: ctypes documentation MessageBoxA example produces error
Type: behavior Stage: resolved
Components: Documentation, Tests Versions: Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Dan.O'Donovan, berker.peksag, docs@python, eryksun, python-dev
Priority: normal Keywords: patch

Created on 2014-07-02 09:14 by Dan.O'Donovan, last changed 2016-09-28 21:33 by eryksun. This issue is now closed.

File name Uploaded Description Edit
ctypes.txt.diff Dan.O'Donovan, 2014-07-02 10:50
Messages (6)
msg222088 - (view) Author: Dan O'Donovan (Dan.O'Donovan) * Date: 2014-07-02 09:14
There is an example in the ctypes python3 documentation for producing a native Windows MessageBoxA

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.
msg222090 - (view) Author: Dan O'Donovan (Dan.O'Donovan) * Date: 2014-07-02 09:18
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.
msg222093 - (view) Author: Dan O'Donovan (Dan.O'Donovan) * Date: 2014-07-02 10:50
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
msg277621 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-09-28 14:06
New changeset 11c3d6a8f5fd by Berker Peksag in branch '3.5':
Issue #21903: Update ctypes example to use MessageBoxW

New changeset 3031e4a95131 by Berker Peksag in branch '3.6':
Issue #21903: Merge from 3.5

New changeset cb55aedcc7e8 by Berker Peksag in branch 'default':
Issue #21903: Merge from 3.6
msg277624 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-09-28 14:13
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.
msg277664 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-09-28 21:33
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.
Date User Action Args
2016-09-28 21:33:54eryksunsetmessages: + msg277664
2016-09-28 14:13:22berker.peksagsetstatus: open -> closed

versions: + Python 3.5, Python 3.6, Python 3.7, - Python 3.3, Python 3.4
nosy: + berker.peksag, eryksun

messages: + msg277624
resolution: fixed
stage: resolved
2016-09-28 14:06:25python-devsetnosy: + python-dev
messages: + msg277621
2014-07-02 10:50:32Dan.O'Donovansetfiles: + ctypes.txt.diff
keywords: + patch
messages: + msg222093
2014-07-02 09:18:39Dan.O'Donovansetmessages: + msg222090
components: + Tests
2014-07-02 09:14:49Dan.O'Donovancreate