【问题标题】:Creating a wrapper for a C# library in Python在 Python 中为 C# 库创建包装器
【发布时间】:2011-08-10 09:41:42
【问题描述】:

主要目标:为 C# 库创建一个包装器,可以在 Python (2.6) 中使用。

更新:现在,我对正在使用的方法进行了更新,但效果不佳。

简单 C# 类库的代码:

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

除此之外,我进入项目属性并启用设置:注册 COM 互操作。

另外,为了使 COM 可以使用类库,我勾选了 Signing -> Sign the Assembly,并给了它一个强键。

此外,每当我编译时,我都会取消注册旧版本:

regasm -u Test /tlb:Test

我注册它:

regasm Test.dll /tlb:Test

然后,我的问题是,在 Python 环境中,我有以下 main.py,它不起作用:

import win32com.client

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

错误不可原谅。

提前谢谢你!

【问题讨论】:

标签: c# python wrapper


【解决方案1】:

如果您使用Python for .NET,另一种选择是。 Windows CPython 2.6 and 2.7 似乎有 alpha 版本可用。你可以简单地运行:

import clr
clr.AddReference("Your.Assembly.Name")
import Test
test = Test.TestClass()
print test.Eureka()

【讨论】:

  • 我看到了你的回答,我知道这是一个有效的解决方案,但是我想让我的库 COM 可见,因为这将使我能够在 PHP 和其他具有COM 支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多