Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a dll in python on Ubuntu

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?

like image 398
FallenAngel Avatar asked Sep 03 '25 06:09

FallenAngel


2 Answers

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.

like image 107
FuzzkingCool Avatar answered Sep 04 '25 18:09

FuzzkingCool


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

like image 33
kolypto Avatar answered Sep 04 '25 18:09

kolypto