Mark Hammond schrieb:
>> comtypes (http://starship.python.net/crew/theller/comtypes) supports
>> this. You have to create a type library, probably with MS tools),
> Excellent! The only problem I saw was the actual creation of the typelib.
Did you understand what I was trying to say? I write an IDL file and compile
it with the Microsoft MIDL compiler to get a TLB file. (Several years ago I
wrote a script that would create a TLB file by using the ICreateTypeLib interface
directly - you may remember it - but this was never used in production).
Nowadays I always use the MIDL compiler, anything else is not worth the effort imo.
> Once you get the typelib itself built, you should be in the exact same
> position with pywin32 as with ctypes - hopefully Thomas can elaborate on
> what steps you need to take (or on what steps pywin32 needs to take to get
> "up-to-par" here...)
The short description for comtypes, once you have the typelib, is this:
- call 'comtypes.client.GetModule(<path-to-typelib.tlb>) to generate the comtypes
wrapper for the interface(s); this will also print out the name of the generated
- The generated modules contain nearly all the boilerplate code; you now write a script
that contains the COM server itself by subclassing the generated CoClass in the wrapper.
Several attributes must be defined in the subclass: the progid and clsid for the coclass
(these are not in the generated code), plus possibly one or two optional attributes.
- You write a 'if __name__ == "__main__":' section that calls
comtypes.server.UseCommandline() with your coclass or coclasses (very much as in pywin32)
When you now register the script with 'python myscript.py /regserver' you can already
create instances of the com object, and the com object already implements several interfaces:
- the interface(s) you have defined in the IDL file
- IConnectionPointContainer if the object has outgoing interfaces.
However, methods on the interfaces defined in the IDL file return E_NOTIMPL because
there is no implementation for them - but the generated wrapper code contains commented
out template code that one could copy and paste as a starting point.
A simple py2exe setup script to create a com server dll looks like this:
from distutils.core import setup
TestComServer = dict(
modules = ["TestComServer"],
other_resources = [("TYPELIB", 1, file(r"TestComServer.tlb", "rb").read())],
dest_base = r"testComServer"
ctypes_com_server = [TestComServer]
What is needed for pywin32? I guess pywin32 should load the typelib resource
from the tlb file (when running as script) or from the exe/dll (when running frozen)
and expose type information via IProvideClassInfo and IDispatch::GetTypeInfo.
Of course the setup script looks different for a pywin32 com server.