This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Kelvin You, amaury.forgeotdarc, belopolsky, eryksun, josh.r, meador.inge, paul.moore, steve.dower, tim.golden, zach.ware
Date 2016-10-20.16:48:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Kelvin is calling WlanScan [1], which returns an error code that apparently can include HRESULT (signed) values cast as DWORD (unsigned) values, including 0x80342002 (ERROR_NDIS_DOT11_POWER_STATE_INVALID). 

ctypes.FormatError calls FormatMessage [2] with the flag FORMAT_MESSAGE_FROM_SYSTEM. About half of the system error codes defined in winerror.h consist of COM HRESULT codes. But, to clarify Kelvin's link [3], this does not include NTSTATUS values from kernel-mode system calls. NTSTATUS values require the ntdll.dll message table.


A workaround in this case is to use the default c_int restype to return the error as a signed integer:

    >>> ctypes.FormatError(0x80342002 - 2**32)
    "The wireless local area network interface is powered down and doesn't
    support the requested operation."

A similar workaround works for setting the exit code to an HRESULT or NTSTATUS error code:

    >>> hex('python -c "import os; os._exit(0xC0000001)"'))
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    OverflowError: Python int too large to convert to C long
    >>> hex('python -c "import sys; sys.exit(0xC0000001)"'))

    >>> hex('python -c "import os; os._exit(0xC0000001 - 2**32)"'))
    >>> hex('python -c "import sys; sys.exit(0xC0000001 - 2**32)"'))
Date User Action Args
2016-10-20 16:48:50eryksunsetrecipients: + eryksun, paul.moore, amaury.forgeotdarc, belopolsky, tim.golden, meador.inge, zach.ware, steve.dower, josh.r, Kelvin You
2016-10-20 16:48:50eryksunsetmessageid: <>
2016-10-20 16:48:50eryksunlinkissue28474 messages
2016-10-20 16:48:50eryksuncreate