[IronPython] IronPython 2.6.1 & ctypes, C library, and debugging with VS2008

Dino Viehland dinov at microsoft.com
Sun Jul 18 23:38:22 CEST 2010

TP wrote:
> I'm using IronPython 2.6.1 for Net 2.0 and VS2008 on Windows XP SP3.
> I have a python script that lets me access the Leptonica Image
> Processing C library (http://leptonica.com/) using the ctypes module.
> Everything seems to work fine with cpython 2.6. I can correctly load
> leptonlib.dll, create leptonica PIX images, manipulate them, and even
> display them using PyQT. I can use VS2008 to put breakpoints on
> leptonica C functions and step through the C code in the VS2008
> debugger.
> I thought I'd give IronPython a try since it also has ctypes, but I
> ran into a number of problems.
> The leptonlib.dll must be loading since I am able to correctly get
> back the library's version string. That is the following works:
>     _getLeptonlibVersion = _leptonlib.getLeptonlibVersion
>     _getLeptonlibVersion.restype = ctypes.c_void_p
>     addr = _getLeptonlibVersion()
>     version = ctypes.string_at(addr)
> However the following code doesn't seem to work under IronPython 2.6.1:
>     pix = Pix(dimensions=(10,20,1))
> where essentially the following is called:
>     _pix = ctypes.c_void_p()
>     _pix.value = _leptonlib.pixCreate(width, height, depth)
>     self._pix = _pix
> and I want to treat _pix as an opaque ptr I just hand back to
> leptonica whenever it needs it. For example:
>     def height(self):
>         """Get height of pix in pixels."""
>         return _leptonlib.pixGetHeight(self._pix)
> Which works with cpython but returns something other than 20 with
> IronPython.
> By specifying "-X:Debug -X:FullFrames -X:Tracing leptonica.py" as the
> arguments to C:\Program Files\IronPython 2.6\ipy.exe I can get
> IronPython to correctly stop at places in my script where I put:
>     import pdb
>     pdb.set_trace()
> when I debug leptonlibd.dll with VS2008. However, my breakpoints on
> leptonica's C functions never seem to get hit. This works fine if I
> instead use python26.exe as command to launch when debugging.

You should only need -X:Debug to get debugging under VS.  pdb uses a 
more Pythonic form of debugging but there's no support for it in VS.

> Ideally I like to have the VS Debugger stop in leptonlibd.dll's
> pixCreate() C function just before it returns its value so I can
> compare that to what I get back on the IronPython side.
> So how's does one use VS2008 to step through C functions in DLLs that
> are loaded by IronPython and the ctypes module?

Do you have symbols (.PDB files) for leptonlibd.dll?  You'll need symbols
to be able to step through the C code.  You can still probably set a breakpoint
in the function because it's DLL exported - I think you can debug->new
breakpoint and enter leptonlibd!pixCreate.  Even w/o symbols you could 
step through the assembly.

> Secondly, am I using ctypes wrong, and does my code only work by
> happenstance for cpython.

I don't see anything particularly wrong on your side and this looks like
a pretty simple call.  I would assume the C functions are defined as:

void* pixCreate(int width, int height, int depth);
int pixGetHeight(void* pixel);

I would hope we're getting all of this right as it seems like simple stuff
that should be tested somewhere.  My first guess would be maybe we're
getting the calling convention wrong.  Maybe it needs to be a WinDLL instead
of a CDLL?  Maybe there's a check in the Python code for sys.platform which
is looking for win32 and uses WinDLL to open the DLL instead of CDLL on

More information about the Ironpython-users mailing list