3

I am using python 2.6.5 on an Ubuntu intalled server.

I need to integrate an API for our applicaion, in that case, i needed to use a DLL given to me by the API provider. Their example of code about api integration is written in Visual Basic... I made a search on google and found some examples of using ctypes , and i try using cdll and pydll, which caused the following error...

OSError: /home//some.dll: invalid ELF header

One possibility is using IronPython, but i do not have much information about ironpython so i am not sure if it will handle my needs completely..

Is there any available module that let me use that dll on python (or aynthing that i am missing from the exixting ones). It is hard to upgrade my python version?

Mp0int
  • 18,172
  • 15
  • 83
  • 114
  • 1
    I may be missing something, but it's my impression that DLL's are solely microsoft entities, and you can't use them on Linux (except through Wine) – Colin Fine Oct 24 '11 at 13:21
  • Not quite. If a DLL is a .NET assembly file, you can use it with Python.NET :) – kolypto Dec 21 '20 at 17:18

3 Answers3

7

DLLs may be windows creatures, but if a DLL is 'pure .NET' and doesn't utilize executables specific to windows etc., then it can work often in Linux, through Mono. (mono ipy.exe).

Ironpython's System and similiar windows modules are customized to be os agnostic (to a untested degree).

I have successfully run NHibernate, FluentNHibernate, log4net, and a few other commonly used DLLS in Ubuntu.

import clr
import sys
sys.path.append(os.path.abspath('./DLL')) #where your dlls are
clr.AddReference('System')
clr.AddReference('FluentNHibernate')
from FluentNHibernate.Cfg.Db import PostgreSQLConfiguration

The key seems to be to import DLLs in this fashion. If a dll imports another (fluentnhibernate imports nhibernate), you don't need to import Nhibernate for example.

FuzzkingCool
  • 309
  • 4
  • 14
3

First, check if your DLL is a .NET Assembly file. An "Assembly DLL file" has nothing to do with the assembler. It's simply a way the .NET framework stores its bytecode inside a DLL file!

Do file library.dll in Linux. If it says something like this:

PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows

then you're lucky: it's an assembly file. You can run it on Linux.

Install Mono. Install Python.NET. Forget IronPython: it's dead.

Now, in Python.NET, you can do this:

import clr
clr.AddReference('./library.dll')

# the library has just registered a namespace we can use

from LibraryName import *

but how do you know what to import? Auto-complete. Or use monop tool to inspect the DLL like this:

$ monop -r library.dll

Assembly Information:
LibraryName
Version=9.9.3.0
Culture=neutral
PublicKeyToken=null

LibraryName.ClassName
...

$ monop -r library.dll LibraryName.ClassName
public class ClassName {

        public ClassName (string inputString);

        ...
}

and it will tell you everything about that library

kolypto
  • 31,774
  • 17
  • 105
  • 99
3

DLLs are Windows creatures. The only way you'll be able to use a DLL is by using a Windows build of Python. You'll be able to run Windows Python on Ubuntu by having Windows installed inside a virtual machine. You also might be able to run it using Wine.

An alternative, of course, is to ask your API provider if they have a Linux version of the API.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Solution we decided to use is installing Windows on Virtual Machine and prepare a web service that will use the DLl and communicate with Python – Mp0int Oct 25 '11 at 07:27
  • 1
    good day. Is there any guide how to execute jar which uses .dll with help of Wine in Ubuntu? Thank you – Andrey Kolesnikov Feb 01 '16 at 07:46