Hi,

Recently we noticed the same problem with our project.

We access the MS Word object model via IDispatch, but  there is a 
GetPoint method which takes left, top, hight and width as byref arguments.

As comtypes doesn't support vt_byref out of the box, our first idea was 
to rewrite the code for VARIANT._set_value(), and provide comtypes with 
a patch.

However, looking at VARIANT._set_value():
  There are lots of returns all the way through the function, so its 
clear it would have to be rewritten almost from the ground up. And since 
I'm not too skilled with all of the VARIANT types, I don't want to break 
any existing logic.

So, what we currently do, is when our project imports comtypes at 
initialization time, we do the following:

#Monkey patch comtypes to support byref in variants
from comtypes.automation import VARIANT, VT_BYREF
from ctypes import cast, c_void_p
from _ctypes import _Pointer

oldVARIANT_value_fset=VARIANT.value.fset

def newVARIANT_value_fset(self,value):
        realValue=value
        if isinstance(value,_Pointer):
                try:
                        value=value.contents
                except (NameError,AttributeError):
                        pass
        oldVARIANT_value_fset(self,value)
        if realValue is not value:
                self.vt|=VT_BYREF
                self._.c_void_p=cast(realValue,c_void_p)

VARIANT.value=property(VARIANT.value.fget,newVARIANT_value_fset,VARIANT.value.fdel)

So, from then on, when ever passing an instance of a POINTER of a ctypes 
type, comtypes will automatically treet the value as byref.

Some example code for GetPoint:

                from ctypes import c_long, pointer
                rangeLeft=c_long()
                rangeTop=c_long()
                rangeWidth=c_long()
                rangeHeight=c_long()
         
self.obj.WinwordWindowObject.getPoint(pointer(rangeLeft),pointer(rangeTop),pointer(rangeWidth),pointer(rangeHeight),self._rangeObj)

I would probably prefer that we use ctypes.byref, rather than 
ctypes.POINTER, as the name relates better to what its doing. But, 
ctypes.byref(x) yields an object who's type is something like 
type(cArgObject), which I can't find anywhere in ctypes or _ctypes. So, 
it would be a little hard for VARIANT._set_value to properly identify 
that value was a byref.

Interestingly enough though,  if comtypes compiles the actual COM 
interfaces for the MS Word object model, POINTER is used anyway, so I 
guess POINTER is ok.

Mick




On 13/02/2010 10:48 AM, Pablo Bianucci wrote:
> Hi Thomas!
>
> On Fri, 12 Feb 2010, Thomas Heller wrote:
>
>> Exactly.  I cannot provide a patch, but here's a code snippet that creates
>> such a beast:
>
> Yes! It works!!
>
> This is the final working snippet:
>
> <snip>
> travel = c_float(0)
> v = VARIANT()
> v._.c_void_p = cast(pointer(travel), c_void_p)
> v.vt = VT_BYREF | VT_R4
>
> mode = c_int(0)
> vm = VARIANT()
> vm._.c_void_p = cast(pointer(mode), c_void_p)
> vm.vt = VT_BYREF | VT_I4
>
> try:
>       piezo1.GetMaxTravel(APTPiezoLib.CHAN1_ID, v)
>       piezo1.GetControlMode(APTPiezoLib.CHAN1_ID,  vm)
> except Exception, e:
>       print "Exception:", e
> print travel.value, mode.value
> </snip>
>
> If you are not careful when setting the types you get a "Type Mismatch"
> error (even though the protoypes in the help were the same, I had to use
> different types for the two funcions there).
>
> I wonder why you can't integrate this into the package. I'm fine as this,
> as this will let me write the program I need, but it would be more user
> friendly to be able use pointers as arguments for the method invocation.
> But I don't understand COM internals, so it could be that implementing
> that could lead to other problems.
>
> Thank you very much for your help!
>
> Bye&  Good Luck!
>
> Pablo.
>
> P.S.: If you visit Canada and stay at a city I happen to be living in,
> consider yourself invited to a drink. ;-)
>
>
> ------------------------------------------------------------------------------
> SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
> Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
> http://p.sf.net/sfu/solaris-dev2dev
> _______________________________________________
> comtypes-users mailing list
> comtypes-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/comtypes-users


-- 
Michael Curran
President, NV Access Inc
Email: m...@nvaccess.org
Website: www.nvaccess.org
ABN 61773362390.

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to