[python-win32] Python COM: Automatic wrap/unwrap?

Mark Hammond mhammond at skippinet.com.au
Tue Nov 11 07:39:30 CET 2008


> It seems that the basic Python types are wrapped and
> unwrapped automatically when needed. Is there some way of
> extending this mechanism? Is there a class I can inherit
> from, or a magic attribute I can set, or some registration
> process I can use, to get instances of my class automatically
> wrapped and unwrapped?

Unfortunately not - autowrap support has been 'on the list' for a long time
now, but it never makes it to the top.  The "right" place to do this would
be in pythoncom itself in the function used to convert a PyObject to a COM
pointer.

What you could do is write a custom policy derived from
win32com.server.policy.DesignatedWrapPolicy().  You could override
_invokeex_, and examine the return value from each of the functions, and
when you decide to, perform the wrapping at that stage.  As long as each
object you wrap also uses that same policy, then all objects will get the
same behaviour - its just that "first" wrap you will need to explicitly do
specifying your policy.  This is a little messy if you return arrays etc,
but might be doable in the simple case.

Cheers,

Mark



More information about the python-win32 mailing list