8

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)

user1278616
  • 103
  • 2
  • 6
  • 2
    As far as I am aware, DLLs cannot "contain classes". They can contain functions, and compilers will mangle method names to function names before putting them into a DLL. So this entirely depends on the language the DLLs are implemented in. (Disclaimer: I'm not familiar with .NET and the Windows platform.) – Sven Marnach Mar 19 '12 at 13:58
  • 1
    This may not be relevant, but quite often dotnet "dlls" are not actually binary compiled dlls, they are c# bytecode with a dll extension. – aquavitae Mar 19 '12 at 13:58
  • http://stackoverflow.com/questions/2082159/how-to-export-c-sharp-methods – cons0ul Mar 19 '12 at 14:59
  • Thanks for all the replies! The link mentioned by @cons0ul will get you to this webpage [link](http://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports) where someone has written a C# template to "make it easier to export managed code to native applications without the need of COM registrations." with an example 'class Test { [DllExport("add", CallingConvention = CallingConvention.StdCall)] public static int Add(int left, int right) { return left + right; } }'. But nothing about getting this "test" class into python (or which dll its from). note: never used C# b4 – user1278616 Mar 19 '12 at 17:00
  • @user1278616: My previous comment was meant as a request for further information from you. Maybe you are looking for [the documentation on the .NET integration of IronPython](http://ironpython.net/documentation/dotnet/). (Again, just guessing.) – Sven Marnach Mar 19 '12 at 18:51
  • @SvenMarnach: I've editted the post, but maybe my problem is general enough (or I am lost enough) that there isnt any new information allowing you to help me... – user1278616 Mar 20 '12 at 17:27
  • "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." This sentence does not make sense. IronPython *is* an implementation of Python. What you said is like "I don't want to use Visual C++ because I want to use C++." Apparently, you are confusing [CPython](http://en.wikipedia.org/wiki/CPython) with [Python](http://en.wikipedia.org/wiki/Python_%28programming_language%29). If you want .NET integration, IronPython is the way to go, and I was actually assuming you are using it. – Sven Marnach Mar 20 '12 at 17:38
  • .. I was unable to import standard things like numpy, scipy, and pyvisa in ironpython, which is why I assumed it was something different from python. A quick google search shows that it _might_ be possible to get things like this in ironpython. Can I have arbitrary python packages in ironpython? – user1278616 Mar 21 '12 at 10:05

1 Answers1

1

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.

wagnerpeer
  • 937
  • 7
  • 22