Connecting Tech Pros Worldwide Help | Site Map

ctypes Basics

dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#1   Feb 24 '07
...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.LoadLibrary("LibName.dll")
To give me access to the DLL functions.

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

inx = c_double(1000.00)
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.restype = 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).



Reply