Hi, I know PythonNet has been specially designed to handle .NET/CLR compatibility but because of some other restrictions I want to use Python 2.3 to access a C# function. First of all is it possible? I am trying with following piece of code in C# (Server app) and python (client app). This is a class library in C# which is compiled and registered with following commands. csc /target:library Class1.cs regasm Try2005.dll /tlb:Try2005.tlb using System; using System.Runtime.InteropServices; namespace Try2005 { public class TestingCSBC { [Guid("1EB394AB-2D4A-4a52-9F22-E8ACAED4800F")] public interface IManagedInterface { int PrintHi(string name); } [Guid("2FD76301-489A-4dc3-BF6B-5DF6FCE96CB8")] public class InterfaceImplementation : IManagedInterface { public int PrintHi(string name) { Console.WriteLine("Hello, {0}!", name); //TestCSBCClass obj = new TESTDLLCOMLib.TestCSBCClass(); //obj.Displaymessage("In C# application!!"); return 33; } } } } Python code: import win32com.client try: serverObj = win32com.client.Dispatch("Try2005.TestingCSBC") except Exception,e: print "Dispatch error",e try: serverObj.PrintHi("But will it work on Tribon??") except Exception,e: print "Error while Calling C# function",e After running the above Py script I am getting following error output. Dispatch error (-2147024894, 'The system cannot find the file specified.', None, None) Error while Calling C# function name 'serverObj' is not defined Any pointers would be greatly appreciated. Thanks in advance.
Hi Amod - Is the main issue the fact that you have to use Python 2.3? If so, you could use the beta 3 release (which was based on 2.3), or see the README.txt for instructions on building the b4 release for Python 2.3. In any case, you should be able to just copy Python.Runtime.dll and CLR.dll from the PythonNet directory to the root directory of your python installation (presuming they were built against 2.3), and then 'import CLR' from your exising Python... Brian Lloyd brian@zope.com V.P. Engineering 540.361.1716 Zope Corporation http://www.zope.com -----Original Message----- From: pythondotnet-bounces@python.org [mailto:pythondotnet-bounces@python.org]On Behalf Of Amod Kulkarni Sent: Monday, January 03, 2005 2:38 AM To: pythondotnet@python.org Subject: [Python.NET] Calling C# from Python 2.3 Hi, I know PythonNet has been specially designed to handle .NET/CLR compatibility but because of some other restrictions I want to use Python 2.3 to access a C# function. First of all is it possible? I am trying with following piece of code in C# (Server app) and python (client app). This is a class library in C# which is compiled and registered with following commands. csc /target:library Class1.cs regasm Try2005.dll /tlb:Try2005.tlb using System; using System.Runtime.InteropServices; namespace Try2005 { public class TestingCSBC { [Guid("1EB394AB-2D4A-4a52-9F22-E8ACAED4800F")] public interface IManagedInterface { int PrintHi(string name); } [Guid("2FD76301-489A-4dc3-BF6B-5DF6FCE96CB8")] public class InterfaceImplementation : IManagedInterface { public int PrintHi(string name) { Console.WriteLine("Hello, {0}!", name); //TestCSBCClass obj = new TESTDLLCOMLib.TestCSBCClass(); //obj.Displaymessage("In C# application!!"); return 33; } } } } Python code: import win32com.client try: serverObj = win32com.client.Dispatch("Try2005.TestingCSBC") except Exception,e: print "Dispatch error",e try: serverObj.PrintHi("But will it work on Tribon??") except Exception,e: print "Error while Calling C# function",e After running the above Py script I am getting following error output. Dispatch error (-2147024894, 'The system cannot find the file specified.', None, None) Error while Calling C# function name 'serverObj' is not defined Any pointers would be greatly appreciated. Thanks in advance.
The docn says that any .NET class to be used from COM must have a public no-parameters constructor. I don't see one in any of the classes you defined. Nesting classes the way you have is not, I don't think, going to be compatible with being called from COM. The class you seem to be asking COM to create (the outer class) is not one that has a PrintHi method. Also, some samples suggest that you might want to use more attributes: [ClassInterface(ClassInterfaceType.AutoDispatch)] [ProgID("Try2005.TestingCSBC")] on the class you want to be callable. I don't think those are required, but they'll increase the explicit info that you're providing to the code that builds the COM-callable-wrapper (the mechanism that exposes the .NET code to COM) for your class. However, for each of those issues, I'm surprised that you're getting the message "cannot find the file specified." You could try using FileMon (from sysinternals.com) to figure out what file it's looking for that's not being found; you could try calling this from VB or VBA and see if you've got the same problem; you could use OLEView.exe (or raw registry examination) to look at what the COM defn of your class is. I'm sure that you can do simple calls to simple .NET objects from Python 2.x without using Python.NET -- but if you can't use the COM object from VB (when declaring the variable as type Object), you won't be able to use it via Python's win32com.client.Dispatch. You might want to look at (perhaps wrapped) http://aspn.activestate.com/ASPN/docs/ActivePython/2.3/pywin32/html/com/win3... for info about using makepy.py to generate a Python "wrapper class" so that you don't have to work with a "raw" COM IDispatch. (If you change 2.3 to 2.4 you get some more info, but apparently nothing about functionality only in 2.3.) Good luck. At 02:37 AM 1/3/2005, Amod Kulkarni wrote
Hi,
I know PythonNet has been specially designed to handle .NET/CLR compatibility but because of some other restrictions I want to use Python 2.3 to access a C# function. [snip]
J. Merrill / Analytical Software Corp
participants (3)
-
Amod Kulkarni
-
Brian Lloyd
-
J. Merrill