Python ctypes structure being overwritten when allocating more memory

后端 未结 1 337
暖寄归人
暖寄归人 2020-12-19 16:03

In Python 3.2 I am creating a Structure object from the data returned by the ctypes.windll.kernel32.DeviceIoControl function. After this is done I can access the Structure f

相关标签:
1条回答
  • 2020-12-19 16:28

    Some observations:

    1. DevIoCtl should be called with byref(BytesReturned).
    2. ctypes.cast's first argument must be an "object that can be interpreted as a pointer". What you are casting, however, is a raw bytes object (from output[:BytesReturned.value]).
    3. At this point, what you returned from __DeviceIoControl is a new Python bytes object. The original reference to ctypes array object has gone out of scope. So, it's entirely possible that it has been garbage collected and/or reused.

    FWIW, I played around with Windows IOCTL dispatch using ctypes just for the heck of it. Also using \\.\PysicalDrive0 and IOCTL_DISK_GET_DRIVE_GEOMETRY.

    I made this gist.

    0 讨论(0)
提交回复
热议问题