【问题标题】:Using a COM dll in python?在 python 中使用 COM dll?
【发布时间】:2014-09-13 04:15:33
【问题描述】:

我需要在我的 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 注册表中注册

有什么帮助吗?

【问题讨论】:

    标签: python python-2.7 com comtypes


    【解决方案1】:

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

    Accessing unregistered COM objects from python via a registered TLB

    感谢马尔西奥·福斯蒂诺。

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2011-05-05
      • 2012-03-09
      • 2014-10-27
      • 2017-02-15
      • 2012-11-01
      • 1970-01-01
      • 2011-05-31
      • 2012-02-04
      相关资源
      最近更新 更多