Am I using ctypes correctly? Where are fprints going?
wingusr at gmail.com
Tue Feb 16 04:42:15 CET 2010
>From the Cplusplus-sig, I gathered that instead of Boost.Python I
should use ctypes for the following problem. But let me make sure I'm
using it correctly.
I am calling the C Leptonica Image Processing C Library
(http://leptonica.com) from python.
Within its leptprotos.h file are functions like this:
extern PIX * pixRead ( const char *filename );
extern void pixDestroy ( PIX **ppix );
Using Windows XP, and within a Command Prompt I do this:
>>> import ctypes
>>> leptonica = ctypes.cdll.leptonlib
>>> pixRead = leptonica.pixRead
>>> pix.value = pixRead(r"test.tif")
1553 #this is the correct width
c_void_p(None) #This look right, pixDestroy() sets the pointer to NULL.
Is this the proper way you are supposed to supply the address of a
pointer that is returned by a C function? Is there a better way?
Should I have done the following also?
>>> pixRead.restype = ctypes.c_void_p
>>> pixDestroy.restype = None
Now that the pix has been destroyed, you shouldn't be able to get its width:
This is correct but there also should have been an error message printed out.
The C library is doing this (I think, I haven't actually used the
debugger on it):
sprintf(charbuf, "Error in %s: %s\n", procname, msg);
fprintf(stderr, charbuf, ival);
But I don't see any error message appear in my console window?
Finally, the documentation states for ctypes.util.find_msvcrt():
Windows only: return the filename of the VC runtype library used by
Python, and by the extension modules. If the name of the library
cannot be determined, None is returned.
If you need to free memory, for example, allocated by an extension
module with a call to the free(void *), it is important that you use
the function in the same library that allocated the memory.
>>> import ctypes.util
Does this mean that if I build a Debug version of the Leptonica DLL
(which will use the Debug version of the runtime library,
msvcr90d.dll), that I also have to build and use a Debug version of
I tried using leptonlibd.dll with the ActiveState release version of
Python26 and didn't see any immediate error.
More information about the Python-list