xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #678: shell32.pyx

File shell32.pyx, 3.4 KB (added by Antoine Martin, 4 years ago)

failed attempt at implementing IPropertyStore access using a C++ cython module

Line 
1# This file is part of Xpra.
2# Copyright (C) 2017 Antoine Martin <antoine@devloop.org.uk>
3# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
4# later version. See the file COPYING for details.
5
6import struct
7from xpra.log import Logger
8log = Logger("win32")
9
10
11cdef extern from "windows.h":
12    ctypedef long HRESULT
13    ctypedef HRESULT SHSTDAPI
14    ctypedef void *PVOID
15    ctypedef PVOID HANDLE
16    ctypedef HANDLE HWND
17   
18    ctypedef Py_UNICODE WCHAR
19    ctypedef unsigned long ULONG
20    ctypedef unsigned long DWORD
21    ctypedef int WORD
22    ctypedef const WCHAR* LPCOLESTR
23    ctypedef const WCHAR* PCWSTR
24    ctypedef unsigned short VARTYPE
25
26cdef extern from "guiddef.h":
27    ctypedef struct GUID:
28        pass
29
30cdef extern from "wtypesbase.h":
31    ctypedef GUID IID
32    ctypedef IID* REFIID
33    ctypedef void* LPCLSID
34
35cdef extern from "shlobj.h":
36    SHSTDAPI SHCLSIDFromString(LPCOLESTR lpsz, LPCLSID pclsid)
37
38cdef extern from "shobjidl.h":
39    GUID IID_IShellFolder
40    #DEFINE_GUID (IID_IShellFolder, __MSABI_LONG(0x000214e6), 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 0x46);
41cdef extern from "propidl.h":
42    ctypedef struct PROPVARIANT:
43        VARTYPE vt
44        WORD    wReserved1
45        WORD    wReserved2
46        WORD    wReserved3
47    void PropVariantInit(PROPVARIANT *pvar)
48    HRESULT PropVariantClear(PROPVARIANT *pvar)
49    ctypedef PROPVARIANT* REFPROPVARIANT
50
51cdef extern from "Wtypes.h":
52    ctypedef struct PROPERTYKEY:
53        GUID  fmtid
54        DWORD pid
55    ctypedef PROPERTYKEY* REFPROPERTYKEY
56
57cdef extern from "propkey.h":
58    pass
59
60cdef extern from "propvarutil.h":
61    HRESULT InitPropVariantFromString(PCWSTR psz,PROPVARIANT *ppropvar)
62
63cdef extern from "shellapi.h":
64    SHSTDAPI SHGetPropertyStoreForWindow(HWND hwnd, REFIID riid, void **ppv)
65
66cdef extern from "propsys.h":
67    ctypedef struct IPropertyStore:
68        HRESULT Commit()
69        HRESULT GetAt(DWORD iProp, PROPERTYKEY pkey)
70        HRESULT GetCount(DWORD *cProps)
71        HRESULT GetValue(REFPROPERTYKEY key, PROPVARIANT *pv)
72        HRESULT SetValue(REFPROPERTYKEY key, REFPROPVARIANT pv)
73        #from IUnknown
74        ULONG AddRef()
75        HRESULT QueryInterface(REFIID riid, void **ppvObject)
76        ULONG Release()
77
78def set_window_group(hwnd, value):
79    log("set_window_group(%i, %s)", hwnd, value)
80    cdef IPropertyStore *pps
81    cdef unsigned long tmp = <unsigned long> hwnd
82    cdef HWND hWnd = <HWND> tmp
83    #cdef GUID IID_IShellFolder
84    #cdef GUID IID_IShellFolder
85    #u16 = "{000214E6-0000-0000-C000-000000000046}".encode("utf16")
86    #SHCLSIDFromString(u16, &IID_IShellFolder)   
87    #CLSIDFromString(u'{56FDF344-FD6D-11d0-958A-006097C9A090}', &IID_IShellFolder)
88    cdef IID iid = IID_IShellFolder
89    cdef HRESULT hr = SHGetPropertyStoreForWindow(hWnd, &iid, <void **> &pps)
90    if hr:
91        log.warn("failed to get the window property store, error code %i", hr)
92        return
93    cdef PROPVARIANT pv
94    cdef HRESULT r
95    if value:
96        r = InitPropVariantFromString(str(value), &pv)
97        if r:
98            log.warn("failed to init prop variant from value '%s', error code %i", value, r)
99            return
100    else:
101        # VT_EMPTY, which removes the property from the window, if present:
102        PropVariantInit(&pv)
103    cdef PROPERTYKEY PKEY_AppUserModel_ID
104    u16 = "{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"
105    SHCLSIDFromString(u16, &PKEY_AppUserModel_ID.fmtid)
106    r = pps.SetValue(&PKEY_AppUserModel_ID, &pv)
107    PropVariantClear(&pv)
108    pps.Release()