From the ctypes documentation:
By default functions are assumed to return the C int type. Other return types can be specified by setting the restype attribute of the function object.
More generally assuming ctypes understands anything about the function being called without being told seems rather dangerous, I don't believe dlls contain any sort of typing information so Python just assumes types kinda match e.g. when you pass in -1
, Python probably pushes a 32b -1 to the stack, the C side reads that as a short which also happens to be -1 (because the machine uses two's complement so taking the first two bytes of 0xFFFFFFFF
gives 0xFFFF
which is also -1 when interpreted as a signed short, if you add a second short
parameter you may find it to be -1, or both to be something completely different and possibly garbage as CDECL pushes parameters from last to first).
Then the C side puts 0xFFFF
in EAX for the return value, ctypes reads that and with no instruction interprets it as the 32b signed integer 0x0000FFFF
.