[python-win32] Access to .NET components from Python (Was: Win32 COM starter book)
Moore, Paul
Paul.Moore@atosorigin.com
Fri, 20 Dec 2002 11:12:44 -0000
From: Moore, Paul=20
>>> This reminded me of something I wondered about the other day.
>>> Is there any extension available which allows Python to access
>>> .NET components?
>> Yes - win32com <wink>. The COM interop is very good,
> Ooh. Must have a look.
Hmm. Either I'm missing something, or it's not as easy as all that :-(
I was going to try a simple use of System.Console, but AFAICT, System
either isn't registered, or somehow doesn't work as I expected. OK,
scrap that for now and write my own little assembly:
using System;
public class CTest {
public void Test() {
Console.Write("Hola ");
Console.WriteLine("Mundo!");
Console.WriteLine("What is your name: ");
String name =3D Console.ReadLine();
Console.Write("Buenos Dias, ");
Console.Write(name);
Console.WriteLine("!");
}
}
Compile with csc /target:library MyAssembly.cs, register with
regasm MyAssembly.dll /tlb:MyAssembly.tlb, and try to test it. Nope,
OLE error 0x80131522. Google tells me that this means I need my
assembly in the same directory as the executable! (That's Python, not
the script - so definitely not where I'd like to put my random DLLs).
OK, copy the thing there for now.
>>> from win32com.client import Dispatch
>>> net =3D Dispatch("CTest")
>>> net.Test()
Hola Mundo!
What is your name:
kjhas
Buenos Dias, kjhas!
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable
Pretty good - it works. But what is the TypeError at the bottom? My C#
code has finished OK. Is it something "on the way back" from .NET to
Python?
So, a mixed success so far...
Paul.