在python中使用COM dll?

时间:2014-07-22 11:09:27

标签: python python-2.7 com comtypes

我需要在我的python项目中使用COM dll(在CSharp中制作)。 我试着遵循这个例子Using COM Objects in Scripting Languages -- Part 2 (Python),但我没有成功。

我的python代码是:

import sys

# for TkInter GUI support
from Tkinter import *
import tkMessageBox
import tkColorChooser

# for COM support
import comtypes.client as cc
import comtypes

# Load the typelibrary registered with the Windows registry
tlb_id = comtypes.GUID("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}")
cc.GetModule((tlb_id, 1, 0))

当我运行python类时,会发生以下错误:

> Traceback (most recent call last):   File "C:\Program Files
> (x86)\JetBrains\PyCharm 129.696\helpers\pydev\pydevd.py", line 1481,
> in <module>
>     debugger.run(setup['file'], None, None)   File "C:\Program Files (x86)\JetBrains\PyCharm 129.696\helpers\pydev\pydevd.py", line 1124,
> in run
>     pydev_imports.execfile(file, globals, locals) #execute the script   File "C:/aa_python/gtk_project/main.py", line 7, in <module>
>     from com_class_file import ComClass   File "C:/aa_python/gtk_project\com_class_file.py", line 15, in <module>
>     cc.GetModule((tlb_id, 1, 0))   File "C:\Python27\lib\site-packages\comtypes-1.1.0-py2.7.egg\comtypes\client\_generate.py",
> line 101, in GetModule
>     tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib[0]), *tlib[1:])   File "C:\Python27\lib\site-packages\comtypes-1.1.0-py2.7.egg\comtypes\typeinfo.py",
> line 473, in LoadRegTypeLib
>     _oleaut32.LoadRegTypeLib(byref(GUID(guid)), wMajorVerNum, wMinorVerNum, lcid, byref(tlib))   File "_ctypes/callproc.c", line
> 945, in GetResult WindowsError: [Error -2147319779] Library not registered

错误行:

cc.GetModule((tlb_id, 1, 0))

在我的CSharp项目中,我在我的COM dll中有这个:

接口

[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")]
public interface ITestCOM
{
    string say_hello();
}

[ClassInterface(ClassInterfaceType.None), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
public class TestCOM : ITestCOM
{
    public string say_hello()
    {
        return "hello world!";
    }
}

上面的这个COM项目,在其他应用程序中使用并且工作正常。我的问题只出现在python中。

及时:我的COM dll已在Windows注册表中注册

任何帮助?

1 个答案:

答案 0 :(得分:3)

在这里,我找到了一个帮助我使用TLB文件来使用COM dll的提示:

Accessing unregistered COM objects from python via a registered TLB

感谢MárcioFaustino。