[IronPython] how to convert C# MarshalAs code to IronPython
abrahams84 at gmail.com
Sun Jun 6 12:15:17 PDT 2010
Is it a good approach to use clrtype.py or is there a better way to call the
C function from IronPython...
Also, the SizeParamIndex works ok when it is set to 10 in VS2005, but it
does error out when used in VS2010, then I set it to 1.
On Sun, Jun 6, 2010 at 9:48 AM, Curt Hagenlocher <curt at hagenlocher.org>wrote:
> You'd need to decorate the string array parameter with a lot more
> information -- it's an ANSI string rather than a Unicode string, for
> instance, and there's another parameter that describes the length of the
> array. But I just looked at the code for clrtype.accepts, and it doesn't
> appear to support decorating the parameter types with additional attributes.
> If you can get at the TypeBuilder -- and I know this used to be possible --
> then the easiest way to implement this is probably just to call
> TypeBuilder.DefinePInvokeMethod directly.
> Is there a typo in the C# code? It looks like SizeParamIndex should be 1
> and not 10.
> On Sat, Jun 5, 2010 at 10:57 PM, Ashley Abraham <abrahams84 at gmail.com>wrote:
>> 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)
>> Users mailing list
>> Users at lists.ironpython.com
> Users mailing list
> Users at lists.ironpython.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Users