How to access a class from a dll in python?

I have several .dll files with classes designed to control an external device connected to my desktop via ethernet. I would like to import these classes in python and use their member functions / variables, etc. To control the device.

I looked through several options, including:

-ctypes (which seemed to work well for functions, but not for classes). Here, “DotNet \ Aerotech.Ensemble.dll” is my dll library, and “Network” is a class in this library with a “Connect” member function. The library is loading, but I cannot access the class ...

>>> from ctypes import * >>> lib = cdll.LoadLibrary('DotNet\Aerotech.Ensemble.dll') >>> lib.Network.Connect() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__ func = self.__getitem__(name) File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'Network' not found 

-IronPython / Python for .Net, but they seem to be separate languages ​​in general, and I want to be able to do everything from one place (python).

-SWIG. The SWIG documentation seems to indicate that it can handle import classes, but it also seems to require that the C ++ code files and header files (which I didn't have) are wrapped for use as a python module.

I'm a beginner and pretty lost here, so any help is appreciated!

EDIT:

@SvenMarnach: I looked at IronPython and was able to get it to work with my DLL files, but I want to do it in Python, since I already do a lot of things in this language. I want to integrate these DLL functions or classes or something into existing python programs.

However, when working with IronPython, I came across Python for .NET , which claims to be able to set a .Net alert on an existing installation python ... This works (i.e. I can access the DLL files and manage my device), if I use it in the directory where I downloaded python for .NET files, but if I try it in some other directory (remembering to add python.net dir to sys.path), you get an error ImportError: dynamic module does not define init function (initclr)
+6
source share
1 answer

As I understand it, you want to use .NET classes in your python code. I am also not very familiar with C # and how it works, but if I understand correctly, it can be compiled into a DLL in the Common Intermediate Language. This CIL is a programming language defined by the Common Language Infrastructure developed by Microsoft and the foundation for the .NET platform. To read CIL, you must use the Common Language Runtime, which is basically a Just-In-Time compiler for machine readable byte code. Here I think this is the solution to your problem. To use the classes defined in the DLL, which we hope to contain classes in CIL, you will need a CLR that maps CIL to Python byte code! IronPython has such a CLR translator. However, you can use the CLR, which works with pure Python, often this mapping can only be one way (with which I worked). If you need to use functions that expect CIL / .NET objects, you will need a CLR that can convert between CIL and Python objects in both directions.

I hope that would be helpful. If you need more information, I suggest looking at the .NET integration documentation for IronPython , as mentioned above.

+1
source

Source: https://habr.com/ru/post/911102/


All Articles