3

Main goal: Create a wrapper for a C# library, which can be used in Python (2.6).

UPDATE: Now, I have updates to the method I am using, which is however not working well.

The code for the simple C# class library:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Test
{
    [Guid("8F38030D-52FA-4816-B587-A925FDD33302")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface _TestClass
    {
        [DispId(1)]
        string Eureka();
    }

    [Guid("BC3F6BB3-42C4-4F30-869A-92EA45BF68D2")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Test.TestClass")]
    public class TestClass : _TestClass
    {
        public TestClass()
        {
        }

        public string Eureka()
        {
            return "Hudson, we no longer have a problem!";
        }
    }
}

enter code here

In addition to this, I went into Project Properties and enabled the setting: Register for COM interop.

Also, in order to make the class library available to COM, I ticked Signing -> Sign the Assembly, and gave it a strong key.

Furthermore, whenever I compile, I unregister the old version with:

regasm -u Test /tlb:Test

And I register it with:

regasm Test.dll /tlb:Test

My problem is then, in the Python environment, I have the following main.py, which is not working:

import win32com.client

o = win32com.client.Dispatch("Test.TestClass")

The error is unforgiven.

thank you in advance!

Mossa
  • 1,656
  • 12
  • 16
  • 2
    possible duplicate of [How to load a C# dll in python?](http://stackoverflow.com/questions/2077870/how-to-load-a-c-dll-in-python) – Preet Sangha Aug 10 '11 at 09:44
  • I've read that, and it is the same topic, however I am not figuring out my problem though. – Mossa Aug 10 '11 at 14:30

1 Answers1

3

A alternative would be if you you use Python for .NET. There seem to be alpha releases for Windows CPython 2.6 and 2.7 available. You could run simply:

import clr
clr.AddReference("Your.Assembly.Name")
import Test
test = Test.TestClass()
print test.Eureka()
jazz
  • 2,371
  • 19
  • 23
  • I see your answer, and I get that it is a valid solution, however I would like to make my library COM visible, because that will enable me to use it within PHP and other programming languages that has COM support. – Mossa Aug 10 '11 at 21:15