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

shuwj shuwj5460 at 163.com
Sun Mar 25 13:33:22 CEST 2012


> 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
-----------------------------------------


gdiplus.py  is as following:
-----------------------------------------
#coding: gbk

from ctypes import *
from ctypes.wintypes import *
from comtypes import GUID

oleaut32 = windll.oleaut32
gdiplus = windll.gdiplus


class GdiplusStartupInput(Structure):
    _fields_ = [
        ('GdiplusVersion', c_uint32),
        ('DebugEventCallback', c_void_p),
        ('SuppressBackgroundThread', BOOL),
        ('SuppressExternalCodecs', BOOL)
    ]

class GdiplusStartupOutput(Structure):
    _fields = [
        ('NotificationHookProc', c_void_p),
        ('NotificationUnhookProc', c_void_p)
    ]


class PicDesc(Structure):
    _fields_ = [
        ('Size', UINT),
        ('Type', UINT),
        ('hPic', HBITMAP),
        ('hPal', HPALETTE)
    ]

PICTYPE_BITMAP = 1

def LoadImage(filename):
    '''Load an image from a file.
    '''

    #Initaialize GDI+
    token = c_ulong()
    startup_in = GdiplusStartupInput()
    startup_in.GdiplusVersion = 1
    startup_out = GdiplusStartupOutput()
    u = gdiplus.GdiplusStartup( byref(token), byref(startup_in), byref(startup_out))

    fname = LPOLESTR( filename )
    bitmap = c_void_p()
    gdiImage = c_void_p()
    x = LPVOID()
        
    if u == 0:
        print u
        #Load the image
        v = gdiplus.GdipCreateBitmapFromFile( fname, byref(gdiImage) )
        if v == 0:
            print v

            #Create a bitmap handle from the GDI image
            gdiplus.GdipCreateHBITMAPFromBitmap( gdiImage, byref(bitmap), 0 )

            #Create the IPicture object from the bitmap handle
            IID_IPicture = GUID('{7BF80980-BF32-101A-8BBB-00AA00300CAB}' )
            picinfo = PicDesc( Type=PICTYPE_BITMAP, hPic=bitmap, hPal=0  )
            picinfo.Size = picinfo.__sizeof__()
            
            print oleaut32.OleCreatePictureIndirect( byref(picinfo), byref(IID_IPicture), True, byref(x) )
            
            gdiplus.GdipDisposeImage( gdiImage)

        gdiplus.GdiplusShutdown( token )
        
    
    return x 
    
    
    
    
    

-- 
shuwj <shuwj5460 at 163.com>




More information about the python-win32 mailing list