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

Ashley Abraham abrahams84 at gmail.com
Sun Jun 6 07:57:20 CEST 2010

    I have the following C# code and I am trying to do the same
functionality in IronPython, any help is appreciated.

I was trying to do the same functionality in IronPython using the clrtype.py
from the IronPython-2.6-Samples and I am able to connect to the library and
the function but I am having trouble passing array of strings. With the
current code it only passes the first letter and then it crashes.

I am trying to call  cwavec(int, int, char *) and it is a C function which
is part of the vniwave.dll and I am trying access it in IronPython.

C# code:

namespace PVWave
    public class OPI
            EntryPoint = "cwavec",
            ExactSpelling = false,
            CharSet = CharSet.Ansi,
            SetLastError = true)]
        public static extern unsafe int cwavec(Int32 action, Int32 nCmds,
            [MarshalAs(UnmanagedType.LPArray, ArraySubType =
UnmanagedType.LPStr, SizeParamIndex = 10)] string[] cmds);


Here is my attempt in IronPython:

import clrtype
import System
import sys

class OPI(object):
    "Description of Class"

    # Note that you could also the "ctypes" modules instead of pinvoke
    __metaclass__ = clrtype.ClrClass


    from System.Runtime.InteropServices import DllImportAttribute,
    DllImport = clrtype.attribute(DllImportAttribute)
    PreserveSig = clrtype.attribute(PreserveSigAttribute)

    @clrtype.accepts(System.Int32, System.Int32,
    def cwavec(action, nCommands, commands): raise RuntimeError("this should
not get called")

def call_pinvoke_method_pvwave():
    print "Calling pinvoke pvwave methods:"
    actionNum = 2
    nCommands = 1
    commands = System.Array[System.String](['PRINT, "Connected to PV-Wave
from IronPython"'])
    print "cwavec(action, nCommands, commands):"
    OPI.cwavec(actionNum, nCommands, commands)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100606/4abbbcc9/attachment.html>

More information about the Ironpython-users mailing list