1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 
7 #ifndef SPHelper_h
8 #define SPHelper_h
9 
10 #include <winapifamily.h>
11 
12 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
13 
14 #include <malloc.h>
15 #include <sapi.h>
16 /* #include <sapiddk.h> */
17 #include <sperror.h>
18 #include <limits.h>
19 #include <mmsystem.h>
20 #include <comcat.h>
21 #include <mmreg.h>
22 /* #include <atlbase.h> */
23 #include <wchar.h>
24 #include <tchar.h>
25 #include <strsafe.h>
26 #include <intsafe.h>
27 
28 inline HRESULT SpGetCategoryFromId(const WCHAR *category_id, ISpObjectTokenCategory **ret, BOOL fCreateIfNotExist = FALSE) {
29     ISpObjectTokenCategory *obj_token_cat;
30     HRESULT hres;
31 
32     hres = ::CoCreateInstance(CLSID_SpObjectTokenCategory, NULL, CLSCTX_ALL, __uuidof(ISpObjectTokenCategory),
33             (void**)&obj_token_cat);
34     if(FAILED(hres))
35         return hres;
36 
37     hres = obj_token_cat->SetId(category_id, fCreateIfNotExist);
38     if(FAILED(hres)) {
39         obj_token_cat->Release();
40         return hres;
41     }
42 
43     *ret = obj_token_cat;
44     return S_OK;
45 }
46 
SpEnumTokens(const WCHAR * category_id,const WCHAR * req_attrs,const WCHAR * opt_attrs,IEnumSpObjectTokens ** ret)47 inline HRESULT SpEnumTokens(const WCHAR *category_id, const WCHAR *req_attrs, const WCHAR *opt_attrs, IEnumSpObjectTokens **ret) {
48     ISpObjectTokenCategory *category;
49     HRESULT hres;
50 
51     hres = SpGetCategoryFromId(category_id, &category);
52     if(SUCCEEDED(hres)) {
53         hres = category->EnumTokens(req_attrs, opt_attrs, ret);
54         category->Release();
55     }
56 
57     return hres;
58 }
59 
60 /* str must be at least 9 chars (8 for 32-bit integer in hex + one for '\0').  */
SpHexFromUlong(WCHAR * str,ULONG ul)61 inline void SpHexFromUlong(WCHAR *str, ULONG ul) {
62     ::_ultow(ul, str, 16);
63 }
64 
65 inline HRESULT SpGetDescription(ISpObjectToken *obj_token, WCHAR **description, LANGID language = GetUserDefaultUILanguage()) {
66     WCHAR lang_id[9];
67     HRESULT hres;
68 
69     SpHexFromUlong(lang_id, language);
70     hres = obj_token->GetStringValue(lang_id, description);
71     if(hres == SPERR_NOT_FOUND)
72         hres = obj_token->GetStringValue(NULL, description);
73 
74     return hres;
75 }
76 
77 #endif
78 #endif
79