10. windows.com - Component Object Model

A module to call COM interfaces from Python or COM vtable in python.

This code is only used in windows.winobject.wmi and windows.winobject.network for the firewall. The ability to create COM vtable is used in the LKD project .

10.1. Using a COM interface

It’s possible to directly call COM interface from python. All you need is the definition of the COM interface.

There are three ways to get the definition of the code interface:

To generate a COM interface you need its definition from the “.c” file. Then add thisit to PythonForWindows\ctypes_generation\com\MyInterface.txt. Finally re-generate the interface using generate.py.

When you have the COM interface defintion you can create an instance of it. Then you need to retrieve the interface by using an API returning an object or window.com.create_instance(). You can then use the instance to call whatever method you need.

Note

see samples windows.com

10.2. Implementing a COM interface

To create COM object you need to:

  1. Create your COMImplementation with an IMPLEMENT attribute that should be a cominterface CODE 1

  2. Implements the methods of the interface CODE 2

  3. Create an instance CODE3

  4. Pass it to whatever native function expects it CODE4

Note

see samples ICallInterceptor

10.3. API

windows.com.init()[source]

Init COM with some default parameters

windows.com.create_instance(clsiid, targetinterface, custom_iid=None, context=5)[source]

A simple wrapper around CoCreateInstance <https://msdn.microsoft.com/en-us/library/windows/desktop/ms686615(v=vs.85).aspx>

class windows.com.COMImplementation[source]

The base class to implements COM object respecting a given interface

AddRef(*args)[source]

Default AddRef implementation that returns 1

QueryInterface(this, piid, result)[source]

Default QueryInterface implementation that returns self if piid is the implemented interface

Release(*args)[source]

Default Release implementation that returns 1