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!

2
  • 2
    possible duplicate of How to load a C# dll in python? Commented Aug 10, 2011 at 9:44
  • I've read that, and it is the same topic, however I am not figuring out my problem though. Commented Aug 10, 2011 at 14:30

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.