dmoreg.c 24.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2003 Michael Günnewig
3
 * Copyright (C) 2003 CodeWeavers Inc. (Ulrich Czekalla)
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19
 */

20 21
#include <stdarg.h>

22 23
#define COBJMACROS

24
#include "windef.h"
25
#include "winbase.h"
26 27 28
#include "winuser.h"
#include "winerror.h"
#include "winreg.h"
29
#include "objbase.h"
30
#include "wine/unicode.h"
31
#include "wine/debug.h"
32
#include "dmo.h"
33 34 35

WINE_DEFAULT_DEBUG_CHANNEL(msdmo);

36
#define MSDMO_MAJOR_VERSION 6
37

38
static const WCHAR szDMORootKey[] = 
39
{
40 41 42
    'D','i','r','e','c','t','S','h','o','w','\\',
    'M','e','d','i','a','O','b','j','e','c','t','s',0
}; 
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
static const WCHAR szDMOInputType[] =
{
    'I','n','p','u','t','T','y','p','e','s',0
};

static const WCHAR szDMOOutputType[] =
{
    'O','u','t','p','u','t','T','y','p','e','s',0
};

static const WCHAR szDMOKeyed[] =
{
    'K','e','y','e','d',0
};

static const WCHAR szDMOCategories[] =
{
    'C','a','t','e','g','o','r','i','e','s',0
};

static const WCHAR szGUIDFmt[] =
{
    '%','0','8','X','-','%','0','4','X','-','%','0','4','X','-','%','0',
    '2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2',
    'X','%','0','2','X','%','0','2','X','%','0','2','X',0
};

static const WCHAR szCat3Fmt[] =
{
    '%','s','\\','%','s','\\','%','s',0
};

static const WCHAR szCat2Fmt[] =
{
    '%','s','\\','%','s',0
};

81 82 83 84 85 86
static const WCHAR szToGuidFmt[] =
{
    '{','%','s','}',0
};


87 88
typedef struct
{
89
    IEnumDMO                    IEnumDMO_iface;
90
    LONG			ref;
91
    DWORD			index;
92
    GUID                        category;
93 94 95 96 97 98 99 100
    DWORD                       dwFlags;
    DWORD                       cInTypes;
    DMO_PARTIAL_MEDIATYPE       *pInTypes;
    DWORD                       cOutTypes;
    DMO_PARTIAL_MEDIATYPE       *pOutTypes;
    HKEY                        hkey;
} IEnumDMOImpl;

101 102 103 104 105
static inline IEnumDMOImpl *impl_from_IEnumDMO(IEnumDMO *iface)
{
    return CONTAINING_RECORD(iface, IEnumDMOImpl, IEnumDMO_iface);
}

106 107
static HRESULT read_types(HKEY root, LPCWSTR key, ULONG *supplied, ULONG requested, DMO_PARTIAL_MEDIATYPE* types);

108
static const IEnumDMOVtbl edmovt;
109 110 111 112 113 114 115 116 117

static LPWSTR GUIDToString(LPWSTR lpwstr, REFGUID lpcguid)
{
    wsprintfW(lpwstr, szGUIDFmt, lpcguid->Data1, lpcguid->Data2,
        lpcguid->Data3, lpcguid->Data4[0], lpcguid->Data4[1],
        lpcguid->Data4[2], lpcguid->Data4[3], lpcguid->Data4[4],
        lpcguid->Data4[5], lpcguid->Data4[6], lpcguid->Data4[7]);

    return lpwstr;
118 119
}

120 121 122 123 124 125 126 127 128 129
static HRESULT string_to_guid(const WCHAR *string, GUID *guid)
{
    WCHAR buffer[39];
    buffer[0] = '{';
    strcpyW(buffer + 1, string);
    buffer[37] = '}';
    buffer[38] = 0;
    return CLSIDFromString(buffer, guid);
}

130
static BOOL IsMediaTypeEqual(const DMO_PARTIAL_MEDIATYPE* mt1, const DMO_PARTIAL_MEDIATYPE* mt2)
131 132
{

133 134 135 136 137 138
    return (IsEqualCLSID(&mt1->type, &mt2->type) ||
            IsEqualCLSID(&mt2->type, &GUID_NULL) ||
            IsEqualCLSID(&mt1->type, &GUID_NULL)) &&
            (IsEqualCLSID(&mt1->subtype, &mt2->subtype) ||
            IsEqualCLSID(&mt2->subtype, &GUID_NULL) ||
            IsEqualCLSID(&mt1->subtype, &GUID_NULL));
139 140
}

141 142
static HRESULT write_types(HKEY hkey, LPCWSTR name, const DMO_PARTIAL_MEDIATYPE* types, DWORD count)
{
143 144
    LONG ret;

145 146
    if (MSDMO_MAJOR_VERSION > 5)
    {
147
        ret = RegSetValueExW(hkey, name, 0, REG_BINARY, (const BYTE*) types,
148 149 150 151 152 153 154 155
                          count* sizeof(DMO_PARTIAL_MEDIATYPE));
    }
    else
    {
        HKEY skey1,skey2,skey3;
        DWORD index = 0;
        WCHAR szGuidKey[64];

156
        ret = RegCreateKeyExW(hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
157
                               KEY_WRITE, NULL, &skey1, NULL);
158 159 160
        if (ret)
            return HRESULT_FROM_WIN32(ret);

161 162 163
        while (index < count)
        {
            GUIDToString(szGuidKey,&types[index].type);
164
            ret = RegCreateKeyExW(skey1, szGuidKey, 0, NULL,
165 166
                        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &skey2, NULL);
            GUIDToString(szGuidKey,&types[index].subtype);
167
            ret = RegCreateKeyExW(skey2, szGuidKey, 0, NULL,
168 169 170 171 172 173 174 175
                        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &skey3, NULL);
            RegCloseKey(skey3);
            RegCloseKey(skey2);
            index ++;
        }
        RegCloseKey(skey1);
    }

176
    return HRESULT_FROM_WIN32(ret);
177 178
}

179
/***************************************************************
180
 * DMORegister (MSDMO.@)
181 182 183 184 185 186 187 188 189 190 191 192 193
 *
 * Register a DirectX Media Object.
 */
HRESULT WINAPI DMORegister(
   LPCWSTR szName,
   REFCLSID clsidDMO,
   REFGUID guidCategory,
   DWORD dwFlags,
   DWORD cInTypes,
   const DMO_PARTIAL_MEDIATYPE *pInTypes,
   DWORD cOutTypes,
   const DMO_PARTIAL_MEDIATYPE *pOutTypes
)
194
{
195 196 197 198 199 200
    WCHAR szguid[64];
    HRESULT hres;
    HKEY hrkey = 0;
    HKEY hkey = 0;
    HKEY hckey = 0;
    HKEY hclskey = 0;
201
    LONG ret;
202

203 204 205 206
    TRACE("%s %s %s\n", debugstr_w(szName), debugstr_guid(clsidDMO), debugstr_guid(guidCategory));

    if (IsEqualGUID(guidCategory, &GUID_NULL))
        return E_INVALIDARG;
207

208
    ret = RegCreateKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0, NULL,
209
        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hrkey, NULL);
210
    if (ret)
211
        return E_FAIL;
212

213
    /* Create clsidDMO key under MediaObjects */ 
214
    ret = RegCreateKeyExW(hrkey, GUIDToString(szguid, clsidDMO), 0, NULL,
215
        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, NULL);
216
    if (ret)
217 218 219
        goto lend;

    /* Set default Name value */
220
    ret = RegSetValueExW(hkey, NULL, 0, REG_SZ, (const BYTE*) szName,
221 222
        (strlenW(szName) + 1) * sizeof(WCHAR));

223
    /* Set InputTypes */
224 225
    hres = write_types(hkey, szDMOInputType, pInTypes, cInTypes);

226
    /* Set OutputTypes */
227
    hres = write_types(hkey, szDMOOutputType, pOutTypes, cOutTypes);
228 229 230 231

    if (dwFlags & DMO_REGISTERF_IS_KEYED)
    {
        /* Create Keyed key */ 
232
        ret = RegCreateKeyExW(hkey, szDMOKeyed, 0, NULL,
233
            REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hckey, NULL);
234
        if (ret)
235 236 237 238 239
            goto lend;
        RegCloseKey(hckey);
    }

    /* Register the category */
240
    ret = RegCreateKeyExW(hrkey, szDMOCategories, 0, NULL,
241
            REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hckey, NULL);
242
    if (ret)
243 244 245 246
        goto lend;

    RegCloseKey(hkey);

247
    ret = RegCreateKeyExW(hckey, GUIDToString(szguid, guidCategory), 0, NULL,
248
            REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, NULL);
249
    if (ret)
250
        goto lend;
251
    ret = RegCreateKeyExW(hkey, GUIDToString(szguid, clsidDMO), 0, NULL,
252
        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hclskey, NULL);
253
    if (ret)
254 255 256 257 258 259 260 261 262 263 264 265
        goto lend;

lend:
    if (hkey)
        RegCloseKey(hkey);
    if (hckey)
        RegCloseKey(hckey);
    if (hclskey)
        RegCloseKey(hclskey);
    if (hrkey)
        RegCloseKey(hrkey);

266
    hres = HRESULT_FROM_WIN32(ret);
267
    TRACE(" hresult=0x%08x\n", hres);
268
    return hres;
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284
static HRESULT unregister_dmo_from_category(const WCHAR *dmoW, const WCHAR *catW, HKEY categories)
{
    HKEY catkey;
    LONG ret;

    ret = RegOpenKeyExW(categories, catW, 0, KEY_WRITE, &catkey);
    if (!ret)
    {
        ret = RegDeleteKeyW(catkey, dmoW);
        RegCloseKey(catkey);
    }

    return !ret ? S_OK : S_FALSE;
}
285 286

/***************************************************************
287
 * DMOUnregister (MSDMO.@)
288 289 290
 *
 * Unregister a DirectX Media Object.
 */
291
HRESULT WINAPI DMOUnregister(REFCLSID dmo, REFGUID category)
292
{
293 294 295
    HKEY rootkey = 0, categorieskey = 0;
    WCHAR dmoW[64], catW[64];
    HRESULT hr = S_FALSE;
296
    LONG ret;
297

298
    TRACE("%s %s\n", debugstr_guid(dmo), debugstr_guid(category));
299

300 301
    ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0, KEY_WRITE, &rootkey);
    if (ret)
302
        return S_FALSE;
303

304 305
    GUIDToString(dmoW, dmo);
    RegDeleteKeyW(rootkey, dmoW);
306

307 308 309 310 311 312
    /* open 'Categories' */
    ret = RegOpenKeyExW(rootkey, szDMOCategories, 0, KEY_WRITE|KEY_ENUMERATE_SUB_KEYS, &categorieskey);
    RegCloseKey(rootkey);
    if (ret)
    {
        hr = HRESULT_FROM_WIN32(ret);
313
        goto lend;
314
    }
315

316 317 318
    /* remove from all categories */
    if (IsEqualGUID(category, &GUID_NULL))
    {
319
        DWORD index = 0, len = ARRAY_SIZE(catW);
320

321 322 323 324 325 326 327 328
        while (!RegEnumKeyExW(categorieskey, index++, catW, &len, NULL, NULL, NULL, NULL))
            hr = unregister_dmo_from_category(dmoW, catW, categorieskey);
    }
    else
    {
        GUIDToString(catW, category);
        hr = unregister_dmo_from_category(dmoW, catW, categorieskey);
    }
329 330

lend:
331 332
    if (categorieskey)
        RegCloseKey(categorieskey);
333

334
    return hr;
335 336 337 338
}


/***************************************************************
339
 * DMOGetName (MSDMO.@)
340
 *
341
 * Get DMO Name from the registry
342
 */
343
HRESULT WINAPI DMOGetName(REFCLSID clsidDMO, WCHAR name[])
344
{
345 346
    static const INT max_name_len = 80*sizeof(WCHAR);
    DWORD count = max_name_len;
347
    WCHAR szguid[64];
348
    HKEY hrkey, hkey;
349
    LONG ret;
350

351
    TRACE("%s %p\n", debugstr_guid(clsidDMO), name);
352

353 354
    if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0, KEY_READ, &hrkey))
        return E_FAIL;
355

356
    ret = RegOpenKeyExW(hrkey, GUIDToString(szguid, clsidDMO), 0, KEY_READ, &hkey);
357 358 359
    RegCloseKey(hrkey);
    if (ret)
        return E_FAIL;
360

361 362
    ret = RegQueryValueExW(hkey, NULL, NULL, NULL, (LPBYTE)name, &count);
    RegCloseKey(hkey);
363

364 365 366 367 368
    if (!ret && count > 1)
    {
        TRACE("name=%s\n", debugstr_w(name));
        return S_OK;
    }
369

370 371
    name[0] = 0;
    return S_FALSE;
372 373
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
static HRESULT dup_partial_mediatype(const DMO_PARTIAL_MEDIATYPE *types, DWORD count, DMO_PARTIAL_MEDIATYPE **ret)
{
    *ret = NULL;

    if (count == 0)
        return S_OK;

    *ret = HeapAlloc(GetProcessHeap(), 0, count*sizeof(*types));
    if (!*ret)
        return E_OUTOFMEMORY;

    memcpy(*ret, types, count*sizeof(*types));
    return S_OK;
}

389 390 391
/**************************************************************************
 *  IEnumDMO_Constructor
 */
392
static HRESULT IEnumDMO_Constructor(
393 394 395 396 397
    REFGUID guidCategory,
    DWORD dwFlags,
    DWORD cInTypes,
    const DMO_PARTIAL_MEDIATYPE *pInTypes,
    DWORD cOutTypes,
398 399
    const DMO_PARTIAL_MEDIATYPE *pOutTypes,
    IEnumDMO **obj)
400 401
{
    IEnumDMOImpl* lpedmo;
402
    HRESULT hr;
403 404

    *obj = NULL;
405

406
    lpedmo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumDMOImpl));
407 408
    if (!lpedmo)
        return E_OUTOFMEMORY;
409

410 411 412
    lpedmo->IEnumDMO_iface.lpVtbl = &edmovt;
    lpedmo->ref = 1;
    lpedmo->index = -1;
413
    lpedmo->category = *guidCategory;
414
    lpedmo->dwFlags = dwFlags;
415 416
    lpedmo->cInTypes = cInTypes;
    lpedmo->cOutTypes = cOutTypes;
417
    lpedmo->hkey = NULL;
418

419 420 421
    hr = dup_partial_mediatype(pInTypes, cInTypes, &lpedmo->pInTypes);
    if (FAILED(hr))
        goto lerr;
422

423 424 425
    hr = dup_partial_mediatype(pOutTypes, cOutTypes, &lpedmo->pOutTypes);
    if (FAILED(hr))
        goto lerr;
426

427 428 429
    /* If not filtering by category enum from media objects root */
    if (IsEqualGUID(guidCategory, &GUID_NULL))
    {
430
        RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0, KEY_READ, &lpedmo->hkey);
431 432 433 434 435
    }
    else
    {
        WCHAR szguid[64];
        WCHAR szKey[MAX_PATH];
436

437
        wsprintfW(szKey, szCat3Fmt, szDMORootKey, szDMOCategories, GUIDToString(szguid, guidCategory));
438
        RegOpenKeyExW(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &lpedmo->hkey);
439 440
    }

441
lerr:
442

443
    if (FAILED(hr))
444
        IEnumDMO_Release(&lpedmo->IEnumDMO_iface);
445 446 447 448 449 450 451
    else
    {
        TRACE("returning %p\n", lpedmo);
        *obj = &lpedmo->IEnumDMO_iface;
    }

    return hr;
452 453 454 455 456 457 458
}

/******************************************************************************
 * IEnumDMO_fnAddRef
 */
static ULONG WINAPI IEnumDMO_fnAddRef(IEnumDMO * iface)
{
459
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);
460 461 462
    ULONG refCount = InterlockedIncrement(&This->ref);
    TRACE("(%p)->(%d)\n", This, refCount);
    return refCount;
463 464 465 466 467
}

/**************************************************************************
 *  EnumDMO_QueryInterface
 */
468
static HRESULT WINAPI IEnumDMO_fnQueryInterface(IEnumDMO* iface, REFIID riid, void **ppvObj)
469
{
470 471 472 473
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObj);

474 475
    *ppvObj = NULL;

476 477 478 479
    if (IsEqualIID(riid, &IID_IEnumDMO) ||
        IsEqualIID(riid, &IID_IUnknown))
    {
        *ppvObj = iface;
480
        IEnumDMO_AddRef(iface);
481
    }
482

483
    return *ppvObj ? S_OK : E_NOINTERFACE;
484 485 486 487 488 489 490
}

/******************************************************************************
 * IEnumDMO_fnRelease
 */
static ULONG WINAPI IEnumDMO_fnRelease(IEnumDMO * iface)
{
491
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);
492
    ULONG refCount = InterlockedDecrement(&This->ref);
493

494 495
    TRACE("(%p)->(%d)\n", This, refCount);

496
    if (!refCount)
497
    {
498 499 500 501 502
        if (This->hkey)
            RegCloseKey(This->hkey);
        HeapFree(GetProcessHeap(), 0, This->pInTypes);
        HeapFree(GetProcessHeap(), 0, This->pOutTypes);
        HeapFree(GetProcessHeap(), 0, This);
503
    }
504
    return refCount;
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
}


/******************************************************************************
 * IEnumDMO_fnNext
 */
static HRESULT WINAPI IEnumDMO_fnNext(
    IEnumDMO * iface, 
    DWORD cItemsToFetch,
    CLSID * pCLSID,
    WCHAR ** Names,
    DWORD * pcItemsFetched)
{
    HKEY hkey;
    WCHAR szNextKey[MAX_PATH];
520
    WCHAR szGuidKey[64];
521 522 523 524 525
    WCHAR szKey[MAX_PATH];
    WCHAR szValue[MAX_PATH];
    DWORD len;
    UINT count = 0;
    HRESULT hres = S_OK;
526
    LONG ret;
527
    GUID guid;
528

529
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);
530

531
    TRACE("(%p)->(%d %p %p %p)\n", This, cItemsToFetch, pCLSID, Names, pcItemsFetched);
532

533
    if (!pCLSID)
534 535
        return E_POINTER;

536 537 538
    if (!pcItemsFetched && cItemsToFetch > 1)
        return E_INVALIDARG;

539
    while (count < cItemsToFetch)
540 541 542
    {
        This->index++;

543
        len = MAX_PATH;
544
        ret = RegEnumKeyExW(This->hkey, This->index, szNextKey, &len, NULL, NULL, NULL, NULL);
545 546 547
        if (ret != ERROR_SUCCESS)
        {
            hres = HRESULT_FROM_WIN32(ret);
548
            break;
549
        }
550

551 552 553
        if (string_to_guid(szNextKey, &guid) != S_OK)
            continue;

554 555
        TRACE("found %s\n", debugstr_w(szNextKey));

556
        if (!(This->dwFlags & DMO_ENUMF_INCLUDE_KEYED))
557 558
        {
            wsprintfW(szKey, szCat3Fmt, szDMORootKey, szNextKey, szDMOKeyed);
559 560
            ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &hkey);
            if (ERROR_SUCCESS == ret)
561 562 563
            {
                RegCloseKey(hkey);
                /* Skip Keyed entries */
564
                continue;
565
            }
566 567 568
        }

        wsprintfW(szKey, szCat2Fmt, szDMORootKey, szNextKey);
569
        ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &hkey);
570
        TRACE("testing %s\n", debugstr_w(szKey));
571 572 573

        if (This->pInTypes)
        {
574
            UINT i, j;
575 576 577
            DWORD cInTypes;
            DMO_PARTIAL_MEDIATYPE* pInTypes;

578 579 580 581
            hres = read_types(hkey, szDMOInputType, &cInTypes,
                    sizeof(szValue)/sizeof(DMO_PARTIAL_MEDIATYPE),
                    (DMO_PARTIAL_MEDIATYPE*)szValue);

582
            if (FAILED(hres))
583 584 585 586 587
            {
                RegCloseKey(hkey);
                continue;
            }

588
            pInTypes = (DMO_PARTIAL_MEDIATYPE *)szValue;
589

590 591 592 593 594 595
            TRACE("read %d intypes for %s:\n", cInTypes, debugstr_w(szKey));
            for (i = 0; i < cInTypes; i++) {
                TRACE("intype %d: type %s, subtype %s\n", i, debugstr_guid(&pInTypes[i].type),
                    debugstr_guid(&pInTypes[i].subtype));
            }

596 597 598 599 600
            for (i = 0; i < This->cInTypes; i++)
            {
                for (j = 0; j < cInTypes; j++) 
                {
                    if (IsMediaTypeEqual(&pInTypes[j], &This->pInTypes[i]))
601
                        break;
602 603
                }

604
                if (j >= cInTypes)
605 606 607 608 609 610 611 612 613 614 615 616
                    break;
            }

            if (i < This->cInTypes)
            {
                RegCloseKey(hkey);
                continue;
            }
        }

        if (This->pOutTypes)
        {
617
            UINT i, j;
618 619 620
            DWORD cOutTypes;
            DMO_PARTIAL_MEDIATYPE* pOutTypes;

621 622 623 624
            hres = read_types(hkey, szDMOOutputType, &cOutTypes,
                    sizeof(szValue)/sizeof(DMO_PARTIAL_MEDIATYPE),
                    (DMO_PARTIAL_MEDIATYPE*)szValue);

625
            if (FAILED(hres))
626 627 628 629 630
            {
                RegCloseKey(hkey);
                continue;
            }

631
            pOutTypes = (DMO_PARTIAL_MEDIATYPE *)szValue;
632

633 634 635 636 637 638
            TRACE("read %d outtypes for %s:\n", cOutTypes, debugstr_w(szKey));
            for (i = 0; i < cOutTypes; i++) {
                TRACE("outtype %d: type %s, subtype %s\n", i, debugstr_guid(&pOutTypes[i].type),
                    debugstr_guid(&pOutTypes[i].subtype));
            }

639 640 641 642 643
            for (i = 0; i < This->cOutTypes; i++)
            {
                for (j = 0; j < cOutTypes; j++) 
                {
                    if (IsMediaTypeEqual(&pOutTypes[j], &This->pOutTypes[i]))
644
                        break;
645 646
                }

647
                if (j >= cOutTypes)
648 649 650 651 652 653 654 655 656 657
                    break;
            }

            if (i < This->cOutTypes)
            {
                RegCloseKey(hkey);
                continue;
            }
        }

658 659
        /* Media object wasn't filtered so add it to return list */
        len = MAX_PATH * sizeof(WCHAR);
660
        ret = RegQueryValueExW(hkey, NULL, NULL, NULL, (LPBYTE)szValue, &len);
661
        if (Names)
662
        {
663 664 665 666 667 668 669
            Names[count] = NULL;
            if (ret == ERROR_SUCCESS)
            {
                Names[count] = CoTaskMemAlloc((strlenW(szValue) + 1) * sizeof(WCHAR));
                if (Names[count])
                    strcpyW(Names[count], szValue);
            }
670
        }
671 672
        wsprintfW(szGuidKey,szToGuidFmt,szNextKey);
        CLSIDFromString(szGuidKey, &pCLSID[count]);
673 674 675

        TRACE("found match %s %s\n", debugstr_w(szValue), debugstr_w(szNextKey));
        RegCloseKey(hkey);
676
        count++;
677 678
    }

679 680
    if (pcItemsFetched) *pcItemsFetched = count;
    if (count < cItemsToFetch)
681 682
        hres = S_FALSE;

683
    TRACE("<-- %i found\n",count);
684 685 686 687 688 689 690 691 692
    return hres;
}
 

/******************************************************************************
 * IEnumDMO_fnSkip
 */
static HRESULT WINAPI IEnumDMO_fnSkip(IEnumDMO * iface, DWORD cItemsToSkip)
{
693
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);
694

695
    TRACE("(%p)->(%d)\n", This, cItemsToSkip);
696 697 698 699 700 701 702 703 704 705 706
    This->index += cItemsToSkip;

    return S_OK;
}


/******************************************************************************
 * IEnumDMO_fnReset
 */
static HRESULT WINAPI IEnumDMO_fnReset(IEnumDMO * iface)
{
707
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);
708

709
    TRACE("(%p)\n", This);
710 711 712 713 714 715 716 717 718
    This->index = -1;

    return S_OK;
}
 

/******************************************************************************
 * IEnumDMO_fnClone
 */
719
static HRESULT WINAPI IEnumDMO_fnClone(IEnumDMO *iface, IEnumDMO **ppEnum)
720
{
721
    IEnumDMOImpl *This = impl_from_IEnumDMO(iface);
722
    TRACE("(%p)->(%p)\n", This, ppEnum);
723
    return IEnumDMO_Constructor(&This->category, This->dwFlags, This->cInTypes, This->pInTypes,
724
        This->cOutTypes, This->pOutTypes, ppEnum);
725 726
}

727 728

/***************************************************************
729
 * DMOEnum (MSDMO.@)
730 731 732 733
 *
 * Enumerate DirectX Media Objects in the registry.
 */
HRESULT WINAPI DMOEnum(
734 735
    REFGUID category,
    DWORD flags,
736 737 738 739 740 741
    DWORD cInTypes,
    const DMO_PARTIAL_MEDIATYPE *pInTypes,
    DWORD cOutTypes,
    const DMO_PARTIAL_MEDIATYPE *pOutTypes,
    IEnumDMO **ppEnum)
{
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
    TRACE("%s 0x%08x %d %p %d %p %p\n", debugstr_guid(category), flags, cInTypes, pInTypes,
        cOutTypes, pOutTypes, ppEnum);

    if (TRACE_ON(msdmo))
    {
        DWORD i;
        if (cInTypes)
        {
            for (i = 0; i < cInTypes; i++)
                TRACE("intype %d - type %s, subtype %s\n", i, debugstr_guid(&pInTypes[i].type),
                    debugstr_guid(&pInTypes[i].subtype));
        }

        if (cOutTypes) {
            for (i = 0; i < cOutTypes; i++)
                TRACE("outtype %d - type %s, subtype %s\n", i, debugstr_guid(&pOutTypes[i].type),
                    debugstr_guid(&pOutTypes[i].subtype));
        }
    }
761

762
    return IEnumDMO_Constructor(category, flags, cInTypes,
763
        pInTypes, cOutTypes, pOutTypes, ppEnum);
764 765 766
}


767
static const IEnumDMOVtbl edmovt =
768
{
769 770 771 772 773 774 775 776
	IEnumDMO_fnQueryInterface,
	IEnumDMO_fnAddRef,
	IEnumDMO_fnRelease,
	IEnumDMO_fnNext,
	IEnumDMO_fnSkip,
	IEnumDMO_fnReset,
	IEnumDMO_fnClone,
};
777 778


779
HRESULT read_types(HKEY root, LPCWSTR key, ULONG *supplied, ULONG requested, DMO_PARTIAL_MEDIATYPE* types )
780
{
781
    HRESULT ret = S_OK;
782

783 784 785
    if (MSDMO_MAJOR_VERSION > 5)
    {
        DWORD len;
786 787
        LONG rc;

788
        len = requested * sizeof(DMO_PARTIAL_MEDIATYPE);
789 790 791
        rc = RegQueryValueExW(root, key, NULL, NULL, (LPBYTE) types, &len);
        ret = HRESULT_FROM_WIN32(rc);

792 793 794
        *supplied = len / sizeof(DMO_PARTIAL_MEDIATYPE);
    }
    else
795
    {
796 797 798 799 800
        HKEY hkey;
        WCHAR szGuidKey[64];

        *supplied = 0;
        if (ERROR_SUCCESS == RegOpenKeyExW(root, key, 0, KEY_READ, &hkey))
801
        {
802 803 804 805
          int index = 0;
          WCHAR szNextKey[MAX_PATH];
          DWORD len;
          LONG rc = ERROR_SUCCESS;
806

807
          while (rc == ERROR_SUCCESS)
808
          {
809
            len = MAX_PATH;
810 811
            rc = RegEnumKeyExW(hkey, index, szNextKey, &len, NULL, NULL, NULL, NULL);
            if (rc == ERROR_SUCCESS)
812
            {
813 814 815 816 817 818 819
              HKEY subk;
              int sub_index = 0;
              LONG rcs = ERROR_SUCCESS;
              WCHAR szSubKey[MAX_PATH];

              RegOpenKeyExW(hkey, szNextKey, 0, KEY_READ, &subk);
              while (rcs == ERROR_SUCCESS)
820
              {
821
                len = MAX_PATH;
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
                rcs = RegEnumKeyExW(subk, sub_index, szSubKey, &len, NULL, NULL, NULL, NULL);
                if (rcs == ERROR_SUCCESS)
                {
                  if (*supplied >= requested)
                  {
                    /* Bailing */
                    ret = S_FALSE;
                    rc = ERROR_MORE_DATA;
                    rcs = ERROR_MORE_DATA;
                    break;
                  }

                  wsprintfW(szGuidKey,szToGuidFmt,szNextKey);
                  CLSIDFromString(szGuidKey, &types[*supplied].type);
                  wsprintfW(szGuidKey,szToGuidFmt,szSubKey);
                  CLSIDFromString(szGuidKey, &types[*supplied].subtype);
                  TRACE("Adding type %s subtype %s at index %i\n",
                    debugstr_guid(&types[*supplied].type),
                    debugstr_guid(&types[*supplied].subtype),
                    *supplied);
                  (*supplied)++;
                }
                sub_index++;
845
              }
846
              index++;
847 848
            }
          }
849
          RegCloseKey(hkey);
850 851 852 853 854
        }
    }
    return ret;
}

855 856 857
/***************************************************************
 * DMOGetTypes (MSDMO.@)
 */
858 859 860 861 862 863 864 865 866 867 868 869
HRESULT WINAPI DMOGetTypes(REFCLSID clsidDMO,
               ULONG ulInputTypesRequested,
               ULONG* pulInputTypesSupplied,
               DMO_PARTIAL_MEDIATYPE* pInputTypes,
               ULONG ulOutputTypesRequested,
               ULONG* pulOutputTypesSupplied,
               DMO_PARTIAL_MEDIATYPE* pOutputTypes)
{
  HKEY root,hkey;
  HRESULT ret = S_OK;
  WCHAR szguid[64];

870 871 872
  TRACE ("(%s,%u,%p,%p,%u,%p,%p)\n", debugstr_guid(clsidDMO), ulInputTypesRequested,
        pulInputTypesSupplied, pInputTypes, ulOutputTypesRequested, pulOutputTypesSupplied,
        pOutputTypes);
873 874 875 876 877 878 879 880 881 882 883 884 885 886

  if (ERROR_SUCCESS != RegOpenKeyExW(HKEY_CLASSES_ROOT, szDMORootKey, 0,
                                     KEY_READ, &root))
    return E_FAIL;

  if (ERROR_SUCCESS != RegOpenKeyExW(root,GUIDToString(szguid,clsidDMO) , 0,
                                     KEY_READ, &hkey))
  {
    RegCloseKey(root);
    return E_FAIL;
  }

  if (ulInputTypesRequested > 0)
  {
887
    ret = read_types(hkey, szDMOInputType, pulInputTypesSupplied, ulInputTypesRequested, pInputTypes );
888 889 890 891 892 893 894
  }
  else
    *pulInputTypesSupplied = 0;

  if (ulOutputTypesRequested > 0)
  {
    HRESULT ret2;
895
    ret2 = read_types(hkey, szDMOOutputType, pulOutputTypesSupplied, ulOutputTypesRequested, pOutputTypes );
896 897 898 899 900 901 902 903

    if (ret == S_OK)
        ret = ret2;
  }
  else
    *pulOutputTypesSupplied = 0;

  return ret;
904
}