[python-win32] a question about word addin , IRibbonExtensibility, GetCustomUI, getImage, IPicture

Gilles Baggieri gilles.baggieri at gmail.com
Fri Dec 14 01:04:44 CET 2012


Mark Hammond <skippy.hammond <at> gmail.com> writes:

> 
> I'm not familiar enough with ctypes to know if that makes sense, but I 
> doubt it - nothing would seem to know the size of the buffer pointed at 
> by 'x'.  You probably need to stick with using a python string object - 
> ensure the string contains exactly the bytes of the metafile.
> 
> Mark
> 
> On 25/03/2012 10:33 PM, shuwj wrote:
> >
> >> I believe a c_void_p is a ctypes construct which aren't supported by
> >> pywin32.  You need to convert it to a "normal" Python type.  I'm
> >> guessing it is binary data, so in py2k, you should be able to simply use
> >> buffer(some_string_object).  The error message seems to imply you may
> >> even be able to use buffer(c_void_p_object), but I don't know how they
> >> work well enough to suggest that will actually work.
> >>
> >> Mark
> >>
> >
> > Hi Mark,
> > Thanks for your reply. I use buffer() in GetImage method and there's no
> > exception now,  but they don't work as expected.
> >
> >
> > -----------------------------------------
> > class wordaddin:
> >      def GetImage(self,ctrl):
> >          from gdiplus import LoadImage
> >          i = LoadImage( 'c:/edit.png' )
> >          i = buffer(i)
> >          print i, 'ddd'
> >          return i
> > -----------------------------------------
> >
> >
> 
Hello,

After spending several hours with the same problem (thanks for gdiplus.py, great 
job!), I've found a solution from Thomas Heller's comtypes testunits. The idea 
is to cast 'x' (pointer) to a comtypes POINTER(IUnknown), and then convert again 
to pythoncom PyIDispatch.
Add this to the end of gdiplus LoadImage (but don't use buffer() as the returned 
object can be return as is to RibbonX callback):


    ...
    if not x.value:
        return None

    from comtypes.automation import VARIANT, IUnknown, IDispatch
    from ctypes import cast, POINTER, PyDLL, py_object, c_void_p, byref
    from ctypes.wintypes import BOOL
    import pythoncom

    # cast to comtypes IUnknown
    cobj = cast(x,POINTER(IUnknown))

    # cast to PyIDispatch
    _PyCom_PyObjectFromIUnknown = 
PyDLL(pythoncom.__file__).PyCom_PyObjectFromIUnknown
    _PyCom_PyObjectFromIUnknown.restype = py_object
    _PyCom_PyObjectFromIUnknown.argtypes = (POINTER(IUnknown), c_void_p, BOOL)

    return _PyCom_PyObjectFromIUnknown(cobj, byref(IDispatch._iid_), True)


Perhaps is there a shorter (and clearer!) way...

Gilles




More information about the python-win32 mailing list