[IronPython] how to convert C# MarshalAs code to IronPython

Jeff Hardy jdhardy at gmail.com
Mon Jun 7 17:02:09 CEST 2010


On Sun, Jun 6, 2010 at 8:20 PM, Ashley Abraham <abrahams84 at gmail.com> wrote:
> Jeff,
>     I think I miss read your reply, what you are saying is that if it based
> on ctypes it will work in IronPython as well is it?? I thought you needed
> IronClad and so on make it work.

Yeah, with ctypes IronPython can call native code directly - it
actually uses P/Invoke behind the scenes to do it. Python also
implements ctypes, as do Jython and PyPy, so any code that you write
using ctypes should work on any Python implementation.

I haven't tested this (seeing as I don't have the exact DLL) but it
should be pretty close:

from ctypes import *
vniwave = CDLL('vniwave.dll')

cwavec = vniwave.cwavec
cwavec.restype = c_int
cwavec.argtypes = [c_int, c_int, POINTER(c_char_p)]

The only part I'm not sure about is the string[] - I'm assuming the C
function takes a char**.

Alternatively, you could write the P/Invoke declarations in a C# class
library and load that into IronPython using clr.AddReference, but I'd
recommend using ctypes.

- Jeff



More information about the Ironpython-users mailing list