[IronPython] how to convert C# MarshalAs code to IronPython
Curt Hagenlocher
curt at hagenlocher.org
Sun Jun 6 16:48:43 CEST 2010
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:
> Hi,
> 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
> {
>
> [DllImport("C:\\datk_code\\VNI\\wave\\bin\\bin.i386nt\\vniwave.dll",
> 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
> declarations
> __metaclass__ = clrtype.ClrClass
>
> sys.path.append("C:\\datk_code\\VNI\\wave\\bin\\bin.i386nt")
>
> from System.Runtime.InteropServices import DllImportAttribute,
> PreserveSigAttribute
> DllImport = clrtype.attribute(DllImportAttribute)
> PreserveSig = clrtype.attribute(PreserveSigAttribute)
>
>
> @staticmethod
> @DllImport("vniwave.dll")
> @PreserveSig()
> @clrtype.accepts(System.Int32, System.Int32,
> System.Array[System.String])
> @clrtype.returns()
> 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)
>
> call_pinvoke_method_pvwave()
>
> Thanks
> Ash
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100606/8ae29902/attachment.html>
More information about the Ironpython-users
mailing list