Connecting Tech Pros Worldwide Help | Site Map

ctypes Basics

  #1  
Old February 24th, 2007, 12:59 PM
dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
...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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python and opencv jlark answers 3 March 18th, 2007 06:50 AM
Calling a C program from a Python Script Brad Tilley answers 22 July 18th, 2005 07:40 PM
Book "Programming on Win32" still useful? Piet answers 2 July 18th, 2005 11:27 AM