0

I'm trying to access a class inside a .dll file using pythonnet and I am unable to create and instance of a class without the following error. I'm using the latest version of pythonnet (2.5.2) and Python 3.10.5.

Error

Traceback (most recent call last):
    x = IDispenser()
TypeError: interface takes exactly one argument

Python Code

import clr
import sys

dll_path = "C:\\Program Files\\Seyonic\\Dispenser Control Monitor 3.8"

if dll_path not in sys.path:
    sys.path.append("C:\\Program Files\\Seyonic\\Dispenser Control Monitor 3.8")

assert dll_path in sys.path

clr.AddReference("Seyonic.Dispenser")
from Seyonic.Dispenser import IDispenser

x = IDispenser()

DLL

namespace Seyonic.Dispenser
{
public interface IDispenser
{
// ----------------------------------------------------------------------------
// Properties
// ----------------------------------------------------------------------------
int Data { get;}
...etc
}
}
ssmith
  • 1
  • 1
  • The key here is what the DLL interface is; yet you shared a severely truncated version of it with essentially none of the info we'd need. We would need to see what arguments the constructor takes. You omitted that info though. – Random Davis Aug 24 '22 at 15:56
  • @RandomDavis I don't have access to that information. The data sheet I'm using for this library only shows the properties and functions contained within the interface, not the constructor. Unless there is a way to view the dll directly, which I'm not sure of. – ssmith Aug 24 '22 at 16:03
  • Yeah there's way to look into DLLs https://stackoverflow.com/questions/4438900/how-to-view-dll-functions – Random Davis Aug 24 '22 at 16:13

1 Answers1

0

After digging through the dll file (doesn't seem to take any arguments for using these classes) I think this is a pythonnet / python3 problem. Here is an excerpt from https://github.com/DynamoDS/Dynamo/wiki/Work-in-progress-to-improve-Python-3-support:

Python classes cannot implement .NET interfaces (Not planned) This is probably an advanced usage scenario of Python and interoperability with .NET. Here a Python class is defined in such a way that it implements a .NET interface. The idea behind this is that instances of the class can be created and passed as arguments to methods accepting them. Here is an example of how this could look like:

import clr
clr.AddReference('SomeLibrary')
from SomeLibrary import SomeInterface, SomeClass

class MyClass(SomeInterface):
  def __init__(self):
    pass

inst = MyClass()
result = SomeClass.SomeMethodTakingSomeInterface(inst)
OUT = result

Given a valid library was provided, the previous code sample would work without issues in the IronPython2 engine. When using CPython3, however, you can get TypeError : interface takes exactly one argument or TypeError : object does not implement SomeInterface, depending on the interface and method definitions. The required changes in Python .NET to make this work seem big when compared to the relevance we have detected for this use case, so we have decided to not plan this work yet.

ssmith
  • 1
  • 1