ctypes Basics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    ctypes Basics

    ...Let me post a couple of lines that I used this week while working with an external DLL that needed C pointers. I only barely understand it at this point because it is new to me, from a friend, and I haven't worked in C in 10 years, but it worked and may lead you in the right direction.

    The DLL expected C pointers for input and output to it's functions.
    from ctypes import *
    Because he's using ctypes.

    ExternalLib = windll.LoadLibr ary("LibName.dl l")
    To give me access to the DLL functions.

    tmpStr = create_string_b uffer(256)
    I'm assuming this is a ctypes function that creates a C string pointer.

    inx = c_double(1000.0 0)
    This DLL works with coordinates, I'm going to pass a double pointer into one of the functions so must declare it as such.

    ExternalLib.SetXIn(inx)
    SetXIn() is one of the libraries functions so I'm passing in the c double pointer which I set above.

    ExternalLib.GetXOut.restyp e = c_double
    As I understand this (which I really don't) The GetXOut will be returning a pointer to a C double so I have to declare it as such.

    outx = ExternalLib.GetXOut()
    Then make the call which now give me a regular double stored in python.

    I also read a bit of the python Ctypes Tutorial, though only enough to get a basic understanding for now (which is probably obvious).
Working...