ctypes: reference of a struct member?

Discussion in 'Python' started by ma, May 1, 2009.

  1. ma

    ma Guest

    If I have this struct in C:

    struct spam {
    int ham;
    char foo;
    };


    if I have this declaration:
    struct spam s_;

    If I wanted to pass a reference to a function of s_'s foo character, I
    can do something like this:

    somefunc(&s_.foo)

    How do I do the same thing in ctypes?
    ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1)
    ma, May 1, 2009
    #1
    1. Advertising

  2. ma

    CTO Guest

    ctypes.byref()
    CTO, May 1, 2009
    #2
    1. Advertising

  3. ma

    ma Guest

    ctypes.byref() does not work for struct members.
    Try it out.
    class s(ctypes.Structure):
    _fields_ = [('x',ctypes.c_int)]

    a = s()
    ctypes.byref(a.x) //this won't work.



    On Fri, May 1, 2009 at 2:28 PM, CTO <> wrote:
    > ctypes.byref()
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    ma, May 1, 2009
    #3
  4. ma <> wrote:

    >If I have this struct in C:
    >
    >struct spam {
    > int ham;
    > char foo;
    >};
    >
    >
    >if I have this declaration:
    >struct spam s_;
    >
    >If I wanted to pass a reference to a function of s_'s foo character, I
    >can do something like this:
    >
    >somefunc(&s_.foo)
    >
    >How do I do the same thing in ctypes?
    >ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1)


    See ctypes.create_string_buffer().
    To unpack the struct elements received from the C function, use
    struct.unpack().

    Here some example:

    def rssiam_volt_Q(self):
    '''Output Voltage Query
    '''
    query_buffer = ctypes.create_string_buffer(8)
    self.rssiam_32_dll.rssiam_volt_Q.argtypes = [ctypes.c_long,
    ctypes.c_char_p]
    result = self.rssiam_32_dll.rssiam_volt_Q(self.session,
    query_buffer)
    if result != VI_SUCCESS:
    raise IOError, 'rssiam_volt_Q: result = %d' % result
    return(struct.unpack('d', query_buffer.raw)[0])


    In this case, it is a 64 bits float (= double) value that is returned by
    reference (query_buffer).
    Attention, struct.unpack() returns a tuple, even when there is only a
    single element to extract from the buffer.




    Regards,

    Günther
    Günther Dietrich, May 1, 2009
    #4
    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. Angus Comber
    Replies:
    7
    Views:
    1,141
    Richard Heathfield
    Feb 5, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,343
    Chris Fogelklou
    Apr 20, 2004
  3. Henk Punt
    Replies:
    0
    Views:
    389
    Henk Punt
    Jul 23, 2004
  4. dutchgoldtony
    Replies:
    15
    Views:
    461
    Mike Wahler
    Nov 16, 2005
  5. Replies:
    0
    Views:
    491
Loading...

Share This Page