在python中使用COM dll?

在python中使用COM dll?,python,python-2.7,com,comtypes,Python,Python 2.7,Com,Comtypes,我需要在我的python项目中使用一个COM dll(用CSharp制作)。 我试图效仿这个例子,但没有成功 我的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 type

我需要在我的python项目中使用一个COM dll(用CSharp制作)。 我试图效仿这个例子,但没有成功

我的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))
> 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
运行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))
> 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
在我的CSharp项目中,我的COM dll中有以下内容:

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))
> 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
接口

[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注册表中注册


有什么帮助吗?

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

多亏了马尔西奥·福斯蒂诺