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 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 "", line 1, in 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 http://stackoverflow.com/questions/18164994/the-ctypes-wraps-messageboxa-example-didnt-work-in-python33) |
|
|
msg277621 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2016-09-28 14:06 |
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 |
|
|
msg277624 - (view) |
Author: Berker Peksag (berker.peksag) *  |
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) *  |
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. |
|
|