6

I am almost at the end of my wits here and am hoping you all could help me figure this one out. I am running Anaconda Python 3.5 64 bit and compiled Python.NET anaconda package to add .NET capabilities to Python. I imported a DLL sent to me by someone and here is how my code looks like:

from __future__ import (
    unicode_literals,
    print_function,
    division,
    absolute_import
)

import clr

from System import String, Char, Int32

clr.setPreload(True)

clr.AddReference('System.Windows.Forms')

clr.AddReference(
        "C:\\Program Files\\XYZ\\TTE.dll"
)

import TTE

from System.Windows.Forms import Form, Application, Button
import System

tt = TTE.TT()

form = Form()
# declaring string (not python native string) to get System.String
cdbp = String('C:\\')
sdbp = String('C:\\')
mdbp = String('C:\\')

tt.Initialize(cdbp, sdbp, mdbp, form)

'''
tt.Initialize.Overloads[
    System.String, System.String, System.String, System.Windows.Forms.Form](
        cdbp, sdbp, mdbp, form
)
'''

When I run the Initialize function, I get the following exception:

Traceback (most recent call last):
  File "C:/Users/as/All/Code/try1.py", line 257, in <module>
    tt.Initialize(cdbp, sdbp, mdbp, form)
System.ArgumentException: Object of type 'System.Int32' cannot be converted to type 'TTE.TT+ResultEnum&'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)

I am fairly new to .NET and have tried googling and looking at all the stack overflow posts related to Python.NET but I dis not find the information I am looking for. Another curious thing is that the same DLL works when used in VB and fails when used in python. Why would that be?

I thought the problem was with Python.NET thinking that I am calling a different (overloaded) function named Initialize so I tried the Overloads call (in comment) and it gave me the following exception:

Traceback (most recent call last):
  File "C:/Users/as/All/Code/try1.py", line 261, in <module>
System.Windows.Forms.Form](cdbp, sdbp,
TypeError: No match found for signature

Then as per recommendation, I printed the Overload and here is the output

Int32 Initialize(System.String ByRef, System.String ByRef, System.String ByRef, System.Windows.Forms.Form, ResultEnum ByRef, ResultEnum ByRef, ResultEnum ByRef)
Int32 Initialize(System.Collections.Specialized.StringCollection ByRef, System.String ByRef, System.String ByRef, System.Windows.Forms.Form, ResultEnum ByRef, ResultEnum ByRef, ResultEnum ByRef)
Int32 Initialize(System.Collections.Generic.List`1[System.String] ByRef, System.String ByRef, System.String ByRef, System.Windows.Forms.Form, ResultEnum ByRef, ResultEnum ByRef, ResultEnum ByRef)

I can clearly see why Overload is complaining, but the VB function Initialize I am targeting has declared the last three ResultEnum parameters as optional, but Python clearly requires it.

Is there a particular direction I should be looking in?

T54
  • 81
  • 1
  • 16
A Saxena
  • 165
  • 2
  • 13
  • What does `tt.Initialize.Overloads` show? – denfromufa Jun 22 '17 at 03:32
  • The problem is optional byref arguments - not supported in c#, hence not in pythonnet: https://stackoverflow.com/questions/2870544/c-sharp-4-0-optional-out-ref-arguments – denfromufa Jun 22 '17 at 14:23
  • the only workaround is to use reflection like this: https://stackoverflow.com/a/9916197/2230844 – denfromufa Jun 22 '17 at 14:24
  • @denfromufa I tried a few things like tt.Initialize(calDBPath, seqDBPath, measDBPath, form, Type.Missing, Type.Missing, Type.Missing), but this is not working and I can see why. Can you post a small piece of code illustrating what you think will work. – A Saxena Jun 22 '17 at 17:34
  • you need to provide minimal vb.net code to call this from pythonnet – denfromufa Jun 22 '17 at 17:48
  • Oh I see, you are saying that the edit needs to happen on vb side. Sadly I do not have access to source code of DLL, we just got the DLL. I guess this rules out pythonnet for now. Now I will give it a shot with ironPython. – A Saxena Jun 22 '17 at 17:55
  • Ok, let us know if it works with IronPython. But with Reflection this should work in pythonnet – denfromufa Jun 23 '17 at 00:46

0 Answers0