2011-08-10 30 views
3

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

更新:现在,我对我正在使用的方法进行了更新,但该方法运行不正常。

了简单的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库可用,我打勾签署 - >签署大会,并给它一个强大的关键。

此外,每当我编译,我注销旧版本:

regasm -u Test /tlb:Test 

和我一起进行注册:

regasm Test.dll的/ TLB:测试

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

import win32com.client 

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

错误是不可宽恕的。

谢谢你提前!

+2

可能重复(http://stackoverflow.com/questions/2077870/how-to-load-ac-dll-in-蟒蛇) –

+0

我读过,这是相同的话题,但我没有搞清楚我的问题。 –

回答

2

另一种方法是如果你使用Python for .NET。似乎有可用的​​的alpha版本。你可以简单地运行:?如何加载在Python C#DLL]

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

我看到了你的答案,并且我知道这是一个有效的解决方案,但是我想让我的库COM可见,因为这将使我能够在PHP和其他支持COM的编程语言中使用它。 –