jscript: Implement ScriptTypeInfo_GetTypeAttr.
[wine.git] / dlls / quartz / filtermapper.c
blobf0a52c4411cd0a8aa2e68ff6394179c69ea4ff4a
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 "uuids.h"
38 #include "initguid.h"
39 #include "wine/fil_data.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
45 typedef struct FilterMapper3Impl
47 IUnknown IUnknown_inner;
48 IFilterMapper3 IFilterMapper3_iface;
49 IFilterMapper IFilterMapper_iface;
50 IAMFilterData IAMFilterData_iface;
51 IUnknown *outer_unk;
52 LONG ref;
53 } FilterMapper3Impl;
55 static inline FilterMapper3Impl *impl_from_IFilterMapper3( IFilterMapper3 *iface )
57 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper3_iface);
60 static inline FilterMapper3Impl *impl_from_IFilterMapper( IFilterMapper *iface )
62 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper_iface);
65 static inline FilterMapper3Impl *impl_from_IAMFilterData( IAMFilterData *iface )
67 return CONTAINING_RECORD(iface, FilterMapper3Impl, IAMFilterData_iface);
70 static inline FilterMapper3Impl *impl_from_IUnknown( IUnknown *iface )
72 return CONTAINING_RECORD(iface, FilterMapper3Impl, IUnknown_inner);
75 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
76 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
77 static const WCHAR wszSlash[] = {'\\',0};
79 /* CLSID property in media category Moniker */
80 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
81 /* FriendlyName property in media category Moniker */
82 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
83 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
84 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
85 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
86 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
87 /* For filters registered with IFilterMapper */
88 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
89 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
90 /* For pins registered with IFilterMapper */
91 static const WCHAR wszPins[] = {'P','i','n','s',0};
92 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
93 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
94 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
95 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
96 /* For types registered with IFilterMapper */
97 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
100 /* registry format for REGFILTER2 */
101 struct REG_RF
103 DWORD dwVersion;
104 DWORD dwMerit;
105 DWORD dwPins;
106 DWORD dwUnused;
109 struct REG_RFP
111 BYTE signature[4]; /* e.g. "0pi3" */
112 DWORD dwFlags;
113 DWORD dwInstances;
114 DWORD dwMediaTypes;
115 DWORD dwMediums;
116 DWORD bCategory; /* is there a category clsid? */
117 /* optional: dwOffsetCategoryClsid */
120 struct REG_TYPE
122 BYTE signature[4]; /* e.g. "0ty3" */
123 DWORD dwUnused;
124 DWORD dwOffsetMajor;
125 DWORD dwOffsetMinor;
128 struct MONIKER_MERIT
130 IMoniker * pMoniker;
131 DWORD dwMerit;
134 struct Vector
136 LPBYTE pData;
137 int capacity; /* in bytes */
138 int current; /* pointer to next free byte */
141 /* returns the position it was added at */
142 static int add_data(struct Vector *v, const void *pData, int size)
144 int index = v->current;
145 if (v->current + size > v->capacity)
147 int new_capacity = (v->capacity + size) * 2;
148 BYTE *new_data = CoTaskMemRealloc(v->pData, new_capacity);
149 if (!new_data) return -1;
150 v->capacity = new_capacity;
151 v->pData = new_data;
153 memcpy(v->pData + v->current, pData, size);
154 v->current += size;
155 return index;
158 static int find_data(const struct Vector *v, const void *pData, int size)
160 int index;
161 for (index = 0; index < v->current; index++)
162 if (!memcmp(v->pData + index, pData, size))
163 return index;
164 /* not found */
165 return -1;
168 static void delete_vector(struct Vector * v)
170 CoTaskMemFree(v->pData);
171 v->current = 0;
172 v->capacity = 0;
175 /*** IUnknown (inner) methods ***/
177 static HRESULT WINAPI Inner_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
179 FilterMapper3Impl *This = impl_from_IUnknown(iface);
181 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
183 *ppv = NULL;
184 if (IsEqualIID(riid, &IID_IUnknown))
185 *ppv = &This->IUnknown_inner;
186 else if (IsEqualIID(riid, &IID_IFilterMapper2) || IsEqualIID(riid, &IID_IFilterMapper3))
187 *ppv = &This->IFilterMapper3_iface;
188 else if (IsEqualIID(riid, &IID_IFilterMapper))
189 *ppv = &This->IFilterMapper_iface;
190 else if (IsEqualIID(riid, &IID_IAMFilterData))
191 *ppv = &This->IAMFilterData_iface;
193 if (*ppv != NULL)
195 IUnknown_AddRef((IUnknown *)*ppv);
196 return S_OK;
199 FIXME("No interface for %s\n", debugstr_guid(riid));
200 return E_NOINTERFACE;
203 static ULONG WINAPI Inner_AddRef(IUnknown *iface)
205 FilterMapper3Impl *This = impl_from_IUnknown(iface);
206 ULONG ref = InterlockedIncrement(&This->ref);
208 TRACE("(%p)->(): new ref = %d\n", This, ref);
210 return ref;
213 static ULONG WINAPI Inner_Release(IUnknown *iface)
215 FilterMapper3Impl *This = impl_from_IUnknown(iface);
216 ULONG ref = InterlockedDecrement(&This->ref);
218 TRACE("(%p)->(): new ref = %d\n", This, ref);
220 if (ref == 0)
221 CoTaskMemFree(This);
223 return ref;
226 static const IUnknownVtbl IInner_VTable =
228 Inner_QueryInterface,
229 Inner_AddRef,
230 Inner_Release
233 static HRESULT WINAPI FilterMapper3_QueryInterface(IFilterMapper3 * iface, REFIID riid, LPVOID *ppv)
235 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
237 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
240 static ULONG WINAPI FilterMapper3_AddRef(IFilterMapper3 * iface)
242 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
244 return IUnknown_AddRef(This->outer_unk);
247 static ULONG WINAPI FilterMapper3_Release(IFilterMapper3 * iface)
249 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
251 return IUnknown_Release(This->outer_unk);
254 /*** IFilterMapper3 methods ***/
256 static HRESULT WINAPI FilterMapper3_CreateCategory(
257 IFilterMapper3 * iface,
258 REFCLSID clsidCategory,
259 DWORD dwCategoryMerit,
260 LPCWSTR szDescription)
262 LPWSTR wClsidAMCat = NULL;
263 LPWSTR wClsidCategory = NULL;
264 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + ARRAY_SIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
265 HKEY hKey = NULL;
266 LONG lRet;
267 HRESULT hr;
269 TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
271 hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
273 if (SUCCEEDED(hr))
275 hr = StringFromCLSID(clsidCategory, &wClsidCategory);
278 if (SUCCEEDED(hr))
280 lstrcpyW(wszKeyName, wszClsidSlash);
281 lstrcatW(wszKeyName, wClsidAMCat);
282 lstrcatW(wszKeyName, wszSlashInstance);
283 lstrcatW(wszKeyName, wClsidCategory);
285 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
286 hr = HRESULT_FROM_WIN32(lRet);
289 if (SUCCEEDED(hr))
291 lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (lstrlenW(szDescription) + 1) * sizeof(WCHAR));
292 hr = HRESULT_FROM_WIN32(lRet);
295 if (SUCCEEDED(hr))
297 lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (lstrlenW(wClsidCategory) + 1) * sizeof(WCHAR));
298 hr = HRESULT_FROM_WIN32(lRet);
301 if (SUCCEEDED(hr))
303 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
304 hr = HRESULT_FROM_WIN32(lRet);
307 RegCloseKey(hKey);
308 CoTaskMemFree(wClsidCategory);
309 CoTaskMemFree(wClsidAMCat);
311 return hr;
314 static HRESULT WINAPI FilterMapper3_UnregisterFilter(
315 IFilterMapper3 * iface,
316 const CLSID *pclsidCategory,
317 const OLECHAR *szInstance,
318 REFCLSID Filter)
320 WCHAR wszKeyName[MAX_PATH];
321 LPWSTR wClsidCategory = NULL;
322 LPWSTR wFilter = NULL;
323 HRESULT hr;
325 TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
327 if (!pclsidCategory)
328 pclsidCategory = &CLSID_LegacyAmFilterCategory;
330 hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
332 if (SUCCEEDED(hr))
334 lstrcpyW(wszKeyName, wszClsidSlash);
335 lstrcatW(wszKeyName, wClsidCategory);
336 lstrcatW(wszKeyName, wszSlashInstance);
337 if (szInstance)
338 lstrcatW(wszKeyName, szInstance);
339 else
341 hr = StringFromCLSID(Filter, &wFilter);
342 if (SUCCEEDED(hr))
343 lstrcatW(wszKeyName, wFilter);
347 if (SUCCEEDED(hr))
349 LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
350 hr = HRESULT_FROM_WIN32(lRet);
353 CoTaskMemFree(wClsidCategory);
354 CoTaskMemFree(wFilter);
356 return hr;
359 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
361 VARIANT var;
362 HRESULT ret;
363 BSTR value;
365 V_VT(&var) = VT_BSTR;
366 V_BSTR(&var) = value = SysAllocString(szName);
368 ret = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
369 SysFreeString(value);
371 return ret;
374 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
376 LPWSTR wszClsid = NULL;
377 VARIANT var;
378 HRESULT hr;
380 hr = StringFromCLSID(clsid, &wszClsid);
382 if (SUCCEEDED(hr))
384 V_VT(&var) = VT_BSTR;
385 V_BSTR(&var) = wszClsid;
386 hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
388 CoTaskMemFree(wszClsid);
389 return hr;
392 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
394 int size = sizeof(struct REG_RF);
395 unsigned int i;
396 struct Vector mainStore = {NULL, 0, 0};
397 struct Vector clsidStore = {NULL, 0, 0};
398 struct REG_RF rrf;
399 HRESULT hr = S_OK;
401 rrf.dwVersion = prf2->dwVersion;
402 rrf.dwMerit = prf2->dwMerit;
403 rrf.dwPins = prf2->u.s2.cPins2;
404 rrf.dwUnused = 0;
406 add_data(&mainStore, &rrf, sizeof(rrf));
408 for (i = 0; i < prf2->u.s2.cPins2; i++)
410 size += sizeof(struct REG_RFP);
411 if (prf2->u.s2.rgPins2[i].clsPinCategory)
412 size += sizeof(DWORD);
413 size += prf2->u.s2.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
414 size += prf2->u.s2.rgPins2[i].nMediums * sizeof(DWORD);
417 for (i = 0; i < prf2->u.s2.cPins2; i++)
419 struct REG_RFP rrfp;
420 REGFILTERPINS2 rgPin2 = prf2->u.s2.rgPins2[i];
421 unsigned int j;
423 rrfp.signature[0] = '0';
424 rrfp.signature[1] = 'p';
425 rrfp.signature[2] = 'i';
426 rrfp.signature[3] = '3';
427 rrfp.signature[0] += i;
428 rrfp.dwFlags = rgPin2.dwFlags;
429 rrfp.dwInstances = rgPin2.cInstances;
430 rrfp.dwMediaTypes = rgPin2.nMediaTypes;
431 rrfp.dwMediums = rgPin2.nMediums;
432 rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
434 add_data(&mainStore, &rrfp, sizeof(rrfp));
435 if (rrfp.bCategory)
437 DWORD index = find_data(&clsidStore, rgPin2.clsPinCategory, sizeof(CLSID));
438 if (index == -1)
439 index = add_data(&clsidStore, rgPin2.clsPinCategory, sizeof(CLSID));
440 index += size;
442 add_data(&mainStore, &index, sizeof(index));
445 for (j = 0; j < rgPin2.nMediaTypes; j++)
447 struct REG_TYPE rt;
448 const CLSID * clsMinorType = rgPin2.lpMediaType[j].clsMinorType ? rgPin2.lpMediaType[j].clsMinorType : &MEDIASUBTYPE_NULL;
449 rt.signature[0] = '0';
450 rt.signature[1] = 't';
451 rt.signature[2] = 'y';
452 rt.signature[3] = '3';
453 rt.signature[0] += j;
454 rt.dwUnused = 0;
455 rt.dwOffsetMajor = find_data(&clsidStore, rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
456 if (rt.dwOffsetMajor == -1)
457 rt.dwOffsetMajor = add_data(&clsidStore, rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
458 rt.dwOffsetMajor += size;
459 rt.dwOffsetMinor = find_data(&clsidStore, clsMinorType, sizeof(CLSID));
460 if (rt.dwOffsetMinor == -1)
461 rt.dwOffsetMinor = add_data(&clsidStore, clsMinorType, sizeof(CLSID));
462 rt.dwOffsetMinor += size;
464 add_data(&mainStore, &rt, sizeof(rt));
467 for (j = 0; j < rgPin2.nMediums; j++)
469 DWORD index = find_data(&clsidStore, rgPin2.lpMedium + j, sizeof(REGPINMEDIUM));
470 if (index == -1)
471 index = add_data(&clsidStore, rgPin2.lpMedium + j, sizeof(REGPINMEDIUM));
472 index += size;
474 add_data(&mainStore, &index, sizeof(index));
478 if (SUCCEEDED(hr))
480 *pcbData = mainStore.current + clsidStore.current;
481 *ppData = CoTaskMemAlloc(*pcbData);
482 if (!*ppData)
483 hr = E_OUTOFMEMORY;
486 if (SUCCEEDED(hr))
488 memcpy(*ppData, mainStore.pData, mainStore.current);
489 memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
492 delete_vector(&mainStore);
493 delete_vector(&clsidStore);
494 return hr;
497 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
499 HRESULT hr = S_OK;
500 struct REG_RF * prrf;
501 LPBYTE pCurrent;
502 DWORD i;
503 REGFILTERPINS2 * rgPins2;
505 prrf = (struct REG_RF *)pData;
506 pCurrent = pData;
508 if (prrf->dwVersion != 2)
510 FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
511 ZeroMemory(prf2, sizeof(*prf2));
512 hr = E_FAIL;
515 if (SUCCEEDED(hr))
517 TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
518 prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
520 prf2->dwVersion = prrf->dwVersion;
521 prf2->dwMerit = prrf->dwMerit;
522 prf2->u.s2.cPins2 = prrf->dwPins;
523 rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
524 prf2->u.s2.rgPins2 = rgPins2;
525 pCurrent += sizeof(struct REG_RF);
527 for (i = 0; i < prrf->dwPins; i++)
529 struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
530 REGPINTYPES * lpMediaType;
531 REGPINMEDIUM * lpMedium;
532 UINT j;
534 /* FIXME: check signature */
536 TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
538 TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
539 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
541 rgPins2[i].dwFlags = prrfp->dwFlags;
542 rgPins2[i].cInstances = prrfp->dwInstances;
543 rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
544 rgPins2[i].nMediums = prrfp->dwMediums;
545 pCurrent += sizeof(struct REG_RFP);
546 if (prrfp->bCategory)
548 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
549 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
550 pCurrent += sizeof(DWORD);
551 rgPins2[i].clsPinCategory = clsCat;
553 else
554 rgPins2[i].clsPinCategory = NULL;
556 if (rgPins2[i].nMediaTypes > 0)
557 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
558 else
559 lpMediaType = NULL;
561 rgPins2[i].lpMediaType = lpMediaType;
563 for (j = 0; j < rgPins2[i].nMediaTypes; j++)
565 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
566 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
567 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
569 /* FIXME: check signature */
570 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
572 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
573 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
575 lpMediaType[j].clsMajorType = clsMajor;
576 lpMediaType[j].clsMinorType = clsMinor;
578 pCurrent += sizeof(*prt);
581 if (rgPins2[i].nMediums > 0)
582 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
583 else
584 lpMedium = NULL;
586 rgPins2[i].lpMedium = lpMedium;
588 for (j = 0; j < rgPins2[i].nMediums; j++)
590 DWORD dwOffset = *(DWORD *)pCurrent;
592 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
594 pCurrent += sizeof(dwOffset);
600 return hr;
603 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
605 UINT i;
606 for (i = 0; i < prf2->u.s2.cPins2; i++)
608 UINT j;
609 CoTaskMemFree((void*)prf2->u.s2.rgPins2[i].clsPinCategory);
611 for (j = 0; j < prf2->u.s2.rgPins2[i].nMediaTypes; j++)
613 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMajorType);
614 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMinorType);
616 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType);
617 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMedium);
619 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2);
622 static HRESULT WINAPI FilterMapper3_RegisterFilter(
623 IFilterMapper3 * iface,
624 REFCLSID clsidFilter,
625 LPCWSTR szName,
626 IMoniker **ppMoniker,
627 const CLSID *pclsidCategory,
628 const OLECHAR *szInstance,
629 const REGFILTER2 *prf2)
631 IParseDisplayName * pParser = NULL;
632 IBindCtx * pBindCtx = NULL;
633 IMoniker * pMoniker = NULL;
634 IPropertyBag * pPropBag = NULL;
635 HRESULT hr;
636 LPWSTR pwszParseName = NULL;
637 LPWSTR pCurrent;
638 static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
639 int nameLen;
640 ULONG ulEaten;
641 LPWSTR szClsidTemp = NULL;
642 REGFILTER2 regfilter2;
643 REGFILTERPINS2* pregfp2 = NULL;
645 TRACE("(%s, %s, %p, %s, %s, %p)\n",
646 debugstr_guid(clsidFilter),
647 debugstr_w(szName),
648 ppMoniker,
649 debugstr_guid(pclsidCategory),
650 debugstr_w(szInstance),
651 prf2);
653 if (prf2->dwVersion == 2)
655 regfilter2 = *prf2;
657 else if (prf2->dwVersion == 1)
659 ULONG i;
660 DWORD flags;
661 /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
662 regfilter2.dwVersion = 2;
663 regfilter2.dwMerit = prf2->dwMerit;
664 regfilter2.u.s2.cPins2 = prf2->u.s1.cPins;
665 pregfp2 = CoTaskMemAlloc(prf2->u.s1.cPins * sizeof(REGFILTERPINS2));
666 regfilter2.u.s2.rgPins2 = pregfp2;
667 for (i = 0; i < prf2->u.s1.cPins; i++)
669 flags = 0;
670 if (prf2->u.s1.rgPins[i].bRendered)
671 flags |= REG_PINFLAG_B_RENDERER;
672 if (prf2->u.s1.rgPins[i].bOutput)
673 flags |= REG_PINFLAG_B_OUTPUT;
674 if (prf2->u.s1.rgPins[i].bZero)
675 flags |= REG_PINFLAG_B_ZERO;
676 if (prf2->u.s1.rgPins[i].bMany)
677 flags |= REG_PINFLAG_B_MANY;
678 pregfp2[i].dwFlags = flags;
679 pregfp2[i].cInstances = 1;
680 pregfp2[i].nMediaTypes = prf2->u.s1.rgPins[i].nMediaTypes;
681 pregfp2[i].lpMediaType = prf2->u.s1.rgPins[i].lpMediaType;
682 pregfp2[i].nMediums = 0;
683 pregfp2[i].lpMedium = NULL;
684 pregfp2[i].clsPinCategory = NULL;
687 else
689 FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
690 return E_NOTIMPL;
693 if (ppMoniker)
694 *ppMoniker = NULL;
696 if (!pclsidCategory)
697 /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
698 * In fact this is the CLSID_LegacyAmFilterCategory one */
699 pclsidCategory = &CLSID_LegacyAmFilterCategory;
701 /* sizeof... will include the null terminator and
702 * the + 1 is for the separator ('\\'). The -1 is
703 * because CHARS_IN_GUID includes the null terminator
705 nameLen = ARRAY_SIZE(wszDevice) + CHARS_IN_GUID - 1 + 1;
707 if (szInstance)
708 nameLen += lstrlenW(szInstance);
709 else
710 nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
712 pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
713 if (!pwszParseName)
714 return E_OUTOFMEMORY;
716 lstrcpyW(pwszParseName, wszDevice);
717 pCurrent += lstrlenW(wszDevice);
719 hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
721 if (SUCCEEDED(hr))
723 memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
724 pCurrent += CHARS_IN_GUID - 1;
725 pCurrent[0] = '\\';
727 if (szInstance)
728 lstrcpyW(pCurrent+1, szInstance);
729 else
731 CoTaskMemFree(szClsidTemp);
732 szClsidTemp = NULL;
734 hr = StringFromCLSID(clsidFilter, &szClsidTemp);
735 if (SUCCEEDED(hr))
736 lstrcpyW(pCurrent+1, szClsidTemp);
740 if (SUCCEEDED(hr))
741 hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
743 if (SUCCEEDED(hr))
744 hr = CreateBindCtx(0, &pBindCtx);
746 if (SUCCEEDED(hr))
747 hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
749 if (pBindCtx)
750 IBindCtx_Release(pBindCtx);
751 if (pParser)
752 IParseDisplayName_Release(pParser);
754 if (SUCCEEDED(hr))
755 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
757 if (SUCCEEDED(hr))
758 hr = FM2_WriteFriendlyName(pPropBag, szName);
760 if (SUCCEEDED(hr))
761 hr = FM2_WriteClsid(pPropBag, clsidFilter);
763 if (SUCCEEDED(hr))
765 BYTE *pData;
766 ULONG cbData;
768 hr = FM2_WriteFilterData(&regfilter2, &pData, &cbData);
769 if (SUCCEEDED(hr))
771 VARIANT var;
772 SAFEARRAY *psa;
773 SAFEARRAYBOUND saBound;
775 saBound.lLbound = 0;
776 saBound.cElements = cbData;
777 psa = SafeArrayCreate(VT_UI1, 1, &saBound);
778 if (!psa)
780 ERR("Couldn't create SAFEARRAY\n");
781 hr = E_FAIL;
784 if (SUCCEEDED(hr))
786 LPBYTE pbSAData;
787 hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
788 if (SUCCEEDED(hr))
790 memcpy(pbSAData, pData, cbData);
791 hr = SafeArrayUnaccessData(psa);
795 V_VT(&var) = VT_ARRAY | VT_UI1;
796 V_ARRAY(&var) = psa;
798 if (SUCCEEDED(hr))
799 hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
801 if (psa)
802 SafeArrayDestroy(psa);
803 CoTaskMemFree(pData);
807 if (pPropBag)
808 IPropertyBag_Release(pPropBag);
809 CoTaskMemFree(szClsidTemp);
810 CoTaskMemFree(pwszParseName);
812 if (SUCCEEDED(hr) && ppMoniker)
813 *ppMoniker = pMoniker;
814 else if (pMoniker)
815 IMoniker_Release(pMoniker);
817 CoTaskMemFree(pregfp2);
819 TRACE("-- returning %x\n", hr);
821 return hr;
824 /* internal helper function */
825 static BOOL MatchTypes(
826 BOOL bExactMatch,
827 DWORD nPinTypes,
828 const REGPINTYPES * pPinTypes,
829 DWORD nMatchTypes,
830 const GUID * pMatchTypes)
832 BOOL bMatch = FALSE;
833 DWORD j;
835 if ((nMatchTypes == 0) && (nPinTypes > 0))
836 bMatch = TRUE;
838 for (j = 0; j < nPinTypes; j++)
840 DWORD i;
841 for (i = 0; i < nMatchTypes*2; i+=2)
843 if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
844 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
846 bMatch = TRUE;
847 break;
851 return bMatch;
854 /* internal helper function for qsort of MONIKER_MERIT array */
855 static int __cdecl mm_compare(const void * left, const void * right)
857 const struct MONIKER_MERIT * mmLeft = left;
858 const struct MONIKER_MERIT * mmRight = right;
860 if (mmLeft->dwMerit == mmRight->dwMerit)
861 return 0;
862 if (mmLeft->dwMerit > mmRight->dwMerit)
863 return -1;
864 return 1;
867 /* NOTES:
868 * Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
869 * (GUID_NULL's in input to function automatically treated as wild cards)
870 * Input/Output needed means match only on criteria if TRUE (with zero input types
871 * meaning match any input/output pin as long as one exists), otherwise match any
872 * filter that meets the rest of the requirements.
874 static HRESULT WINAPI FilterMapper3_EnumMatchingFilters(
875 IFilterMapper3 * iface,
876 IEnumMoniker **ppEnum,
877 DWORD dwFlags,
878 BOOL bExactMatch,
879 DWORD dwMerit,
880 BOOL bInputNeeded,
881 DWORD cInputTypes,
882 const GUID *pInputTypes,
883 const REGPINMEDIUM *pMedIn,
884 const CLSID *pPinCategoryIn,
885 BOOL bRender,
886 BOOL bOutputNeeded,
887 DWORD cOutputTypes,
888 const GUID *pOutputTypes,
889 const REGPINMEDIUM *pMedOut,
890 const CLSID *pPinCategoryOut)
892 ICreateDevEnum * pCreateDevEnum;
893 IMoniker * pMonikerCat;
894 IEnumMoniker * pEnumCat;
895 HRESULT hr;
896 struct Vector monikers = {NULL, 0, 0};
898 TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
899 ppEnum,
900 dwFlags,
901 bExactMatch ? "true" : "false",
902 dwMerit,
903 bInputNeeded ? "true" : "false",
904 cInputTypes,
905 pInputTypes,
906 pMedIn,
907 pPinCategoryIn,
908 bRender ? "true" : "false",
909 bOutputNeeded ? "true" : "false",
910 pOutputTypes,
911 pMedOut,
912 pPinCategoryOut);
914 if (dwFlags != 0)
916 FIXME("dwFlags = %x not implemented\n", dwFlags);
919 *ppEnum = NULL;
921 hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
922 if (FAILED(hr))
923 return hr;
925 hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
926 if (FAILED(hr)) {
927 ICreateDevEnum_Release(pCreateDevEnum);
928 return hr;
931 while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
933 IPropertyBag * pPropBagCat = NULL;
934 VARIANT var;
935 HRESULT hrSub; /* this is so that one buggy filter
936 doesn't make the whole lot fail */
938 VariantInit(&var);
940 hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
942 if (SUCCEEDED(hrSub))
943 hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
945 if (SUCCEEDED(hrSub) && (V_UI4(&var) >= dwMerit))
947 CLSID clsidCat;
948 IEnumMoniker * pEnum;
949 IMoniker * pMoniker;
951 VariantClear(&var);
953 if (TRACE_ON(quartz))
955 VARIANT temp;
956 V_VT(&temp) = VT_EMPTY;
957 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
958 TRACE("Considering category %s\n", debugstr_w(V_BSTR(&temp)));
959 VariantClear(&temp);
962 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
964 if (SUCCEEDED(hrSub))
965 hrSub = CLSIDFromString(V_BSTR(&var), &clsidCat);
967 if (SUCCEEDED(hrSub))
968 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
970 if (hrSub == S_OK)
972 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
974 IPropertyBag * pPropBag = NULL;
975 VARIANT var;
976 BYTE *pData = NULL;
977 REGFILTER2 rf2;
978 DWORD i;
979 BOOL bInputMatch = !bInputNeeded;
980 BOOL bOutputMatch = !bOutputNeeded;
982 ZeroMemory(&rf2, sizeof(rf2));
983 VariantInit(&var);
985 hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
987 if (TRACE_ON(quartz))
989 VARIANT temp;
990 V_VT(&temp) = VT_EMPTY;
991 IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
992 TRACE("Considering filter %s\n", debugstr_w(V_BSTR(&temp)));
993 VariantClear(&temp);
996 if (SUCCEEDED(hrSub))
998 hrSub = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
1001 if (SUCCEEDED(hrSub))
1002 hrSub = SafeArrayAccessData(V_ARRAY(&var), (LPVOID*)&pData);
1004 if (SUCCEEDED(hrSub))
1005 hrSub = FM2_ReadFilterData(pData, &rf2);
1007 if (pData)
1008 SafeArrayUnaccessData(V_ARRAY(&var));
1010 VariantClear(&var);
1012 /* Logic used for bInputMatch expression:
1013 * There exists some pin such that bInputNeeded implies (pin is an input and
1014 * (bRender implies pin has render flag) and major/minor types members of
1015 * pInputTypes )
1016 * bOutputMatch is similar, but without the "bRender implies ..." part
1017 * and substituting variables names containing input for output
1020 /* determine whether filter meets requirements */
1021 if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
1023 for (i = 0; (i < rf2.u.s2.cPins2) && (!bInputMatch || !bOutputMatch); i++)
1025 const REGFILTERPINS2 * rfp2 = rf2.u.s2.rgPins2 + i;
1027 bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1028 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
1029 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
1030 bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1031 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
1034 if (bInputMatch && bOutputMatch)
1036 struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1037 IMoniker_AddRef(pMoniker);
1038 add_data(&monikers, &mm, sizeof(mm));
1042 FM2_DeleteRegFilter(&rf2);
1043 if (pPropBag)
1044 IPropertyBag_Release(pPropBag);
1045 IMoniker_Release(pMoniker);
1047 IEnumMoniker_Release(pEnum);
1051 VariantClear(&var);
1052 if (pPropBagCat)
1053 IPropertyBag_Release(pPropBagCat);
1054 IMoniker_Release(pMonikerCat);
1057 if (SUCCEEDED(hr))
1059 IMoniker ** ppMoniker;
1060 unsigned int i;
1061 ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1063 /* sort the monikers in descending merit order */
1064 qsort(monikers.pData, nMonikerCount,
1065 sizeof(struct MONIKER_MERIT),
1066 mm_compare);
1068 /* construct an IEnumMoniker interface */
1069 ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1070 for (i = 0; i < nMonikerCount; i++)
1072 /* no need to AddRef here as already AddRef'd above */
1073 ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1075 hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1076 CoTaskMemFree(ppMoniker);
1079 delete_vector(&monikers);
1080 IEnumMoniker_Release(pEnumCat);
1081 ICreateDevEnum_Release(pCreateDevEnum);
1083 return hr;
1086 static HRESULT WINAPI FilterMapper3_GetICreateDevEnum(IFilterMapper3 *iface, ICreateDevEnum **ppEnum)
1088 TRACE("(%p, %p)\n", iface, ppEnum);
1089 if (!ppEnum)
1090 return E_POINTER;
1091 return CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (void**)ppEnum);
1094 static const IFilterMapper3Vtbl fm3vtbl =
1097 FilterMapper3_QueryInterface,
1098 FilterMapper3_AddRef,
1099 FilterMapper3_Release,
1101 FilterMapper3_CreateCategory,
1102 FilterMapper3_UnregisterFilter,
1103 FilterMapper3_RegisterFilter,
1104 FilterMapper3_EnumMatchingFilters,
1105 FilterMapper3_GetICreateDevEnum
1108 /*** IUnknown methods ***/
1110 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1112 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1114 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1116 return FilterMapper3_QueryInterface(&This->IFilterMapper3_iface, riid, ppv);
1119 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1121 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1123 return IUnknown_AddRef(This->outer_unk);
1126 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1128 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1130 return IUnknown_Release(This->outer_unk);
1133 /*** IFilterMapper methods ***/
1135 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1136 IFilterMapper * iface,
1137 IEnumRegFilters **ppEnum,
1138 DWORD dwMerit,
1139 BOOL bInputNeeded,
1140 CLSID clsInMaj,
1141 CLSID clsInSub,
1142 BOOL bRender,
1143 BOOL bOutputNeeded,
1144 CLSID clsOutMaj,
1145 CLSID clsOutSub)
1147 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1148 GUID InputType[2];
1149 GUID OutputType[2];
1150 IEnumMoniker* ppEnumMoniker;
1151 IMoniker* IMon;
1152 ULONG nb;
1153 ULONG idx = 0, nb_mon = 0;
1154 REGFILTER* regfilters;
1155 HRESULT hr;
1157 TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s)\n",
1158 This,
1159 iface,
1160 ppEnum,
1161 dwMerit,
1162 bInputNeeded ? "true" : "false",
1163 debugstr_guid(&clsInMaj),
1164 debugstr_guid(&clsInSub),
1165 bRender ? "true" : "false",
1166 bOutputNeeded ? "true" : "false",
1167 debugstr_guid(&clsOutMaj),
1168 debugstr_guid(&clsOutSub));
1170 InputType[0] = clsInMaj;
1171 InputType[1] = clsInSub;
1172 OutputType[0] = clsOutMaj;
1173 OutputType[1] = clsOutSub;
1175 *ppEnum = NULL;
1177 hr = IFilterMapper3_EnumMatchingFilters(&This->IFilterMapper3_iface, &ppEnumMoniker, 0, TRUE,
1178 dwMerit, bInputNeeded, 1, InputType, NULL, &GUID_NULL, bRender, bOutputNeeded, 1,
1179 OutputType, NULL, &GUID_NULL);
1181 if (FAILED(hr))
1182 return hr;
1184 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1186 IMoniker_Release(IMon);
1187 nb_mon++;
1190 if (!nb_mon)
1192 IEnumMoniker_Release(ppEnumMoniker);
1193 return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1196 regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1197 if (!regfilters)
1199 IEnumMoniker_Release(ppEnumMoniker);
1200 return E_OUTOFMEMORY;
1202 ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1204 IEnumMoniker_Reset(ppEnumMoniker);
1205 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1207 IPropertyBag * pPropBagCat = NULL;
1208 VARIANT var;
1209 HRESULT hrSub;
1210 GUID clsid;
1211 int len;
1213 VariantInit(&var);
1215 hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1217 if (SUCCEEDED(hrSub))
1218 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1220 if (SUCCEEDED(hrSub))
1221 hrSub = CLSIDFromString(V_BSTR(&var), &clsid);
1223 VariantClear(&var);
1225 if (SUCCEEDED(hrSub))
1226 hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1228 if (SUCCEEDED(hrSub))
1230 len = (lstrlenW(V_BSTR(&var))+1) * sizeof(WCHAR);
1231 if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1232 hr = E_OUTOFMEMORY;
1235 if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1237 memcpy(regfilters[idx].Name, V_BSTR(&var), len);
1238 regfilters[idx].Clsid = clsid;
1239 idx++;
1242 if (pPropBagCat)
1243 IPropertyBag_Release(pPropBagCat);
1244 IMoniker_Release(IMon);
1245 VariantClear(&var);
1248 if (SUCCEEDED(hr))
1250 hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1253 for (idx = 0; idx < nb_mon; idx++)
1254 CoTaskMemFree(regfilters[idx].Name);
1255 CoTaskMemFree(regfilters);
1256 IEnumMoniker_Release(ppEnumMoniker);
1258 return hr;
1262 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1264 HRESULT hr;
1265 LPWSTR wszClsid = NULL;
1266 HKEY hKey;
1267 LONG lRet;
1268 WCHAR wszKeyName[ARRAY_SIZE(wszFilterSlash)-1 + (CHARS_IN_GUID-1) + 1];
1270 TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1272 hr = StringFromCLSID(&clsid, &wszClsid);
1274 if (SUCCEEDED(hr))
1276 lstrcpyW(wszKeyName, wszFilterSlash);
1277 lstrcatW(wszKeyName, wszClsid);
1279 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1280 hr = HRESULT_FROM_WIN32(lRet);
1283 if (SUCCEEDED(hr))
1285 lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (lstrlenW(szName) + 1) * sizeof(WCHAR));
1286 hr = HRESULT_FROM_WIN32(lRet);
1287 RegCloseKey(hKey);
1290 if (SUCCEEDED(hr))
1292 lstrcpyW(wszKeyName, wszClsidSlash);
1293 lstrcatW(wszKeyName, wszClsid);
1295 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1296 hr = HRESULT_FROM_WIN32(lRet);
1299 if (SUCCEEDED(hr))
1301 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1302 hr = HRESULT_FROM_WIN32(lRet);
1303 RegCloseKey(hKey);
1306 CoTaskMemFree(wszClsid);
1308 return hr;
1311 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1313 TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1315 /* Not implemented in Windows (tested on Win2k) */
1317 return E_NOTIMPL;
1320 static HRESULT WINAPI FilterMapper_RegisterPin(
1321 IFilterMapper * iface,
1322 CLSID Filter,
1323 LPCWSTR szName,
1324 BOOL bRendered,
1325 BOOL bOutput,
1326 BOOL bZero,
1327 BOOL bMany,
1328 CLSID ConnectsToFilter,
1329 LPCWSTR ConnectsToPin)
1331 HRESULT hr;
1332 LONG lRet;
1333 LPWSTR wszClsid = NULL;
1334 HKEY hKey = NULL;
1335 HKEY hPinsKey = NULL;
1336 WCHAR * wszPinsKeyName;
1337 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1339 TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1340 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1342 hr = StringFromCLSID(&Filter, &wszClsid);
1344 if (SUCCEEDED(hr))
1346 lstrcpyW(wszKeyName, wszClsidSlash);
1347 lstrcatW(wszKeyName, wszClsid);
1349 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1350 hr = HRESULT_FROM_WIN32(lRet);
1353 if (SUCCEEDED(hr))
1355 wszPinsKeyName = CoTaskMemAlloc((lstrlenW(wszPins) + 1 + lstrlenW(szName) + 1) * 2);
1356 if (!wszPinsKeyName)
1357 hr = E_OUTOFMEMORY;
1360 if (SUCCEEDED(hr))
1362 lstrcpyW(wszPinsKeyName, wszPins);
1363 lstrcatW(wszPinsKeyName, wszSlash);
1364 lstrcatW(wszPinsKeyName, szName);
1366 lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1367 hr = HRESULT_FROM_WIN32(lRet);
1368 CoTaskMemFree(wszPinsKeyName);
1371 if (SUCCEEDED(hr))
1373 lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1374 hr = HRESULT_FROM_WIN32(lRet);
1377 if (SUCCEEDED(hr))
1379 lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1380 hr = HRESULT_FROM_WIN32(lRet);
1383 if (SUCCEEDED(hr))
1385 lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1386 hr = HRESULT_FROM_WIN32(lRet);
1389 if (SUCCEEDED(hr))
1391 lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1392 hr = HRESULT_FROM_WIN32(lRet);
1395 if (SUCCEEDED(hr))
1397 HKEY hkeyDummy = NULL;
1399 lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1400 hr = HRESULT_FROM_WIN32(lRet);
1402 if (hkeyDummy) RegCloseKey(hkeyDummy);
1405 CoTaskMemFree(wszClsid);
1406 if (hKey)
1407 RegCloseKey(hKey);
1408 if (hPinsKey)
1409 RegCloseKey(hPinsKey);
1411 return hr;
1415 static HRESULT WINAPI FilterMapper_RegisterPinType(
1416 IFilterMapper * iface,
1417 CLSID clsFilter,
1418 LPCWSTR szName,
1419 CLSID clsMajorType,
1420 CLSID clsSubType)
1422 HRESULT hr;
1423 LONG lRet;
1424 LPWSTR wszClsid = NULL;
1425 LPWSTR wszClsidMajorType = NULL;
1426 LPWSTR wszClsidSubType = NULL;
1427 HKEY hKey = NULL;
1428 WCHAR * wszTypesKey;
1429 WCHAR wszKeyName[MAX_PATH];
1431 TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1432 debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1434 hr = StringFromCLSID(&clsFilter, &wszClsid);
1436 if (SUCCEEDED(hr))
1438 hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1441 if (SUCCEEDED(hr))
1443 hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1446 if (SUCCEEDED(hr))
1448 wszTypesKey = CoTaskMemAlloc((lstrlenW(wszClsidSlash) + lstrlenW(wszClsid) + lstrlenW(wszPins) +
1449 lstrlenW(szName) + lstrlenW(wszTypes) + 3 + 1) * 2);
1450 if (!wszTypesKey)
1451 hr = E_OUTOFMEMORY;
1454 if (SUCCEEDED(hr))
1456 lstrcpyW(wszTypesKey, wszClsidSlash);
1457 lstrcatW(wszTypesKey, wszClsid);
1458 lstrcatW(wszTypesKey, wszSlash);
1459 lstrcatW(wszTypesKey, wszPins);
1460 lstrcatW(wszTypesKey, wszSlash);
1461 lstrcatW(wszTypesKey, szName);
1462 lstrcatW(wszTypesKey, wszSlash);
1463 lstrcatW(wszTypesKey, wszTypes);
1465 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1466 hr = HRESULT_FROM_WIN32(lRet);
1467 CoTaskMemFree(wszTypesKey);
1470 if (SUCCEEDED(hr))
1472 HKEY hkeyDummy = NULL;
1474 lstrcpyW(wszKeyName, wszClsidMajorType);
1475 lstrcatW(wszKeyName, wszSlash);
1476 lstrcatW(wszKeyName, wszClsidSubType);
1478 lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1479 hr = HRESULT_FROM_WIN32(lRet);
1480 RegCloseKey(hKey);
1482 if (hkeyDummy) RegCloseKey(hkeyDummy);
1485 CoTaskMemFree(wszClsid);
1486 CoTaskMemFree(wszClsidMajorType);
1487 CoTaskMemFree(wszClsidSubType);
1489 return hr;
1492 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1494 HRESULT hr;
1495 LONG lRet;
1496 LPWSTR wszClsid = NULL;
1497 HKEY hKey;
1498 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1500 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1502 hr = StringFromCLSID(&Filter, &wszClsid);
1504 if (SUCCEEDED(hr))
1506 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1507 hr = HRESULT_FROM_WIN32(lRet);
1510 if (SUCCEEDED(hr))
1512 lRet = RegDeleteKeyW(hKey, wszClsid);
1513 hr = HRESULT_FROM_WIN32(lRet);
1514 RegCloseKey(hKey);
1517 if (SUCCEEDED(hr))
1519 lstrcpyW(wszKeyName, wszClsidSlash);
1520 lstrcatW(wszKeyName, wszClsid);
1522 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1523 if (lRet == ERROR_FILE_NOT_FOUND)
1524 goto done;
1525 hr = HRESULT_FROM_WIN32(lRet);
1528 if (SUCCEEDED(hr))
1530 lRet = RegDeleteValueW(hKey, wszMeritName);
1531 if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
1532 hr = HRESULT_FROM_WIN32(lRet);
1534 lRet = RegDeleteTreeW(hKey, wszPins);
1535 if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
1536 hr = HRESULT_FROM_WIN32(lRet);
1538 RegCloseKey(hKey);
1541 done:
1542 CoTaskMemFree(wszClsid);
1544 return hr;
1547 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1549 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1551 /* Not implemented in Windows (tested on Win2k) */
1553 return E_NOTIMPL;
1556 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1558 HRESULT hr;
1559 LONG lRet;
1560 LPWSTR wszClsid = NULL;
1561 HKEY hKey = NULL;
1562 WCHAR * wszPinNameKey;
1563 WCHAR wszKeyName[ARRAY_SIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1565 TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1567 if (!Name)
1568 return E_INVALIDARG;
1570 hr = StringFromCLSID(&Filter, &wszClsid);
1572 if (SUCCEEDED(hr))
1574 lstrcpyW(wszKeyName, wszClsidSlash);
1575 lstrcatW(wszKeyName, wszClsid);
1577 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1578 hr = HRESULT_FROM_WIN32(lRet);
1581 if (SUCCEEDED(hr))
1583 wszPinNameKey = CoTaskMemAlloc((lstrlenW(wszPins) + 1 + lstrlenW(Name) + 1) * 2);
1584 if (!wszPinNameKey)
1585 hr = E_OUTOFMEMORY;
1588 if (SUCCEEDED(hr))
1590 lstrcpyW(wszPinNameKey, wszPins);
1591 lstrcatW(wszPinNameKey, wszSlash);
1592 lstrcatW(wszPinNameKey, Name);
1594 lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1595 hr = HRESULT_FROM_WIN32(lRet);
1596 CoTaskMemFree(wszPinNameKey);
1599 CoTaskMemFree(wszClsid);
1600 if (hKey)
1601 RegCloseKey(hKey);
1603 return hr;
1606 static const IFilterMapperVtbl fmvtbl =
1609 FilterMapper_QueryInterface,
1610 FilterMapper_AddRef,
1611 FilterMapper_Release,
1613 FilterMapper_RegisterFilter,
1614 FilterMapper_RegisterFilterInstance,
1615 FilterMapper_RegisterPin,
1616 FilterMapper_RegisterPinType,
1617 FilterMapper_UnregisterFilter,
1618 FilterMapper_UnregisterFilterInstance,
1619 FilterMapper_UnregisterPin,
1620 FilterMapper_EnumMatchingFilters
1624 /*** IUnknown methods ***/
1625 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1627 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1629 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1632 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1634 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1636 return IUnknown_AddRef(This->outer_unk);
1639 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1641 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1643 return IUnknown_Release(This->outer_unk);
1646 /*** IAMFilterData methods ***/
1647 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1648 BYTE *pData, ULONG cb,
1649 BYTE **ppRegFilter2)
1651 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1652 HRESULT hr = S_OK;
1653 static REGFILTER2 *prf2;
1655 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1657 prf2 = CoTaskMemAlloc(sizeof(*prf2));
1658 if (!prf2)
1659 return E_OUTOFMEMORY;
1660 *ppRegFilter2 = (BYTE *)&prf2;
1662 hr = FM2_ReadFilterData(pData, prf2);
1663 if (FAILED(hr))
1665 CoTaskMemFree(prf2);
1666 *ppRegFilter2 = NULL;
1669 return hr;
1672 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1673 REGFILTER2 *prf2,
1674 BYTE **pRegFilterData,
1675 ULONG *pcb)
1677 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1679 TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1681 return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1684 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1685 AMFilterData_QueryInterface,
1686 AMFilterData_AddRef,
1687 AMFilterData_Release,
1688 AMFilterData_ParseFilterData,
1689 AMFilterData_CreateFilterData
1692 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1694 FilterMapper3Impl * pFM2impl;
1696 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1698 pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
1699 if (!pFM2impl)
1700 return E_OUTOFMEMORY;
1702 pFM2impl->IUnknown_inner.lpVtbl = &IInner_VTable;
1703 pFM2impl->IFilterMapper3_iface.lpVtbl = &fm3vtbl;
1704 pFM2impl->IFilterMapper_iface.lpVtbl = &fmvtbl;
1705 pFM2impl->IAMFilterData_iface.lpVtbl = &AMFilterDataVtbl;
1706 pFM2impl->ref = 1;
1708 if (pUnkOuter)
1709 pFM2impl->outer_unk = pUnkOuter;
1710 else
1711 pFM2impl->outer_unk = &pFM2impl->IUnknown_inner;
1713 *ppObj = &pFM2impl->IUnknown_inner;
1715 TRACE("-- created at %p\n", pFM2impl);
1717 return S_OK;
1720 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1722 FilterMapper3Impl *pFM2impl;
1723 HRESULT hr;
1725 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1727 hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
1728 if (FAILED(hr))
1729 return hr;
1731 *ppObj = &pFM2impl->IFilterMapper_iface;
1733 return hr;