2
votes

I want to know if it is possible to handle Delphi Packed records with ctypes. I have a dll written in Delphi that is exposing some methods with stdcall convention.

On of the methods is returning a custom record packed like this:

TMapCell = packed record
    Tile : Word;
    Z : Shortint;
end;

When records aren't packed I could handle them in a way like this:

class TMapCell(Structure):
    _fields_ = [
        ('Tile', c_ushort),
        ('Z', c_byte),
    ]

but when I am trying to use it I have an access violation erron

WindowsError: exception: access violation writing 0x0000112D
1
By the way, the documentation for ctypes is, just like the rest of the standard Python libraries, excellent. You can find out almost everything you ever need to know about ctypes by starting here: docs.python.org/2.7/library/ctypes.html#module-ctypesDavid Heffernan

1 Answers

3
votes

Specify the _pack_ attribute to control packing:

class TMapCell(Structure):

    _pack_ = 1

    _fields_ = [
        ('Tile', c_ushort),
        ('Z', c_byte),
    ]

Of course, it would be so much better if you refrained from packing your records in the first place. Always prefer aligned records.