Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a TLB-defined interface with Python and COM

Tags:

python

com

I will try to keep this question as tight as possible, but if it seems that I am saying insane things, it is almost certainly because I am ignorant of some key point, so please do correct me.


I am writing a program, in a Windows environment, that will interface with an existing application that has a COM interface to allow 3rd-party software to interact with it.

I have read all of the documentation for this application, and it says that there is a TLB file that defines the functions and data available via COM.

How do I use the TLB file with python? How do I discover the progID of the application so that I can interface with it (this isn't given in the documentation).

I'm pretty lost. I have a fair amount of experience with Python, but I am completely new to developing in a Windows environment. Any help would be enormously helpful. I have been reading all the documentation on win32com, but I still have no clue what to do, as no one addresses -- as far as I have seen -- bringing in a TLB file.

like image 808
user1336958 Avatar asked Apr 16 '12 18:04

user1336958


2 Answers

OK, it's been a while since I've done this, and I'm not a COM expert by any means. Read the COM chapter from Python Programming on Windows to see how to do this. Follow along with the examples (trying things out against Excel) to get a feel of how things work.

First off, install the PyWin32 Extensions if you haven't already. This is the package that gives you pythonwin.exe and the COM interface modules. Get it from here.

Then you are going to open the "COM Makepy Utility" from PythonWin's Tools menu. Browse through the list of registered COM components (some will be typelibs, others DLLs) until you identify the one you have (you have to do a bit of detective work). Click OK to generate the Python glue code. You will then need to run it again with the -i command-line argmument to generate the boilerplate code so your python script can use this glue. Here's a paraphrase of the O'Reilly example for the Microsoft Excel Object library:

import win32com.client

from win32com.client import gencache
gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 2)
earlyBound = win32com.client.Dispatch("Excel.Application")
lateBound = win32com.client.dynamic.Dispatch("Excel.Application")

print earlyBound.ActiveCell()

Using early-bound objects is optional, but it does improve performance.

To find the ProgID is again a bit of detective work, although this answer seems to imply it's going to be hard. Try poking around the HKEY_CLASSES_ROOT hive of the registry with RegEdit to see if you see a ProgID that looks promising.

like image 20
Gordon Broom Avatar answered Nov 05 '22 06:11

Gordon Broom


The questions asked is to link the custom TLB file with COM client to be developed in python. I have done a small example code for my COM server developed in C# and same is accessed by python client using "comtypes" package. The below snippet of code gives:

    import comtypes.client as CC
    import comtypes

    ccHandle = CC.CreateObject("CSharpServer.InterfaceImplementation")
    print (ccHandle)
    import comtypes.gen.CSharpServer as CS
    InterfaceHandle = ccHandle.QueryInterface(CS.IManagedInterface)

    print ("output of PrintHi function = ", InterfaceHandle.PrintHi("World"))

The above python script is for the C# COM server code available at http://msdn.microsoft.com/en-us/library/aa645738(v=vs.71).aspx (refer to the File 1: CSharpServer.cs).

like image 98
Susen Avatar answered Nov 05 '22 07:11

Susen