python, ctypes and GetIconInfo issue

mymyxin at gmail.com mymyxin at gmail.com
Thu May 5 16:47:05 EDT 2016


Hello,

I try to make the GetIconInfo function work, but I can't figure out
what I'm doing wrong.

>From the MSDN documentation the function is

https://msdn.microsoft.com/en-us/library/windows/desktop/ms648070%28v=vs.85%29.aspx

    # BOOL WINAPI GetIconInfo(
    # _In_  HICON     hIcon,
    # _Out_ PICONINFO piconinfo
    # );

which I defined as

    GetIconInfo = windll.user32.GetIconInfo
    GetIconInfo.argtypes   = [HICON, POINTER(ICONINFO)]
    GetIconInfo.restype    = BOOL
    GetIconInfo.errcheck   = ErrorIfZero


The structure piconinfo is described as
https://msdn.microsoft.com/en-us/library/windows/desktop/ms648052%28v=vs.85%29.aspx

    # typedef struct _ICONINFO {
    # BOOL    fIcon;
    # DWORD   xHotspot;
    # DWORD   yHotspot;
    # HBITMAP hbmMask;
    # HBITMAP hbmColor;
    # } ICONINFO, *PICONINFO;

my implementation is

    class ICONINFO(Structure):
        __fields__ = [
                      ('fIcon',     BOOL),
                      ('xHotspot',  DWORD),
                      ('yHotspot',  DWORD),
                      ('hbmMask',   HBITMAP),
                      ('hbmColor',  HBITMAP),
                     ]
                     
#### not part of the problem but needed to get the icon handle    
    hicon = ImageList_GetIcon(def_il_handle,1,ILD_NORMAL)
    print hicon
####

As the documentation states, the function run successful if return code is
none zero. Well I get 1 returned but as soon as I try to access a class member
the program crashes.
    
    iconinfo = ICONINFO()
    lres =  GetIconInfo(hicon, pointer(iconinfo))
    print lres
    print '{0}'.format(sizeof(iconinfo))   # <- crash

If I comment the print of sizeof... the program keeps running but if I call
the same code a second time then it crashes at GetIconInfo(hicon, ...)

So it looks like I'm doing something terribly wrong but don't see it.

Can someone shed some light on it?

Thank you
Hubert



More information about the Python-list mailing list