Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access a class from a dll in python?

I have a handfull of .dll files with classes designed to control an external device connected to my desktop via ethernet. I'd like to import these classes to python and use their member functions / variables etc to control the device.

I have looked at a number of options including:

-ctypes (which seemed to work well for functions but not classes). Here "DotNet\Aerotech.Ensemble.dll" is my dll library, and "Network" is a class in that library with a member function "Connect". The library loads, but I can't 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 these seem to be seperate languages altogether and I want to be able to do everything from one place (python).

-SWIG. The SWIG documentation seems to indicate that it can handle importing classes, but also seems to require C++ code and or header files (which I dont have) to be wrapped for use as a python module.

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

EDIT:

@SvenMarnach: I had a look at IronPython and was able to get it working with my dll files, but I want to do this in Python since I am already doing a lot of things in that language. I want to integrate these dll functions or classes or whatever into existing python programs.

In getting IronPython working, however, I stumbled back accross Python for .NET which claims to be able to install .Net awareness to existing python installations...This works (ie I can access the dll files and control my device) if I use it in the directory which I downloaded the python for .NET files to, but if I try it in some other directory (remembering to append the python.net dir to the sys.path), you get an error ImportError: dynamic module does not define init function (initclr)

like image 363
user1278616 Avatar asked Mar 19 '12 13:03

user1278616


People also ask

Can Python import DLL?

Calling a dll from python pyd is nothing more than a DLL which python can use directly. If you have access to the source code for the external library, you can use SWIG (Simplified Wrapper Interface Generator) to compile this to a . pyd library which can be used directly in python.

Can DLL have classes?

If you are programming in Visual Basic or C#, you must declare DLL functions within a class or Visual Basic module. Within a class, you define a static method for each DLL function you want to call.


1 Answers

As I understand you want to use .NET classes in you python code. I'm also not very familiar with C# and the way it works but if I understand it right it can be compiled into a DLL in the Common Intermediate Language. This CIL is a programming language defined by the Common Language Infrastructure which invented by microsoft and the basis for the .NET framework. To read CIL you have to use a Common Language Runtime which is basically a Just-In-Time compiler to machine readable byte code. Here I think lies the solution to your problem. To use the classes defined in the DLL which hopefully contains classes in the CIL you will need a CLR which maps CIL to Python byte code! IronPython has such a CLR mapper. However it is possible to use a CLR which works with pure Python often these mapper can be one way only (The one I worked with). If you need to use functions which expect CIL/.NET objects you will need a CLR which can convert between CIL and Python objects in both ways.

I hope this iwas helpful. If you are need further information I suggest to look at the documentation on the .NET integration for IronPython as already stated above.

like image 126
wagnerpeer Avatar answered Sep 21 '22 03:09

wagnerpeer