[python-win32] win32com.client: Struggling with dynamic dispatch, behavior differs when debugging

Dominik Heller dominik.heller1 at gmail.com
Thu Sep 29 03:29:45 EDT 2016


Hi Tim, thanks for your reply, much appreciated.

> Do you have an IDL file or a C++ header file? It might be possible for
> you to build a type library.

I do even have the complete C++ source. However I currently have no
possibility to generate the IDL-files in an automated way and it's too
much to do by hand, let alone maintaining these files.
It might be possible to have another look into this in the future, maybe
even generating these files with a script or something.

>
>> obj_2.DoSomething(“MyArgument”) # doesn’t work
>>
>
>What is the C++ signature of this method?
>

This would be the declaration in the C++ class header:

class CMyCommand : public CCmdTarget {
  // ...
  afx_msg void DoSomething(LPCTSTR message);
  // ...
};

with this being the dispatch macro in the corresponding cpp:

DISP_FUNCTION(CMyCommand, "DoSomething", DoSomething, VT_EMPTY, VTS_BSTR)

Basically, it seems that on the obj_2 "main" dispatch, I can't use any
methods which do require parameters (such as the BSTR here).

Having GetSomeNumber as a property sounds false to me as well, it shouldn't
be possible to set the value this COM method returns.
It is declared/dispatched with:

afx_msg long GetSomeNumber();
DISP_FUNCTION(CMyCommand, "GetSomeNumber", GetSomeNumber, VT_I4, VTS_NONE)


> Dynamic dispatch has rather strict rules on the data types and parameter
> usages.  Many C++ programmers totally disregard those rules, and treat a
> COM interface as if it were a simple pure virtual base class.  It's not
> hard to build C++ interfaces that cannot be called dynamically.

So if I understand you correctly, it should be possible to use all these
methods even with a dynamic python dispatch object, as long as they're
implemented correctly on C++ side? What could be the thing going wrong in
the C++ code?

> Pretty much.  The C++ implementation is expected to identify one or more
> [output] parameters.  The Python wrapper will return those values.  You
> don't pass a location for them.

So if I'm aware that a python tuple is returned and the parameter list for
the call is thus shorter, I can still call it on a dynamic dispatch?

Basically I'm trying to find out how much my life would get easier if I
somehow generated those IDL-files, or if rather the existing C++ COM
interface needs to be fixed.


More information about the python-win32 mailing list