Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to use .NET DLL from Python

2,887 views
Skip to first unread message

Huayang Xia

unread,
Feb 6, 2008, 4:27:26 PM2/6/08
to
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

unread,
Feb 6, 2008, 4:37:02 PM2/6/08
to Huayang Xia, pytho...@python.org
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


--
Shane Geiger
IT Director
National Council on Economic Education
sge...@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

Luis M. González

unread,
Feb 6, 2008, 4:59:25 PM2/6/08
to

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

Huayang Xia

unread,
Feb 6, 2008, 5:25:53 PM2/6/08
to

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

Huayang Xia

unread,
Feb 6, 2008, 5:33:54 PM2/6/08
to
Or maybe we can do it in IronPython?

Gabriel Genellina

unread,
Feb 6, 2008, 5:38:43 PM2/6/08
to pytho...@python.org
Huayang Xia wrote:
>> I have several .NET DLL (I have no source code for them), is there
>> anyway to use them from python instead of from C#.

En Wed, 06 Feb 2008 19:37:02 -0200, Shane Geiger <sge...@ncee.net>
escribió:

> 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.

--
Gabriel Genellina

Christian Heimes

unread,
Feb 6, 2008, 5:55:12 PM2/6/08
to pytho...@python.org
Huayang Xia wrote:
> 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

Fuzzyman

unread,
Feb 6, 2008, 7:16:14 PM2/6/08
to

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

Fuzzyman

unread,
Feb 6, 2008, 7:17:15 PM2/6/08
to


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

>
> Luis

Luis M. González

unread,
Feb 6, 2008, 9:26:15 PM2/6/08
to

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

>>> import sys
>>> sys.path.append('C:\Documents and Settings\luismg\Escritorio\IronPython-2.0A
8\DLLs')
>>> from ClassLibrary1 import *
Traceback (most recent call last):
File , line unknown, in ##235
File , line unknown, in _stub_##2
ImportError: No module named ClassLibrary1

Fuzzyman

unread,
Feb 7, 2008, 3:52:34 AM2/7/08
to

Luis M. González wrote:
> On 6 feb, 21:17, Fuzzyman <fuzzy...@gmail.com> wrote:

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

Luis M. González

unread,
Feb 7, 2008, 9:35:28 AM2/7/08
to

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...

Christian Heimes

unread,
Feb 7, 2008, 10:55:48 AM2/7/08
to pytho...@python.org
Luis M. González wrote:
> 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

Huayang Xia

unread,
Feb 7, 2008, 1:28:50 PM2/7/08
to
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?

Christian Heimes

unread,
Feb 7, 2008, 2:22:18 PM2/7/08
to pytho...@python.org

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

Fuzzyman

unread,
Feb 7, 2008, 3:30:25 PM2/7/08
to

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

Huayang Xia

unread,
Feb 7, 2008, 4:38:19 PM2/7/08
to

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?

Fuzzyman

unread,
Feb 7, 2008, 6:42:17 PM2/7/08
to

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

Dino Viehland

unread,
Feb 12, 2008, 10:26:24 PM2/12/08
to pytho...@python.org
>>
>> 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.

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.

Luis M. González

unread,
Feb 13, 2008, 1:58:34 PM2/13/08
to

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

Fuzzyman

unread,
Feb 14, 2008, 4:26:57 PM2/14/08
to
On Feb 13, 6:58 pm, "Luis M. González" <luis...@gmail.com> wrote:
> On 13 feb, 00:26, Dino Viehland <di...@exchange.microsoft.com> wrote:
>
> > >> 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.
>
> > This *should* work in bothIronPython1.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?

If you have the CPython standard library in your IRONPYTHONPATH, then
when IronPython does it's normal 'import site' on startup the CPython
one will be used instead of the default IronPython one...

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

Luis M. González

unread,
Feb 15, 2008, 7:22:37 AM2/15/08
to

I see. Thank you!

Luis

0 new messages