Is there a way to use .NET DLL from Python

H

Huayang Xia

Hello All,

I have several .NET DLL (I have no source code for them), is there
anyway to use them from python instead of from C#.

Thanks,
Huayang
 
S

Shane Geiger

The following links *may* put you on the right path:


Calling DLL functions from Python (
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146847 ), a
fairly complete description with some helper class code. Another example
( http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181063 ) of
using the calldll module to do this.

calldll module: http://www.nightmare.com/software.html


http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146847






Huayang said:
Hello All,

I have several .NET DLL (I have no source code for them), is there
anyway to use them from python instead of from C#.

Thanks,
Huayang


--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
L

Luis M. González

Hello All,

I have several .NET DLL (I have no source code for them), is there
anyway to use them from python instead of from C#.

Thanks,
Huayang

I used to put my .dll files into the .DLL folder, so I could simply
import them as I would with any other python module.
But since I started using Ironpython 2.0 Alpha* this doesn't work
anymore...
Any hint?

Luis
 
H

Huayang Xia

I used to put my .dll files into the .DLL folder, so I could simply
import them as I would with any other python module.
But since I started using Ironpython 2.0 Alpha* this doesn't work
anymore...
Any hint?

Luis

Is there anyway to import class (to generate objects) from .NET DLL?
 
G

Gabriel Genellina

Huayang said:
Calling DLL functions from Python
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146847 ), a
fairly complete description with some helper class code. Another example
( http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181063 ) of
using the calldll module to do this.

Both recipes are rather obsolete now, since version 2.5 ctypes is included
in the standard library. Anyway they don't help the OP to load a .NET dll;
there is PythonDotNET http://pythonnet.sf.net/ but I've never used it
myself.
 
C

Christian Heimes

Huayang said:
Is there anyway to import class (to generate objects) from .NET DLL?

You can use PythonDotNET if you want to access .NET assemblies in
CPython (the standard Python implementation written in C).

Christian
 
F

Fuzzyman

Hello All,

I have several .NET DLL (I have no source code for them), is there
anyway to use them from python instead of from C#.

Thanks,
Huayang

To access .NET types you either need to use IronPython or
Python.NET. .NET assemblies are dependent on the .NET runtime to do
anything and so can't be accessed with ctypes as other DLLs can.

Michael Foord
http://www.manning.com/foord
 
F

Fuzzyman

I used to put my .dll files into the .DLL folder, so I could simply
import them as I would with any other python module.
But since I started using Ironpython 2.0 Alpha* this doesn't work
anymore...
Any hint?


The rule is probably still that the DLLs must be in a directory on
sys.path for the interpreter to find them. Try adding the directory
containing the assemblies to sys.path and see if you can add
references to them.

Michael Foord
http://www.manning.com/foord
 
L

Luis M. González

The rule is probably still that the DLLs must be in a directory on
sys.path for the interpreter to find them. Try adding the directory
containing the assemblies to sys.path and see if you can add
references to them.

Michael Foordhttp://www.manning.com/foord

I tried adding the directory to sys.path.
Still not working...
Traceback (most recent call last):
File , line unknown, in ##235
File , line unknown, in _stub_##2
ImportError: No module named ClassLibrary1
 
F

Fuzzyman

Luis said:
I tried adding the directory to sys.path.
Still not working...

Traceback (most recent call last):
File , line unknown, in ##235
File , line unknown, in _stub_##2
ImportError: No module named ClassLibrary1

You need to add references to assemblies before you can import from
the namespaces they contain.

import clr
clr.AddReference('ClassLibrary1')

HTH

Fuzzyman
http://www.manning.com/foord
 
L

Luis M. González

You need to add references to assemblies before you can import from
the namespaces they contain.

import clr
clr.AddReference('ClassLibrary1')

HTH

Fuzzymanhttp://www.manning.com/foord

Oh, I know what you mean.
But that was exactly the reason for having a .DLLs folder, isn't it?
When you place an assembly into this folder, you avoid having to write
this boilerplate code, and simply import the assembly as you would
with a normal python module. At least, that´s how it worked in
previous versions...
 
C

Christian Heimes

Luis said:
Oh, I know what you mean.
But that was exactly the reason for having a .DLLs folder, isn't it?
When you place an assembly into this folder, you avoid having to write
this boilerplate code, and simply import the assembly as you would
with a normal python module. At least, that´s how it worked in
previous versions...

In IronPython and with PythonDotNET you can import namespaces. Assembly
names and name spaces don't have to be related. E.g. ClassLibrary1.dll
may provide the namespace ClassLibA and ClassLibB.
clr.AddReference('ClassLibrary1') loads the assembly 'ClassLibrary1' and
makes all namespaces available to Python.

Christian
 
H

Huayang Xia

What's the difference between .NET DLL and normal C DLL? Do you mean
after clr.AddReference('ClassLibrary1'), there is no need to import
ClassLibrary1?
 
C

Christian Heimes

Huayang said:
What's the difference between .NET DLL and normal C DLL? Do you mean
after clr.AddReference('ClassLibrary1'), there is no need to import
ClassLibrary1?

A normal DLL and an assembly DLL share only the header. The rest is
totally different. You can see the DLL as a container for the CIL code.

clr.AddReference('ClassLibrary1') makes the namespaces of the
ClassLibrary1 assembly available to IronPython and PythonDotNET. import
ClassLibrary1 imports the name space. You must import the assembly
before you can use its name spaces.

Christian
 
F

Fuzzyman

Oh, I know what you mean.
But that was exactly the reason for having a .DLLs folder, isn't it?
When you place an assembly into this folder, you avoid having to write
this boilerplate code, and simply import the assembly as you would
with a normal python module. At least, that´s how it worked in
previous versions...

No. You have always had to add references to assemblies before being
able to use the namespaces they contain. You even have to do this with
C# in Visual Studio.

Michael
http://www.manning.com/foord
 
H

Huayang Xia

No. You have always had to add references to assemblies before being
able to use the namespaces they contain. You even have to do this with
C# in Visual Studio.

Michaelhttp://www.manning.com/foord

Is there any special .NET DLL format for IronPython to import (e.g.
rename the DLL to .pyd). Why when I try to import the DLL, it always
complain the module doesn't exist?
 
F

Fuzzyman

Is there any special .NET DLL format for IronPython to import (e.g.
rename the DLL to .pyd). Why when I try to import the DLL, it always
complain the module doesn't exist?

First you need to make sure the DLL is on the path - and *then* you
need to add a reference to the assembly (dll) as I showed earlier.

Michael
 
D

Dino Viehland

No. You have always had to add references to assemblies before being
able to use the namespaces they contain. You even have to do this with
C# in Visual Studio.

This *should* work in both IronPython 1.x and IronPyton 2.0 - the catch though is that it's implemented in the default site.py we ship with. So if you do the usual thing and use CPython's Lib directory you'll lose this feature w/o copying it over.
 
L

Luis M. González

This *should* work in both IronPython 1.x and IronPyton 2.0 - the catch though is that it's implemented in the default site.py we ship with. So if you do the usual thing and use CPython's Lib directory you'll lose this feature w/o copying it over.

Sorry Dino, I don't understand...
What does Cpython's Lib have to do with it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top