Re: Saving a Device context (DC) into a picture file

eryk sun <[email protected]> Sat, 22 Apr 2017 21:27:39 +0000
Newsgroups gmane.comp.python.ctypes
Message-ID <CACL+1atjsYgN5-C=+SCVs2YmmC+8M38Qa8Ck06k38apDu30ngA@mail.gmail.com>
On Sat, Apr 22, 2017 at 7:15 PM, Michael C
<[email protected]> wrote:
> Never mind, I found this
> https://books.google.ca/books?id=9MS9BQAAQBAJ

Thankfully a lot of the Windows code in Black Hat Python uses PyWin32.
The few ctypes examples aren't top notch. For example, the author
calls

     user32.GetWindowThreadProcessId(hwnd, byref(pid))

without setting the prototype of GetWindowThreadProcessId. In 64-bit
Python 2, the value of the hwnd argument may get corrupted if the
prototype isn't defined. It's only by luck of the draw that it usually
works.

Some WinAPI pseudohandles are non-zero in the upper DWORD, such as
HWND_TOPMOST, which is defined as (HWND)(-1), i.e. 0xFFFFFFFFFFFFFFFF
on a 64-bit system. This has the same problem in Python 2 if you
forget to set the prototype or manually wrap the value in a pointer
type. Python 3 ctypes doesn't have this problem, since it sign extends
a negative argument as an intptr_t.

Here's another problem in the book's example code. The default restype
is a signed c_int, and here the author is calling GetTickCount without
first setting GetTickCount.restype = ctypes.c_uint:

    run_time = kernel32.GetTickCount()
    elapsed = run_time - struct_lastinputinfo.dwTime

After 24.86 days system uptime, the tick count reaches 0x80000000
(milliseconds), and the signed return value wraps around to
-2147483648, incrementing toward 0. The value of `elapsed` will be
nonsense.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot