Interconvert a ctypes.Structure to/from a binary string?

Discussion in 'Python' started by Andrew Lentvorski, Aug 2, 2008.

  1. Basically, I'd like to use the ctypes module as a much more descriptive
    "struct" module.

    Is there a way to take a ctypes.Structure-based class and convert it
    to/from a binary string?

    Thanks,
    -a
     
    Andrew Lentvorski, Aug 2, 2008
    #1
    1. Advertising

  2. Andrew Lentvorski

    Uwe Schmitt Guest

    On 2 Aug., 08:35, Andrew Lentvorski <> wrote:
    > Basically, I'd like to use the ctypes module as a much more descriptive
    > "struct" module.
    >
    > Is there a way to take a ctypes.Structure-based class and convert it
    > to/from a binary string?
    >
    > Thanks,
    > -a


    My first idea was :

    from ctypes import *
    from pickle import *

    class T(Structure): pass

    print dumps(T())

    which raises an TypeError, "abstract class".

    And then I found

    http://osdir.com/ml/python.ctypes/2006-03/msg00009.html

    Greetings, Uwe
     
    Uwe Schmitt, Aug 2, 2008
    #2
    1. Advertising

  3. On Aug 1, 11:35 pm, Andrew Lentvorski <> wrote:
    > Basically, I'd like to use the ctypes module as a much more descriptive
    > "struct" module.
    >
    > Is there a way to take a ctypes.Structure-based class and convert it
    > to/from a binary string?
    >
    > Thanks,
    > -a


    After chugging through the ctypes source code, I found that I can
    abuse ctypes into doing what I want.

    Here is how I did it. I can abuse
    string_at(addressof(SomeCtypesClass), length) to get a binary string
    out of ctypes while I use:

    def analyze_elf_header(binaryData):
    headerSize = ctypes.sizeof(Elf32_Ehdr)
    header = Elf32_Ehdr()

    # Abuse ctypes to initialize from a string
    bb = ctypes.create_string_buffer(binaryData[0:headerSize])
    ctypes.memmove(ctypes.addressof(header), ctypes.addressof(bb),
    headerSize)

    To jam stuff into a ctypes class. This seems like an oversight in the
    module though. It would really be better if the class itself had
    methods to init from/produce to a binary string.

    However, I would prefer that somebody who actually knows ctypes to
    weigh in here with comments about what I did.

    Thanks,
    -a
     
    Andrew P. Lentvorski, Jr., Aug 3, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    424
    Henk Punt
    Jul 23, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,295
    Neil Kurzman
    Feb 9, 2005
  3. =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B

    ctypes: how to make a structure pointer to point to a buffer

    =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B, Apr 23, 2007, in forum: Python
    Replies:
    1
    Views:
    1,118
    Diez B. Roggisch
    Apr 23, 2007
  4. Replies:
    7
    Views:
    503
  5. Replies:
    0
    Views:
    535
Loading...

Share This Page