在Python中为C库创建包装器

2024-05-03 02:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

主要目标:为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,并给它一个强键。在

此外,无论何时编译,我都会注销旧版本:

^{pr2}$

我把它注册到:

regasm Test.dll /tlb:Test

我的问题是,在Python环境中,我有以下情况主.py,但不起作用:

import win32com.client

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

这个错误是不可原谅的。在

提前谢谢你!在


Tags: 方法代码testcomclient目标string类库