Eduardo
Eduardo

Reputation: 1837

Using a COM dll in python?

I need to use an COM dll (made in CSharp) in my python project. I tried to follow this example Using COM Objects in Scripting Languages -- Part 2 (Python), but I dont have success.

My python code is:

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

When I run the python class, this error occurs:

> 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

Error line:

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

In my CSharp Project, I have this in my COM dll:

Interface

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

Class

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

This COM project above, is used in others applications and works fine. My problem is only in python.

In time: my COM dll is registered in windows registry

Any Help?

Upvotes: 3

Views: 2994

Answers (1)

Eduardo
Eduardo

Reputation: 1837

Here I found a tip that helped me use my COM dll using the TLB file:

Accessing unregistered COM objects from python via a registered TLB

Thanks to Márcio Faustino.

Upvotes: 3

Related Questions