[IronPython] ctypes.py from FePy

Curt Hagenlocher curt at hagenlocher.org
Tue Apr 8 05:21:02 CEST 2008

On Mon, Apr 7, 2008 at 9:54 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
> I've been trying to use ctypes.py from FePy for dynamic platform invoke.
> Unfortunately it doesn't appear to work...

The main problem appears to be that MethodTarget.CanOptimize in
IronPython 1.1.1 assumes that the method will have a DeclaringType --
but a dynamically generated method isn't necessarily attached to a
type.  As such, it doesn't appear that this version of ctypes.py could
have ever worked with IronPython 1.1.1.

I haven't checked earlier versions of IronPython 1.

By changing MethodTarget.CanOptimize in the IronPython source so that
it returns false when mi.DeclaringType == null, I was able to make
ctypes.py work.

> Unfortunately 'WDLL' doesn't exist in ctypes. If I replace it with
> 'CDLL' it blows up with the following error:

Presumably, the difference between WDLL and CDLL was that the latter
uses a calling convention of CallingConvention.Cdecl while the former
would have used CallingConvention.Winapi.  This is a pretty important

In general, I think that there's some really nice code in ctypes.py,
but like all invocations of pInvoke, you definitely can't use it
blindly.  Your PostMessage would probably break on a 64-bit OS even if
it weren't for the issues of the calling convention and the problem in
MethodTarget.CanOptimize -- because in the proper definition of
PostMessage, the second and third parameters are IntPtr, but ctypes.py
would treat them as "int32"

Curt Hagenlocher
curt at hagenlocher.org

More information about the Ironpython-users mailing list