1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * ReactOS Explorer
 *
 * Copyright 2016 Sylvain Deverre <deverre dot sylv at gmail dot com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * Wraps the component categories manager enum
 */

#include "shellbars.h"

#define REGPATH L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Discardable\\PostSetup\\Component Categories"
#define IMPLEMENTING L"Implementing"
#define REQUIRING L"Requiring"

typedef struct categoryCacheHeader
{
    DWORD               dwSize;         // size of header only
    DWORD               version;        // currently 1
    SYSTEMTIME          writeTime;      // time we were written to registry
    DWORD               classCount;     // number of classes following
} CATCACHEHDR, *PCATCACHEHDR;

/*
 * This class manages a cached explorer component categories items, writing cache if it
 * doesn't exist yet.
 * It is used by CSHEnumClassesOfCategories internally.
 */
class CComCatCachedCategory
{
    public:
        CComCatCachedCategory();
        virtual ~CComCatCachedCategory();
        HRESULT WriteCacheToDSA(HDSA pDest);
        HRESULT STDMETHODCALLTYPE Initialize(CATID &catID, BOOL reloadCache);
    private:
        BOOL LoadFromRegistry();
        HRESULT LoadFromComCatMgr();
        HRESULT CacheDSA();
        CATID fCategory;
        HDSA fLocalDsa;
};

CComCatCachedCategory::CComCatCachedCategory()
{
    fLocalDsa = DSA_Create(sizeof(GUID), 5);
}

HRESULT STDMETHODCALLTYPE CComCatCachedCategory::Initialize(CATID &catID, BOOL reloadCache)<--- Parameter 'catID' can be declared with const
{
    HRESULT hr;

    fCategory = catID;
    if (reloadCache || !LoadFromRegistry())
    {
        hr = LoadFromComCatMgr();
        if (FAILED_UNEXPECTEDLY(hr))
            return hr;

        hr = CacheDSA();
        if (FAILED_UNEXPECTEDLY(hr))
            return hr;
    }
    return S_OK;
}

CComCatCachedCategory::~CComCatCachedCategory()
{
    DSA_Destroy(fLocalDsa);
}

BOOL CComCatCachedCategory::LoadFromRegistry()
{
    WCHAR bufKey[MAX_PATH];
    WCHAR guidStr[MAX_PATH];
    DWORD dataSize, i;
    CComHeapPtr<CATCACHEHDR> buffer;
    GUID *guidArray;

    if (!fLocalDsa)
        return FALSE;

    dataSize = 0;
    if (!StringFromGUID2(fCategory, guidStr, MAX_PATH))
        return FALSE;

    wsprintf(bufKey, L"%s\\%s\\%s", REGPATH , guidStr, L"Enum");

    // Try to read key and get proper value size
    if (SHGetValue(HKEY_CURRENT_USER, bufKey, IMPLEMENTING, NULL, NULL, &dataSize))
        return FALSE;

    buffer.Attach((PCATCACHEHDR)CoTaskMemAlloc(dataSize));

    SHGetValue(HKEY_CURRENT_USER, bufKey, IMPLEMENTING, NULL, buffer, &dataSize);
    guidArray = (GUID*)(buffer + 1);
    for (i = 0; i < buffer->classCount; i++)
    {
        // Add class to cache
        DSA_InsertItem(fLocalDsa, DSA_APPEND, guidArray + i);
    }

    return TRUE;
}

HRESULT CComCatCachedCategory::CacheDSA()
{
    WCHAR                               bufKey[MAX_PATH];
    WCHAR                               guidStr[MAX_PATH];
    UINT                                elemCount;
    UINT                                i;
    UINT                                bufferSize;
    CComHeapPtr<CATCACHEHDR>            buffer;
    GUID                                *guidArray;
    GUID                                *tmp;

    elemCount = DSA_GetItemCount(fLocalDsa);
    bufferSize = sizeof(CATCACHEHDR) + elemCount * sizeof(GUID);
    if (!StringFromGUID2(fCategory, guidStr, MAX_PATH))
        return E_FAIL;

    buffer.Attach((PCATCACHEHDR)CoTaskMemAlloc(bufferSize));
    if (!buffer)
        return E_OUTOFMEMORY;

    // Correctly fill cache header
    buffer->dwSize = sizeof(CATCACHEHDR);
    buffer->version = 1;
    GetSystemTime(&buffer->writeTime);
    buffer->classCount = (DWORD)elemCount;

    guidArray = (GUID*)(buffer + 1);
    wsprintf(bufKey, L"%s\\%s\\%s", REGPATH , guidStr, L"Enum");

    // Write DSA contents inside the memory buffer allocated
    for(i = 0; i < elemCount; i++)
    {
        tmp = (GUID*)DSA_GetItemPtr(fLocalDsa, i);
        if (tmp)
        {
            guidArray[i] = *tmp;
        }
    }

    // Save items to registry
    SHSetValue(HKEY_CURRENT_USER, bufKey, IMPLEMENTING, REG_BINARY, buffer, bufferSize);

    guidArray = NULL;
    return S_OK;
}

HRESULT CComCatCachedCategory::LoadFromComCatMgr()
{
    HRESULT hr;
    CComPtr<ICatInformation> pCatInformation;
    CComPtr<IEnumGUID> pEnumGUID;
    ULONG pFetched;
    CLSID tmp;

    // Get component categories manager instance
    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER,
        IID_PPV_ARG(ICatInformation, &pCatInformation));
    if (FAILED_UNEXPECTEDLY(hr))
        return hr;

    // Get the proper enumerator
    hr = pCatInformation->EnumClassesOfCategories(1, &fCategory, NULL, NULL, &pEnumGUID);
    if (FAILED_UNEXPECTEDLY(hr))
        return hr;

    // Enumerate elements
    do
    {
        pFetched = 0;
        pEnumGUID->Next(1, &tmp, &pFetched);
        if (pFetched)
        {
            if (DSA_InsertItem(fLocalDsa, DSA_APPEND, &tmp) == E_OUTOFMEMORY)
                return E_OUTOFMEMORY;
        }
    }
    while (pFetched > 0);
    return S_OK;
}

HRESULT CComCatCachedCategory::WriteCacheToDSA(HDSA pDest)
{
    INT i;
    for(i = 0; i < DSA_GetItemCount(fLocalDsa); i++)
    {
        if (DSA_InsertItem(pDest, DSA_APPEND, DSA_GetItemPtr(fLocalDsa, i)) == DSA_ERR)
            return E_OUTOFMEMORY;
    }
    return S_OK;
}

class CSHEnumClassesOfCategories :
    public CComCoClass<CSHEnumClassesOfCategories>,
    public CComObjectRootEx<CComMultiThreadModelNoCS>,
    public IEnumGUID
{
    private:
        CComPtr<ICatInformation> fCatInformation;
        HDSA fDsa;
        ULONG fCursor;

    public:
        CSHEnumClassesOfCategories();
        virtual ~CSHEnumClassesOfCategories();
        virtual HRESULT STDMETHODCALLTYPE Initialize(ULONG cImplemented, CATID *pImplemented, ULONG cRequired, CATID *pRequired);
        // *** IEnumGUID methods ***
        virtual HRESULT STDMETHODCALLTYPE Clone(IEnumCLSID **ppvOut);
        virtual HRESULT STDMETHODCALLTYPE Next(ULONG cElt, CLSID *pElts, ULONG *pFetched);
        virtual HRESULT STDMETHODCALLTYPE Reset();
        virtual HRESULT STDMETHODCALLTYPE Skip(ULONG nbElts);

        BEGIN_COM_MAP(CSHEnumClassesOfCategories)
            COM_INTERFACE_ENTRY_IID(IID_IEnumGUID, IEnumGUID)
        END_COM_MAP()
};

CSHEnumClassesOfCategories::CSHEnumClassesOfCategories()
{
    fCursor = 0;
    fDsa = DSA_Create(sizeof(GUID), 5);
}

CSHEnumClassesOfCategories::~CSHEnumClassesOfCategories()
{
    if (fDsa)
        DSA_Destroy(fDsa);
}

HRESULT CSHEnumClassesOfCategories::Initialize(ULONG cImplemented, CATID *pImplemented, ULONG cRequired, CATID *pRequired)
{
    UINT i;
    HRESULT hr;

    if (!fDsa)
        return E_FAIL;

    // Parameter validation:
    // - We must have at least one category to manage.
    // - The array pointers must not be NULL if there is a non-zero
    //   element count specified for them.
    if (cImplemented == 0 && cRequired == 0)
        return E_INVALIDARG;
    if ((cImplemented && !pImplemented) || (cRequired && !pRequired))
        return E_INVALIDARG;

    // For each implemented category, create a cache and add it to our local DSA.
    for (i = 0; i < cImplemented; i++)
    {
        CComCatCachedCategory cachedCat;
        hr = cachedCat.Initialize(pImplemented[i], FALSE);
        if (FAILED_UNEXPECTEDLY(hr))
            return hr;
        cachedCat.WriteCacheToDSA(fDsa);
    }

    // TODO: Implement caching of the required categories.
    if (cRequired > 0)
    {
        FIXME("Implement required categories class enumeration\n");

        // Only fail in case we didn't look at the implemented categories.
        if (cImplemented == 0)
            return E_NOTIMPL;
    }

    return S_OK;
}

// *** IEnumGUID methods ***

HRESULT STDMETHODCALLTYPE CSHEnumClassesOfCategories::Clone(IEnumCLSID **ppvOut)
{
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE CSHEnumClassesOfCategories::Next(ULONG cElt, CLSID *pElts, ULONG *pFetched)
{
    ULONG i;
    ULONG read;
    GUID *tmp;

    if (!pElts)
        return E_INVALIDARG;
    read = 0;
    for (i = 0; i < cElt && (fCursor < (ULONG)DSA_GetItemCount(fDsa)); i++)
    {
        tmp = (GUID*)DSA_GetItemPtr(fDsa, fCursor + i);
        if (!tmp)
            break;
        pElts[i] = *tmp;
        read++;
    }
    fCursor += read;
    if (pFetched)
        *pFetched = read;
    return S_OK;
}

HRESULT STDMETHODCALLTYPE CSHEnumClassesOfCategories::Reset()
{
    fCursor = 0;
    return S_OK;
}

HRESULT STDMETHODCALLTYPE CSHEnumClassesOfCategories::Skip(ULONG nbElts)
{
    if (fCursor + nbElts >= (ULONG)DSA_GetItemCount(fDsa))
        return E_INVALIDARG;
    fCursor += nbElts;
    return S_OK;
}

/*************************************************************************
 * SHEnumClassesOfCategories	[BROWSEUI.136]
 */
extern "C" HRESULT WINAPI SHEnumClassesOfCategories(ULONG cImplemented, CATID *pImplemented, ULONG cRequired, CATID *pRequired, IEnumGUID **out)
{
    HRESULT hr;

    if (!out)
        return E_INVALIDARG;

    hr = ShellObjectCreatorInit<CSHEnumClassesOfCategories>(
            cImplemented, pImplemented, cRequired, pRequired, IID_PPV_ARG(IEnumGUID, out));
    if (FAILED_UNEXPECTEDLY(hr))
        return hr;
    return S_OK;
}