d3d9: Translate E_INVALIDARG to D3DERR_INVALIDCALL in d3d9_surface_LockRect().
[wine.git] / dlls / quartz / filtermapper.c
blob1a5f76d9a8f716636a08e1034819d50dffd3b65d
1 /*
2 * IFilterMapper & IFilterMapper3 Implementations
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
32 #include "quartz_private.h"
34 #include "ole2.h"
35 #include "olectl.h"
36 #include "strmif.h"
37 #include "wine/unicode.h"
38 #include "uuids.h"
39 #include "initguid.h"
40 #include "wine/fil_data.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
46 typedef struct FilterMapper3Impl
48 IUnknown IUnknown_inner;
49 IFilterMapper3 IFilterMapper3_iface;
50 IFilterMapper IFilterMapper_iface;
51 IAMFilterData IAMFilterData_iface;
52 IUnknown *outer_unk;
53 LONG ref;
54 } FilterMapper3Impl;
56 static inline FilterMapper3Impl *impl_from_IFilterMapper3( IFilterMapper3 *iface )
58 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper3_iface);
61 static inline FilterMapper3Impl *impl_from_IFilterMapper( IFilterMapper *iface )
63 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper_iface);
66 static inline FilterMapper3Impl *impl_from_IAMFilterData( IAMFilterData *iface )
68 return CONTAINING_RECORD(iface, FilterMapper3Impl, IAMFilterData_iface);
71 static inline FilterMapper3Impl *impl_from_IUnknown( IUnknown *iface )
73 return CONTAINING_RECORD(iface, FilterMapper3Impl, IUnknown_inner);
76 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
77 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
78 static const WCHAR wszSlash[] = {'\\',0};
80 /* CLSID property in media category Moniker */
81 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
82 /* FriendlyName property in media category Moniker */
83 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
84 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
85 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
86 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
87 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
88 /* For filters registered with IFilterMapper */
89 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
90 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
91 /* For pins registered with IFilterMapper */
92 static const WCHAR wszPins[] = {'P','i','n','s',0};
93 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
94 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
95 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
96 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
97 /* For types registered with IFilterMapper */
98 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
101 /* registry format for REGFILTER2 */
102 struct REG_RF
104 DWORD dwVersion;
105 DWORD dwMerit;
106 DWORD dwPins;
107 DWORD dwUnused;
110 struct REG_RFP
112 BYTE signature[4]; /* e.g. "0pi3" */
113 DWORD dwFlags;
114 DWORD dwInstances;
115 DWORD dwMediaTypes;
116 DWORD dwMediums;
117 DWORD bCategory; /* is there a category clsid? */
118 /* optional: dwOffsetCategoryClsid */
121 struct REG_TYPE
123 BYTE signature[4]; /* e.g. "0ty3" */
124 DWORD dwUnused;
125 DWORD dwOffsetMajor;
126 DWORD dwOffsetMinor;
129 struct MONIKER_MERIT
131 IMoniker * pMoniker;
132 DWORD dwMerit;
135 struct Vector
137 LPBYTE pData;
138 int capacity; /* in bytes */
139 int current; /* pointer to next free byte */
142 /* returns the position it was added at */
143 static int add_data(struct Vector *v, const void *pData, int size)
145 int index = v->current;
146 if (v->current + size > v->capacity)
148 int new_capacity = (v->capacity + size) * 2;
149 BYTE *new_data = CoTaskMemRealloc(v->pData, new_capacity);
150 if (!new_data) return -1;
151 v->capacity = new_capacity;
152 v->pData = new_data;
154 memcpy(v->pData + v->current, pData, size);
155 v->current += size;
156 return index;
159 static int find_data(const struct Vector *v, const void *pData, int size)
161 int index;
162 for (index = 0; index < v->current; index++)
163 if (!memcmp(v->pData + index, pData, size))
164 return index;
165 /* not found */
166 return -1;
169 static void delete_vector(struct Vector * v)
171 CoTaskMemFree(v->pData);
172 v->current = 0;
173 v->capacity = 0;
176 /*** IUnknown (inner) methods ***/
178 static HRESULT WINAPI Inner_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
180 FilterMapper3Impl *This = impl_from_IUnknown(iface);
182 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
184 *ppv = NULL;
185 if (IsEqualIID(riid, &IID_IUnknown))
186 *ppv = &This->IUnknown_inner;
187 else if (IsEqualIID(riid, &IID_IFilterMapper2) || IsEqualIID(riid, &IID_IFilterMapper3))
188 *ppv = &This->IFilterMapper3_iface;
189 else if (IsEqualIID(riid, &IID_IFilterMapper))
190 *ppv = &This->IFilterMapper_iface;
191 else if (IsEqualIID(riid, &IID_IAMFilterData))
192 *ppv = &This->IAMFilterData_iface;
194 if (*ppv != NULL)
196 IUnknown_AddRef((IUnknown *)*ppv);
197 return S_OK;
200 FIXME("No interface for %s\n", debugstr_guid(riid));
201 return E_NOINTERFACE;
204 static ULONG WINAPI Inner_AddRef(IUnknown *iface)
206 FilterMapper3Impl *This = impl_from_IUnknown(iface);
207 ULONG ref = InterlockedIncrement(&This->ref);
209 TRACE("(%p)->(): new ref = %d\n", This, ref);
211 return ref;
214 static ULONG WINAPI Inner_Release(IUnknown *iface)
216 FilterMapper3Impl *This = impl_from_IUnknown(iface);
217 ULONG ref = InterlockedDecrement(&This->ref);
219 TRACE("(%p)->(): new ref = %d\n", This, ref);
221 if (ref == 0)
222 CoTaskMemFree(This);
224 return ref;
227 static const IUnknownVtbl IInner_VTable =
229 Inner_QueryInterface,
230 Inner_AddRef,
231 Inner_Release
234 static HRESULT WINAPI FilterMapper3_QueryInterface(IFilterMapper3 * iface, REFIID riid, LPVOID *ppv)
236 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
238 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
241 static ULONG WINAPI FilterMapper3_AddRef(IFilterMapper3 * iface)
243 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
245 return IUnknown_AddRef(This->outer_unk);
248 static ULONG WINAPI FilterMapper3_Release(IFilterMapper3 * iface)
250 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
252 return IUnknown_Release(This->outer_unk);
255 /*** IFilterMapper3 methods ***/
257 static HRESULT WINAPI FilterMapper3_CreateCategory(
258 IFilterMapper3 * iface,
259 REFCLSID clsidCategory,
260 DWORD dwCategoryMerit,
261 LPCWSTR szDescription)
263 LPWSTR wClsidAMCat = NULL;
264 LPWSTR wClsidCategory = NULL;
265 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + ARRAY_SIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
266 HKEY hKey = NULL;
267 LONG lRet;
268 HRESULT hr;
270 TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
272 hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
274 if (SUCCEEDED(hr))
276 hr = StringFromCLSID(clsidCategory, &wClsidCategory);
279 if (SUCCEEDED(hr))
281 strcpyW(wszKeyName, wszClsidSlash);
282 strcatW(wszKeyName, wClsidAMCat);
283 strcatW(wszKeyName, wszSlashInstance);
284 strcatW(wszKeyName, wClsidCategory);
286 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
287 hr = HRESULT_FROM_WIN32(lRet);
290 if (SUCCEEDED(hr))
292 lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
293 hr = HRESULT_FROM_WIN32(lRet);
296 if (SUCCEEDED(hr))
298 lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
299 hr = HRESULT_FROM_WIN32(lRet);
302 if (SUCCEEDED(hr))
304 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
305 hr = HRESULT_FROM_WIN32(lRet);
308 RegCloseKey(hKey);
309 CoTaskMemFree(wClsidCategory);
310 CoTaskMemFree(wClsidAMCat);
312 return hr;
315 static HRESULT WINAPI FilterMapper3_UnregisterFilter(
316 IFilterMapper3 * iface,
317 const CLSID *pclsidCategory,
318 const OLECHAR *szInstance,
319 REFCLSID Filter)
321 WCHAR wszKeyName[MAX_PATH];
322 LPWSTR wClsidCategory = NULL;
323 LPWSTR wFilter = NULL;
324 HRESULT hr;
326 TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
328 if (!pclsidCategory)
329 pclsidCategory = &CLSID_LegacyAmFilterCategory;
331 hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
333 if (SUCCEEDED(hr))
335 strcpyW(wszKeyName, wszClsidSlash);
336 strcatW(wszKeyName, wClsidCategory);
337 strcatW(wszKeyName, wszSlashInstance);
338 if (szInstance)
339 strcatW(wszKeyName, szInstance);
340 else
342 hr = StringFromCLSID(Filter, &wFilter);
343 if (SUCCEEDED(hr))
344 strcatW(wszKeyName, wFilter);
348 if (SUCCEEDED(hr))
350 LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
351 hr = HRESULT_FROM_WIN32(lRet);
354 CoTaskMemFree(wClsidCategory);
355 CoTaskMemFree(wFilter);
357 return hr;
360 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
362 VARIANT var;
363 HRESULT ret;
364 BSTR value;
366 V_VT(&var) = VT_BSTR;
367 V_BSTR(&var) = value = SysAllocString(szName);
369 ret = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
370 SysFreeString(value);
372 return ret;
375 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
377 LPWSTR wszClsid = NULL;
378 VARIANT var;
379 HRESULT hr;
381 hr = StringFromCLSID(clsid, &wszClsid);
383 if (SUCCEEDED(hr))
385 V_VT(&var) = VT_BSTR;
386 V_BSTR(&var) = wszClsid;
387 hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
389 CoTaskMemFree(wszClsid);
390 return hr;
393 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
395 int size = sizeof(struct REG_RF);
396 unsigned int i;
397 struct Vector mainStore = {NULL, 0, 0};
398 struct Vector clsidStore = {NULL, 0, 0};
399 struct REG_RF rrf;
400 HRESULT hr = S_OK;
402 rrf.dwVersion = prf2->dwVersion;
403 rrf.dwMerit = prf2->dwMerit;
404 rrf.dwPins = prf2->u.s2.cPins2;
405 rrf.dwUnused = 0;
407 add_data(&mainStore, &rrf, sizeof(rrf));
409 for (i = 0; i < prf2->u.s2.cPins2; i++)
411 size += sizeof(struct REG_RFP);
412 if (prf2->u.s2.rgPins2[i].clsPinCategory)
413 size += sizeof(DWORD);
414 size += prf2->u.s2.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
415 size += prf2->u.s2.rgPins2[i].nMediums * sizeof(DWORD);
418 for (i = 0; i < prf2->u.s2.cPins2; i++)
420 struct REG_RFP rrfp;
421 REGFILTERPINS2 rgPin2 = prf2->u.s2.rgPins2[i];
422 unsigned int j;
424 rrfp.signature[0] = '0';
425 rrfp.signature[1] = 'p';
426 rrfp.signature[2] = 'i';
427 rrfp.signature[3] = '3';
428 rrfp.signature[0] += i;
429 rrfp.dwFlags = rgPin2.dwFlags;
430 rrfp.dwInstances = rgPin2.cInstances;
431 rrfp.dwMediaTypes = rgPin2.nMediaTypes;
432 rrfp.dwMediums = rgPin2.nMediums;
433 rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
435 add_data(&mainStore, &rrfp, sizeof(rrfp));
436 if (rrfp.bCategory)
438 DWORD index = find_data(&clsidStore, rgPin2.clsPinCategory, sizeof(CLSID));
439 if (index == -1)
440 index = add_data(&clsidStore, rgPin2.clsPinCategory, sizeof(CLSID));
441 index += size;
443 add_data(&mainStore, &index, sizeof(index));
446 for (j = 0; j < rgPin2.nMediaTypes; j++)
448 struct REG_TYPE rt;
449 const CLSID * clsMinorType = rgPin2.lpMediaType[j].clsMinorType ? rgPin2.lpMediaType[j].clsMinorType : &MEDIASUBTYPE_NULL;
450 rt.signature[0] = '0';
451 rt.signature[1] = 't';
452 rt.signature[2] = 'y';
453 rt.signature[3] = '3';
454 rt.signature[0] += j;
455 rt.dwUnused = 0;
456 rt.dwOffsetMajor = find_data(&clsidStore, rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
457 if (rt.dwOffsetMajor == -1)
458 rt.dwOffsetMajor = add_data(&clsidStore, rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
459 rt.dwOffsetMajor += size;
460 rt.dwOffsetMinor = find_data(&clsidStore, clsMinorType, sizeof(CLSID));
461 if (rt.dwOffsetMinor == -1)
462 rt.dwOffsetMinor = add_data(&clsidStore, clsMinorType, sizeof(CLSID));
463 rt.dwOffsetMinor += size;
465 add_data(&mainStore, &rt, sizeof(rt));
468 for (j = 0; j < rgPin2.nMediums; j++)
470 DWORD index = find_data(&clsidStore, rgPin2.lpMedium + j, sizeof(REGPINMEDIUM));
471 if (index == -1)
472 index = add_data(&clsidStore, rgPin2.lpMedium + j, sizeof(REGPINMEDIUM));
473 index += size;
475 add_data(&mainStore, &index, sizeof(index));
479 if (SUCCEEDED(hr))
481 *pcbData = mainStore.current + clsidStore.current;
482 *ppData = CoTaskMemAlloc(*pcbData);
483 if (!*ppData)
484 hr = E_OUTOFMEMORY;
487 if (SUCCEEDED(hr))
489 memcpy(*ppData, mainStore.pData, mainStore.current);
490 memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
493 delete_vector(&mainStore);
494 delete_vector(&clsidStore);
495 return hr;
498 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
500 HRESULT hr = S_OK;
501 struct REG_RF * prrf;
502 LPBYTE pCurrent;
503 DWORD i;
504 REGFILTERPINS2 * rgPins2;
506 prrf = (struct REG_RF *)pData;
507 pCurrent = pData;
509 if (prrf->dwVersion != 2)
511 FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
512 ZeroMemory(prf2, sizeof(*prf2));
513 hr = E_FAIL;
516 if (SUCCEEDED(hr))
518 TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
519 prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
521 prf2->dwVersion = prrf->dwVersion;
522 prf2->dwMerit = prrf->dwMerit;
523 prf2->u.s2.cPins2 = prrf->dwPins;
524 rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
525 prf2->u.s2.rgPins2 = rgPins2;
526 pCurrent += sizeof(struct REG_RF);
528 for (i = 0; i < prrf->dwPins; i++)
530 struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
531 REGPINTYPES * lpMediaType;
532 REGPINMEDIUM * lpMedium;
533 UINT j;
535 /* FIXME: check signature */
537 TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
539 TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
540 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
542 rgPins2[i].dwFlags = prrfp->dwFlags;
543 rgPins2[i].cInstances = prrfp->dwInstances;
544 rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
545 rgPins2[i].nMediums = prrfp->dwMediums;
546 pCurrent += sizeof(struct REG_RFP);
547 if (prrfp->bCategory)
549 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
550 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
551 pCurrent += sizeof(DWORD);
552 rgPins2[i].clsPinCategory = clsCat;
554 else
555 rgPins2[i].clsPinCategory = NULL;
557 if (rgPins2[i].nMediaTypes > 0)
558 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
559 else
560 lpMediaType = NULL;
562 rgPins2[i].lpMediaType = lpMediaType;
564 for (j = 0; j < rgPins2[i].nMediaTypes; j++)
566 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
567 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
568 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
570 /* FIXME: check signature */
571 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
573 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
574 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
576 lpMediaType[j].clsMajorType = clsMajor;
577 lpMediaType[j].clsMinorType = clsMinor;
579 pCurrent += sizeof(*prt);
582 if (rgPins2[i].nMediums > 0)
583 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
584 else
585 lpMedium = NULL;
587 rgPins2[i].lpMedium = lpMedium;
589 for (j = 0; j < rgPins2[i].nMediums; j++)
591 DWORD dwOffset = *(DWORD *)pCurrent;
593 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
595 pCurrent += sizeof(dwOffset);
601 return hr;
604 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
606 UINT i;
607 for (i = 0; i < prf2->u.s2.cPins2; i++)
609 UINT j;
610 CoTaskMemFree((void*)prf2->u.s2.rgPins2[i].clsPinCategory);
612 for (j = 0; j < prf2->u.s2.rgPins2[i].nMediaTypes; j++)
614 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMajorType);
615 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMinorType);
617 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType);
618 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMedium);
620 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2);
623 static HRESULT WINAPI FilterMapper3_RegisterFilter(
624 IFilterMapper3 * iface,
625 REFCLSID clsidFilter,
626 LPCWSTR szName,
627 IMoniker **ppMoniker,
628 const CLSID *pclsidCategory,
629 const OLECHAR *szInstance,
630 const REGFILTER2 *prf2)
632 IParseDisplayName * pParser = NULL;
633 IBindCtx * pBindCtx = NULL;
634 IMoniker * pMoniker = NULL;
635 IPropertyBag * pPropBag = NULL;
636 HRESULT hr;
637 LPWSTR pwszParseName = NULL;
638 LPWSTR pCurrent;
639 static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
640 int nameLen;
641 ULONG ulEaten;
642 LPWSTR szClsidTemp = NULL;
643 REGFILTER2 regfilter2;
644 REGFILTERPINS2* pregfp2 = NULL;
646 TRACE("(%s, %s, %p, %s, %s, %p)\n",
647 debugstr_guid(clsidFilter),
648 debugstr_w(szName),
649 ppMoniker,
650 debugstr_guid(pclsidCategory),
651 debugstr_w(szInstance),
652 prf2);
654 if (prf2->dwVersion == 2)
656 regfilter2 = *prf2;
658 else if (prf2->dwVersion == 1)
660 ULONG i;
661 DWORD flags;
662 /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
663 regfilter2.dwVersion = 2;
664 regfilter2.dwMerit = prf2->dwMerit;
665 regfilter2.u.s2.cPins2 = prf2->u.s1.cPins;
666 pregfp2 = CoTaskMemAlloc(prf2->u.s1.cPins * sizeof(REGFILTERPINS2));
667 regfilter2.u.s2.rgPins2 = pregfp2;
668 for (i = 0; i < prf2->u.s1.cPins; i++)
670 flags = 0;
671 if (prf2->u.s1.rgPins[i].bRendered)
672 flags |= REG_PINFLAG_B_RENDERER;
673 if (prf2->u.s1.rgPins[i].bOutput)
674 flags |= REG_PINFLAG_B_OUTPUT;
675 if (prf2->u.s1.rgPins[i].bZero)
676 flags |= REG_PINFLAG_B_ZERO;
677 if (prf2->u.s1.rgPins[i].bMany)
678 flags |= REG_PINFLAG_B_MANY;
679 pregfp2[i].dwFlags = flags;
680 pregfp2[i].cInstances = 1;
681 pregfp2[i].nMediaTypes = prf2->u.s1.rgPins[i].nMediaTypes;
682 pregfp2[i].lpMediaType = prf2->u.s1.rgPins[i].lpMediaType;
683 pregfp2[i].nMediums = 0;
684 pregfp2[i].lpMedium = NULL;
685 pregfp2[i].clsPinCategory = NULL;
688 else
690 FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
691 return E_NOTIMPL;
694 if (ppMoniker)
695 *ppMoniker = NULL;
697 if (!pclsidCategory)
698 /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
699 * In fact this is the CLSID_LegacyAmFilterCategory one */
700 pclsidCategory = &CLSID_LegacyAmFilterCategory;
702 /* sizeof... will include the null terminator and
703 * the + 1 is for the separator ('\\'). The -1 is
704 * because CHARS_IN_GUID includes the null terminator
706 nameLen = ARRAY_SIZE(wszDevice) + CHARS_IN_GUID - 1 + 1;
708 if (szInstance)
709 nameLen += strlenW(szInstance);
710 else
711 nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
713 pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
714 if (!pwszParseName)
715 return E_OUTOFMEMORY;
717 strcpyW(pwszParseName, wszDevice);
718 pCurrent += strlenW(wszDevice);
720 hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
722 if (SUCCEEDED(hr))
724 memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
725 pCurrent += CHARS_IN_GUID - 1;
726 pCurrent[0] = '\\';
728 if (szInstance)
729 strcpyW(pCurrent+1, szInstance);
730 else
732 CoTaskMemFree(szClsidTemp);
733 szClsidTemp = NULL;
735 hr = StringFromCLSID(clsidFilter, &szClsidTemp);
736 if (SUCCEEDED(hr))
737 strcpyW(pCurrent+1, szClsidTemp);
741 if (SUCCEEDED(hr))
742 hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
744 if (SUCCEEDED(hr))
745 hr = CreateBindCtx(0, &pBindCtx);
747 if (SUCCEEDED(hr))
748 hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
750 if (pBindCtx)
751 IBindCtx_Release(pBindCtx);
752 if (pParser)
753 IParseDisplayName_Release(pParser);
755 if (SUCCEEDED(hr))
756 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
758 if (SUCCEEDED(hr))
759 hr = FM2_WriteFriendlyName(pPropBag, szName);
761 if (SUCCEEDED(hr))
762 hr = FM2_WriteClsid(pPropBag, clsidFilter);
764 if (SUCCEEDED(hr))
766 BYTE *pData;
767 ULONG cbData;
769 hr = FM2_WriteFilterData(&regfilter2, &pData, &cbData);
770 if (SUCCEEDED(hr))
772 VARIANT var;
773 SAFEARRAY *psa;
774 SAFEARRAYBOUND saBound;
776 saBound.lLbound = 0;
777 saBound.cElements = cbData;
778 psa = SafeArrayCreate(VT_UI1, 1, &saBound);
779 if (!psa)
781 ERR("Couldn't create SAFEARRAY\n");
782 hr = E_FAIL;
785 if (SUCCEEDED(hr))
787 LPBYTE pbSAData;
788 hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
789 if (SUCCEEDED(hr))
791 memcpy(pbSAData, pData, cbData);
792 hr = SafeArrayUnaccessData(psa);
796 V_VT(&var) = VT_ARRAY | VT_UI1;
797 V_ARRAY(&var) = psa;
799 if (SUCCEEDED(hr))
800 hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
802 if (psa)
803 SafeArrayDestroy(psa);
804 CoTaskMemFree(pData);
808 if (pPropBag)
809 IPropertyBag_Release(pPropBag);
810 CoTaskMemFree(szClsidTemp);
811 CoTaskMemFree(pwszParseName);
813 if (SUCCEEDED(hr) && ppMoniker)
814 *ppMoniker = pMoniker;
815 else if (pMoniker)
816 IMoniker_Release(pMoniker);
818 CoTaskMemFree(pregfp2);
820 TRACE("-- returning %x\n", hr);
822 return hr;
825 /* internal helper function */
826 static BOOL MatchTypes(
827 BOOL bExactMatch,
828 DWORD nPinTypes,
829 const REGPINTYPES * pPinTypes,
830 DWORD nMatchTypes,
831 const GUID * pMatchTypes)
833 BOOL bMatch = FALSE;
834 DWORD j;
836 if ((nMatchTypes == 0) && (nPinTypes > 0))
837 bMatch = TRUE;
839 for (j = 0; j < nPinTypes; j++)
841 DWORD i;
842 for (i = 0; i < nMatchTypes*2; i+=2)
844 if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
845 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
847 bMatch = TRUE;
848 break;
852 return bMatch;
855 /* internal helper function for qsort of MONIKER_MERIT array */
856 static int mm_compare(const void * left, const void * right)
858 const struct MONIKER_MERIT * mmLeft = left;
859 const struct MONIKER_MERIT * mmRight = right;
861 if (mmLeft->dwMerit == mmRight->dwMerit)
862 return 0;
863 if (mmLeft->dwMerit > mmRight->dwMerit)
864 return -1;
865 return 1;
868 /* NOTES:
869 * Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
870 * (GUID_NULL's in input to function automatically treated as wild cards)
871 * Input/Output needed means match only on criteria if TRUE (with zero input types
872 * meaning match any input/output pin as long as one exists), otherwise match any
873 * filter that meets the rest of the requirements.
875 static HRESULT WINAPI FilterMapper3_EnumMatchingFilters(
876 IFilterMapper3 * iface,
877 IEnumMoniker **ppEnum,
878 DWORD dwFlags,
879 BOOL bExactMatch,
880 DWORD dwMerit,
881 BOOL bInputNeeded,
882 DWORD cInputTypes,
883 const GUID *pInputTypes,
884 const REGPINMEDIUM *pMedIn,
885 const CLSID *pPinCategoryIn,
886 BOOL bRender,
887 BOOL bOutputNeeded,
888 DWORD cOutputTypes,
889 const GUID *pOutputTypes,
890 const REGPINMEDIUM *pMedOut,
891 const CLSID *pPinCategoryOut)
893 ICreateDevEnum * pCreateDevEnum;
894 IMoniker * pMonikerCat;
895 IEnumMoniker * pEnumCat;
896 HRESULT hr;
897 struct Vector monikers = {NULL, 0, 0};
899 TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
900 ppEnum,
901 dwFlags,
902 bExactMatch ? "true" : "false",
903 dwMerit,
904 bInputNeeded ? "true" : "false",
905 cInputTypes,
906 pInputTypes,
907 pMedIn,
908 pPinCategoryIn,
909 bRender ? "true" : "false",
910 bOutputNeeded ? "true" : "false",
911 pOutputTypes,
912 pMedOut,
913 pPinCategoryOut);
915 if (dwFlags != 0)
917 FIXME("dwFlags = %x not implemented\n", dwFlags);
920 *ppEnum = NULL;
922 hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
923 if (FAILED(hr))
924 return hr;
926 hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
927 if (FAILED(hr)) {
928 ICreateDevEnum_Release(pCreateDevEnum);
929 return hr;
932 while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
934 IPropertyBag * pPropBagCat = NULL;
935 VARIANT var;
936 HRESULT hrSub; /* this is so that one buggy filter
937 doesn't make the whole lot fail */
939 VariantInit(&var);
941 hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
943 if (SUCCEEDED(hrSub))
944 hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
946 if (SUCCEEDED(hrSub) && (V_UI4(&var) >= dwMerit))
948 CLSID clsidCat;
949 IEnumMoniker * pEnum;
950 IMoniker * pMoniker;
952 VariantClear(&var);
954 if (TRACE_ON(quartz))
956 VARIANT temp;
957 V_VT(&temp) = VT_EMPTY;
958 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
959 TRACE("Considering category %s\n", debugstr_w(V_BSTR(&temp)));
960 VariantClear(&temp);
963 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
965 if (SUCCEEDED(hrSub))
966 hrSub = CLSIDFromString(V_BSTR(&var), &clsidCat);
968 if (SUCCEEDED(hrSub))
969 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
971 if (hrSub == S_OK)
973 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
975 IPropertyBag * pPropBag = NULL;
976 VARIANT var;
977 BYTE *pData = NULL;
978 REGFILTER2 rf2;
979 DWORD i;
980 BOOL bInputMatch = !bInputNeeded;
981 BOOL bOutputMatch = !bOutputNeeded;
983 ZeroMemory(&rf2, sizeof(rf2));
984 VariantInit(&var);
986 hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
988 if (TRACE_ON(quartz))
990 VARIANT temp;
991 V_VT(&temp) = VT_EMPTY;
992 IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
993 TRACE("Considering filter %s\n", debugstr_w(V_BSTR(&temp)));
994 VariantClear(&temp);
997 if (SUCCEEDED(hrSub))
999 hrSub = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
1002 if (SUCCEEDED(hrSub))
1003 hrSub = SafeArrayAccessData(V_ARRAY(&var), (LPVOID*)&pData);
1005 if (SUCCEEDED(hrSub))
1006 hrSub = FM2_ReadFilterData(pData, &rf2);
1008 if (pData)
1009 SafeArrayUnaccessData(V_ARRAY(&var));
1011 VariantClear(&var);
1013 /* Logic used for bInputMatch expression:
1014 * There exists some pin such that bInputNeeded implies (pin is an input and
1015 * (bRender implies pin has render flag) and major/minor types members of
1016 * pInputTypes )
1017 * bOutputMatch is similar, but without the "bRender implies ..." part
1018 * and substituting variables names containing input for output
1021 /* determine whether filter meets requirements */
1022 if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
1024 for (i = 0; (i < rf2.u.s2.cPins2) && (!bInputMatch || !bOutputMatch); i++)
1026 const REGFILTERPINS2 * rfp2 = rf2.u.s2.rgPins2 + i;
1028 bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1029 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
1030 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
1031 bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1032 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
1035 if (bInputMatch && bOutputMatch)
1037 struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1038 IMoniker_AddRef(pMoniker);
1039 add_data(&monikers, &mm, sizeof(mm));
1043 FM2_DeleteRegFilter(&rf2);
1044 if (pPropBag)
1045 IPropertyBag_Release(pPropBag);
1046 IMoniker_Release(pMoniker);
1048 IEnumMoniker_Release(pEnum);
1052 VariantClear(&var);
1053 if (pPropBagCat)
1054 IPropertyBag_Release(pPropBagCat);
1055 IMoniker_Release(pMonikerCat);
1058 if (SUCCEEDED(hr))
1060 IMoniker ** ppMoniker;
1061 unsigned int i;
1062 ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1064 /* sort the monikers in descending merit order */
1065 qsort(monikers.pData, nMonikerCount,
1066 sizeof(struct MONIKER_MERIT),
1067 mm_compare);
1069 /* construct an IEnumMoniker interface */
1070 ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1071 for (i = 0; i < nMonikerCount; i++)
1073 /* no need to AddRef here as already AddRef'd above */
1074 ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1076 hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1077 CoTaskMemFree(ppMoniker);
1080 delete_vector(&monikers);
1081 IEnumMoniker_Release(pEnumCat);
1082 ICreateDevEnum_Release(pCreateDevEnum);
1084 return hr;
1087 static HRESULT WINAPI FilterMapper3_GetICreateDevEnum(IFilterMapper3 *iface, ICreateDevEnum **ppEnum)
1089 TRACE("(%p, %p)\n", iface, ppEnum);
1090 if (!ppEnum)
1091 return E_POINTER;
1092 return CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (void**)ppEnum);
1095 static const IFilterMapper3Vtbl fm3vtbl =
1098 FilterMapper3_QueryInterface,
1099 FilterMapper3_AddRef,
1100 FilterMapper3_Release,
1102 FilterMapper3_CreateCategory,
1103 FilterMapper3_UnregisterFilter,
1104 FilterMapper3_RegisterFilter,
1105 FilterMapper3_EnumMatchingFilters,
1106 FilterMapper3_GetICreateDevEnum
1109 /*** IUnknown methods ***/
1111 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1113 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1115 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1117 return FilterMapper3_QueryInterface(&This->IFilterMapper3_iface, riid, ppv);
1120 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1122 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1124 return IUnknown_AddRef(This->outer_unk);
1127 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1129 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1131 return IUnknown_Release(This->outer_unk);
1134 /*** IFilterMapper methods ***/
1136 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1137 IFilterMapper * iface,
1138 IEnumRegFilters **ppEnum,
1139 DWORD dwMerit,
1140 BOOL bInputNeeded,
1141 CLSID clsInMaj,
1142 CLSID clsInSub,
1143 BOOL bRender,
1144 BOOL bOutputNeeded,
1145 CLSID clsOutMaj,
1146 CLSID clsOutSub)
1148 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1149 GUID InputType[2];
1150 GUID OutputType[2];
1151 IEnumMoniker* ppEnumMoniker;
1152 IMoniker* IMon;
1153 ULONG nb;
1154 ULONG idx = 0, nb_mon = 0;
1155 REGFILTER* regfilters;
1156 HRESULT hr;
1158 TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s)\n",
1159 This,
1160 iface,
1161 ppEnum,
1162 dwMerit,
1163 bInputNeeded ? "true" : "false",
1164 debugstr_guid(&clsInMaj),
1165 debugstr_guid(&clsInSub),
1166 bRender ? "true" : "false",
1167 bOutputNeeded ? "true" : "false",
1168 debugstr_guid(&clsOutMaj),
1169 debugstr_guid(&clsOutSub));
1171 InputType[0] = clsInMaj;
1172 InputType[1] = clsInSub;
1173 OutputType[0] = clsOutMaj;
1174 OutputType[1] = clsOutSub;
1176 *ppEnum = NULL;
1178 hr = IFilterMapper3_EnumMatchingFilters(&This->IFilterMapper3_iface, &ppEnumMoniker, 0, TRUE,
1179 dwMerit, bInputNeeded, 1, InputType, NULL, &GUID_NULL, bRender, bOutputNeeded, 1,
1180 OutputType, NULL, &GUID_NULL);
1182 if (FAILED(hr))
1183 return hr;
1185 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1187 IMoniker_Release(IMon);
1188 nb_mon++;
1191 if (!nb_mon)
1193 IEnumMoniker_Release(ppEnumMoniker);
1194 return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1197 regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1198 if (!regfilters)
1200 IEnumMoniker_Release(ppEnumMoniker);
1201 return E_OUTOFMEMORY;
1203 ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1205 IEnumMoniker_Reset(ppEnumMoniker);
1206 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1208 IPropertyBag * pPropBagCat = NULL;
1209 VARIANT var;
1210 HRESULT hrSub;
1211 GUID clsid;
1212 int len;
1214 VariantInit(&var);
1216 hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1218 if (SUCCEEDED(hrSub))
1219 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1221 if (SUCCEEDED(hrSub))
1222 hrSub = CLSIDFromString(V_BSTR(&var), &clsid);
1224 VariantClear(&var);
1226 if (SUCCEEDED(hrSub))
1227 hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1229 if (SUCCEEDED(hrSub))
1231 len = (strlenW(V_BSTR(&var))+1) * sizeof(WCHAR);
1232 if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1233 hr = E_OUTOFMEMORY;
1236 if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1238 memcpy(regfilters[idx].Name, V_BSTR(&var), len);
1239 regfilters[idx].Clsid = clsid;
1240 idx++;
1243 if (pPropBagCat)
1244 IPropertyBag_Release(pPropBagCat);
1245 IMoniker_Release(IMon);
1246 VariantClear(&var);
1249 if (SUCCEEDED(hr))
1251 hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1254 for (idx = 0; idx < nb_mon; idx++)
1255 CoTaskMemFree(regfilters[idx].Name);
1256 CoTaskMemFree(regfilters);
1257 IEnumMoniker_Release(ppEnumMoniker);
1259 return hr;
1263 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1265 HRESULT hr;
1266 LPWSTR wszClsid = NULL;
1267 HKEY hKey;
1268 LONG lRet;
1269 WCHAR wszKeyName[ARRAY_SIZE(wszFilterSlash)-1 + (CHARS_IN_GUID-1) + 1];
1271 TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1273 hr = StringFromCLSID(&clsid, &wszClsid);
1275 if (SUCCEEDED(hr))
1277 strcpyW(wszKeyName, wszFilterSlash);
1278 strcatW(wszKeyName, wszClsid);
1280 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1281 hr = HRESULT_FROM_WIN32(lRet);
1284 if (SUCCEEDED(hr))
1286 lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (strlenW(szName) + 1) * sizeof(WCHAR));
1287 hr = HRESULT_FROM_WIN32(lRet);
1288 RegCloseKey(hKey);
1291 if (SUCCEEDED(hr))
1293 strcpyW(wszKeyName, wszClsidSlash);
1294 strcatW(wszKeyName, wszClsid);
1296 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1297 hr = HRESULT_FROM_WIN32(lRet);
1300 if (SUCCEEDED(hr))
1302 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1303 hr = HRESULT_FROM_WIN32(lRet);
1304 RegCloseKey(hKey);
1307 CoTaskMemFree(wszClsid);
1309 return hr;
1312 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1314 TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1316 /* Not implemented in Windows (tested on Win2k) */
1318 return E_NOTIMPL;
1321 static HRESULT WINAPI FilterMapper_RegisterPin(
1322 IFilterMapper * iface,
1323 CLSID Filter,
1324 LPCWSTR szName,
1325 BOOL bRendered,
1326 BOOL bOutput,
1327 BOOL bZero,
1328 BOOL bMany,
1329 CLSID ConnectsToFilter,
1330 LPCWSTR ConnectsToPin)
1332 HRESULT hr;
1333 LONG lRet;
1334 LPWSTR wszClsid = NULL;
1335 HKEY hKey = NULL;
1336 HKEY hPinsKey = NULL;
1337 WCHAR * wszPinsKeyName;
1338 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1340 TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1341 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1343 hr = StringFromCLSID(&Filter, &wszClsid);
1345 if (SUCCEEDED(hr))
1347 strcpyW(wszKeyName, wszClsidSlash);
1348 strcatW(wszKeyName, wszClsid);
1350 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1351 hr = HRESULT_FROM_WIN32(lRet);
1354 if (SUCCEEDED(hr))
1356 wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1357 if (!wszPinsKeyName)
1358 hr = E_OUTOFMEMORY;
1361 if (SUCCEEDED(hr))
1363 strcpyW(wszPinsKeyName, wszPins);
1364 strcatW(wszPinsKeyName, wszSlash);
1365 strcatW(wszPinsKeyName, szName);
1367 lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1368 hr = HRESULT_FROM_WIN32(lRet);
1369 CoTaskMemFree(wszPinsKeyName);
1372 if (SUCCEEDED(hr))
1374 lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1375 hr = HRESULT_FROM_WIN32(lRet);
1378 if (SUCCEEDED(hr))
1380 lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1381 hr = HRESULT_FROM_WIN32(lRet);
1384 if (SUCCEEDED(hr))
1386 lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1387 hr = HRESULT_FROM_WIN32(lRet);
1390 if (SUCCEEDED(hr))
1392 lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1393 hr = HRESULT_FROM_WIN32(lRet);
1396 if (SUCCEEDED(hr))
1398 HKEY hkeyDummy = NULL;
1400 lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1401 hr = HRESULT_FROM_WIN32(lRet);
1403 if (hkeyDummy) RegCloseKey(hkeyDummy);
1406 CoTaskMemFree(wszClsid);
1407 if (hKey)
1408 RegCloseKey(hKey);
1409 if (hPinsKey)
1410 RegCloseKey(hPinsKey);
1412 return hr;
1416 static HRESULT WINAPI FilterMapper_RegisterPinType(
1417 IFilterMapper * iface,
1418 CLSID clsFilter,
1419 LPCWSTR szName,
1420 CLSID clsMajorType,
1421 CLSID clsSubType)
1423 HRESULT hr;
1424 LONG lRet;
1425 LPWSTR wszClsid = NULL;
1426 LPWSTR wszClsidMajorType = NULL;
1427 LPWSTR wszClsidSubType = NULL;
1428 HKEY hKey = NULL;
1429 WCHAR * wszTypesKey;
1430 WCHAR wszKeyName[MAX_PATH];
1432 TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1433 debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1435 hr = StringFromCLSID(&clsFilter, &wszClsid);
1437 if (SUCCEEDED(hr))
1439 hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1442 if (SUCCEEDED(hr))
1444 hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1447 if (SUCCEEDED(hr))
1449 wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1450 strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1451 if (!wszTypesKey)
1452 hr = E_OUTOFMEMORY;
1455 if (SUCCEEDED(hr))
1457 strcpyW(wszTypesKey, wszClsidSlash);
1458 strcatW(wszTypesKey, wszClsid);
1459 strcatW(wszTypesKey, wszSlash);
1460 strcatW(wszTypesKey, wszPins);
1461 strcatW(wszTypesKey, wszSlash);
1462 strcatW(wszTypesKey, szName);
1463 strcatW(wszTypesKey, wszSlash);
1464 strcatW(wszTypesKey, wszTypes);
1466 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1467 hr = HRESULT_FROM_WIN32(lRet);
1468 CoTaskMemFree(wszTypesKey);
1471 if (SUCCEEDED(hr))
1473 HKEY hkeyDummy = NULL;
1475 strcpyW(wszKeyName, wszClsidMajorType);
1476 strcatW(wszKeyName, wszSlash);
1477 strcatW(wszKeyName, wszClsidSubType);
1479 lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1480 hr = HRESULT_FROM_WIN32(lRet);
1481 RegCloseKey(hKey);
1483 if (hkeyDummy) RegCloseKey(hkeyDummy);
1486 CoTaskMemFree(wszClsid);
1487 CoTaskMemFree(wszClsidMajorType);
1488 CoTaskMemFree(wszClsidSubType);
1490 return hr;
1493 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1495 HRESULT hr;
1496 LONG lRet;
1497 LPWSTR wszClsid = NULL;
1498 HKEY hKey;
1499 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1501 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1503 hr = StringFromCLSID(&Filter, &wszClsid);
1505 if (SUCCEEDED(hr))
1507 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1508 hr = HRESULT_FROM_WIN32(lRet);
1511 if (SUCCEEDED(hr))
1513 lRet = RegDeleteKeyW(hKey, wszClsid);
1514 hr = HRESULT_FROM_WIN32(lRet);
1515 RegCloseKey(hKey);
1518 if (SUCCEEDED(hr))
1520 strcpyW(wszKeyName, wszClsidSlash);
1521 strcatW(wszKeyName, wszClsid);
1523 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1524 if (lRet == ERROR_FILE_NOT_FOUND)
1525 goto done;
1526 hr = HRESULT_FROM_WIN32(lRet);
1529 if (SUCCEEDED(hr))
1531 lRet = RegDeleteValueW(hKey, wszMeritName);
1532 if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
1533 hr = HRESULT_FROM_WIN32(lRet);
1535 lRet = RegDeleteTreeW(hKey, wszPins);
1536 if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
1537 hr = HRESULT_FROM_WIN32(lRet);
1539 RegCloseKey(hKey);
1542 done:
1543 CoTaskMemFree(wszClsid);
1545 return hr;
1548 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1550 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1552 /* Not implemented in Windows (tested on Win2k) */
1554 return E_NOTIMPL;
1557 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1559 HRESULT hr;
1560 LONG lRet;
1561 LPWSTR wszClsid = NULL;
1562 HKEY hKey = NULL;
1563 WCHAR * wszPinNameKey;
1564 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1566 TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1568 if (!Name)
1569 return E_INVALIDARG;
1571 hr = StringFromCLSID(&Filter, &wszClsid);
1573 if (SUCCEEDED(hr))
1575 strcpyW(wszKeyName, wszClsidSlash);
1576 strcatW(wszKeyName, wszClsid);
1578 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1579 hr = HRESULT_FROM_WIN32(lRet);
1582 if (SUCCEEDED(hr))
1584 wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1585 if (!wszPinNameKey)
1586 hr = E_OUTOFMEMORY;
1589 if (SUCCEEDED(hr))
1591 strcpyW(wszPinNameKey, wszPins);
1592 strcatW(wszPinNameKey, wszSlash);
1593 strcatW(wszPinNameKey, Name);
1595 lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1596 hr = HRESULT_FROM_WIN32(lRet);
1597 CoTaskMemFree(wszPinNameKey);
1600 CoTaskMemFree(wszClsid);
1601 if (hKey)
1602 RegCloseKey(hKey);
1604 return hr;
1607 static const IFilterMapperVtbl fmvtbl =
1610 FilterMapper_QueryInterface,
1611 FilterMapper_AddRef,
1612 FilterMapper_Release,
1614 FilterMapper_RegisterFilter,
1615 FilterMapper_RegisterFilterInstance,
1616 FilterMapper_RegisterPin,
1617 FilterMapper_RegisterPinType,
1618 FilterMapper_UnregisterFilter,
1619 FilterMapper_UnregisterFilterInstance,
1620 FilterMapper_UnregisterPin,
1621 FilterMapper_EnumMatchingFilters
1625 /*** IUnknown methods ***/
1626 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1628 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1630 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1633 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1635 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1637 return IUnknown_AddRef(This->outer_unk);
1640 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1642 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1644 return IUnknown_Release(This->outer_unk);
1647 /*** IAMFilterData methods ***/
1648 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1649 BYTE *pData, ULONG cb,
1650 BYTE **ppRegFilter2)
1652 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1653 HRESULT hr = S_OK;
1654 static REGFILTER2 *prf2;
1656 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1658 prf2 = CoTaskMemAlloc(sizeof(*prf2));
1659 if (!prf2)
1660 return E_OUTOFMEMORY;
1661 *ppRegFilter2 = (BYTE *)&prf2;
1663 hr = FM2_ReadFilterData(pData, prf2);
1664 if (FAILED(hr))
1666 CoTaskMemFree(prf2);
1667 *ppRegFilter2 = NULL;
1670 return hr;
1673 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1674 REGFILTER2 *prf2,
1675 BYTE **pRegFilterData,
1676 ULONG *pcb)
1678 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1680 TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1682 return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1685 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1686 AMFilterData_QueryInterface,
1687 AMFilterData_AddRef,
1688 AMFilterData_Release,
1689 AMFilterData_ParseFilterData,
1690 AMFilterData_CreateFilterData
1693 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1695 FilterMapper3Impl * pFM2impl;
1697 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1699 pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
1700 if (!pFM2impl)
1701 return E_OUTOFMEMORY;
1703 pFM2impl->IUnknown_inner.lpVtbl = &IInner_VTable;
1704 pFM2impl->IFilterMapper3_iface.lpVtbl = &fm3vtbl;
1705 pFM2impl->IFilterMapper_iface.lpVtbl = &fmvtbl;
1706 pFM2impl->IAMFilterData_iface.lpVtbl = &AMFilterDataVtbl;
1707 pFM2impl->ref = 1;
1709 if (pUnkOuter)
1710 pFM2impl->outer_unk = pUnkOuter;
1711 else
1712 pFM2impl->outer_unk = &pFM2impl->IUnknown_inner;
1714 *ppObj = &pFM2impl->IUnknown_inner;
1716 TRACE("-- created at %p\n", pFM2impl);
1718 return S_OK;
1721 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1723 FilterMapper3Impl *pFM2impl;
1724 HRESULT hr;
1726 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1728 hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
1729 if (FAILED(hr))
1730 return hr;
1732 *ppObj = &pFM2impl->IFilterMapper_iface;
1734 return hr;