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
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
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?
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
You can use PythonDotNET if you want to access .NET assemblies in
CPython (the standard Python implementation written in C).
Christian
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
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
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
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
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
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
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
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
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?
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
I see. Thank you!
Luis