quartz: Add directsound renderer to filter list.
[wine] / dlls / quartz / regsvr.c
1 /*
2  *      self-registerable dll functions for quartz.dll
3  *
4  * Copyright (C) 2003 John K. Hohm
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #define COBJMACROS
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "winerror.h"
32
33 #include "ole2.h"
34 #include "uuids.h"
35 #include "strmif.h"
36
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
40
41 /*
42  * Near the bottom of this file are the exported DllRegisterServer and
43  * DllUnregisterServer, which make all this worthwhile.
44  */
45
46 /***********************************************************************
47  *              interface for self-registering
48  */
49 struct regsvr_interface
50 {
51     IID const *iid;             /* NULL for end of list */
52     LPCSTR name;                /* can be NULL to omit */
53     IID const *base_iid;        /* can be NULL to omit */
54     int num_methods;            /* can be <0 to omit */
55     CLSID const *ps_clsid;      /* can be NULL to omit */
56     CLSID const *ps_clsid32;    /* can be NULL to omit */
57 };
58
59 static HRESULT register_interfaces(struct regsvr_interface const *list);
60 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
61
62 struct regsvr_coclass
63 {
64     CLSID const *clsid;         /* NULL for end of list */
65     LPCSTR name;                /* can be NULL to omit */
66     LPCSTR ips;                 /* can be NULL to omit */
67     LPCSTR ips32;               /* can be NULL to omit */
68     LPCSTR ips32_tmodel;        /* can be NULL to omit */
69     LPCSTR progid;              /* can be NULL to omit */
70     LPCSTR viprogid;            /* can be NULL to omit */
71     LPCSTR progid_extra;        /* can be NULL to omit */
72 };
73
74 static HRESULT register_coclasses(struct regsvr_coclass const *list);
75 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
76
77 struct regsvr_mediatype_parsing
78 {
79     CLSID const *majortype;     /* NULL for end of list */
80     CLSID const *subtype;
81     LPCSTR line[11];            /* NULL for end of list */
82 };
83
84 static HRESULT register_mediatypes_parsing(struct regsvr_mediatype_parsing const *list);
85 static HRESULT unregister_mediatypes_parsing(struct regsvr_mediatype_parsing const *list);
86
87 struct regsvr_mediatype_extension
88 {
89     CLSID const *majortype;     /* NULL for end of list */
90     CLSID const *subtype;
91     LPCSTR extension;
92 };
93
94 struct mediatype
95 {
96     CLSID const *majortype;     /* NULL for end of list */
97     CLSID const *subtype;
98     DWORD fourcc;
99 };
100
101 struct pin
102 {
103     DWORD flags;                /* 0xFFFFFFFF for end of list */
104     struct mediatype mediatypes[11];
105 };
106
107 struct regsvr_filter
108 {
109     CLSID const *clsid;         /* NULL for end of list */
110     CLSID const *category;
111     WCHAR name[50];
112     DWORD merit;
113     struct pin pins[11];
114 };
115
116 static HRESULT register_mediatypes_extension(struct regsvr_mediatype_extension const *list);
117 static HRESULT unregister_mediatypes_extension(struct regsvr_mediatype_extension const *list);
118
119 static HRESULT register_filters(struct regsvr_filter const *list);
120 static HRESULT unregister_filters(struct regsvr_filter const *list);
121
122 /***********************************************************************
123  *              static string constants
124  */
125 static WCHAR const interface_keyname[10] = {
126     'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
127 static WCHAR const base_ifa_keyname[14] = {
128     'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
129     'e', 0 };
130 static WCHAR const num_methods_keyname[11] = {
131     'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
132 static WCHAR const ps_clsid_keyname[15] = {
133     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
134     'i', 'd', 0 };
135 static WCHAR const ps_clsid32_keyname[17] = {
136     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
137     'i', 'd', '3', '2', 0 };
138 static WCHAR const clsid_keyname[6] = {
139     'C', 'L', 'S', 'I', 'D', 0 };
140 static WCHAR const curver_keyname[7] = {
141     'C', 'u', 'r', 'V', 'e', 'r', 0 };
142 static WCHAR const ips_keyname[13] = {
143     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
144     0 };
145 static WCHAR const ips32_keyname[15] = {
146     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
147     '3', '2', 0 };
148 static WCHAR const progid_keyname[7] = {
149     'P', 'r', 'o', 'g', 'I', 'D', 0 };
150 static WCHAR const viprogid_keyname[25] = {
151     'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
152     'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
153     0 };
154 static char const tmodel_valuename[] = "ThreadingModel";
155 static WCHAR const mediatype_name[11] = {
156     'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
157 static WCHAR const subtype_valuename[8] = {
158     'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
159 static WCHAR const sourcefilter_valuename[14] = {
160     'S', 'o', 'u', 'r', 'c', 'e', ' ', 'F', 'i', 'l', 't', 'e', 'r', 0 };
161 static WCHAR const extensions_keyname[11] = {
162     'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 's', 0 };
163
164 /***********************************************************************
165  *              static helper functions
166  */
167 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
168 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
169                                    WCHAR const *value);
170 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
171                                    char const *value);
172 static LONG register_progid(WCHAR const *clsid,
173                             char const *progid, char const *curver_progid,
174                             char const *name, char const *extra);
175
176 /***********************************************************************
177  *              register_interfaces
178  */
179 static HRESULT register_interfaces(struct regsvr_interface const *list)
180 {
181     LONG res = ERROR_SUCCESS;
182     HKEY interface_key;
183
184     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
185                           KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
186     if (res != ERROR_SUCCESS) goto error_return;
187
188     for (; res == ERROR_SUCCESS && list->iid; ++list) {
189         WCHAR buf[39];
190         HKEY iid_key;
191
192         StringFromGUID2(list->iid, buf, 39);
193         res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
194                               KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
195         if (res != ERROR_SUCCESS) goto error_close_interface_key;
196
197         if (list->name) {
198             res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
199                                  (CONST BYTE*)(list->name),
200                                  strlen(list->name) + 1);
201             if (res != ERROR_SUCCESS) goto error_close_iid_key;
202         }
203
204         if (list->base_iid) {
205             res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
206             if (res != ERROR_SUCCESS) goto error_close_iid_key;
207         }
208
209         if (0 <= list->num_methods) {
210             static WCHAR const fmt[3] = { '%', 'd', 0 };
211             HKEY key;
212
213             res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
214                                   KEY_READ | KEY_WRITE, NULL, &key, NULL);
215             if (res != ERROR_SUCCESS) goto error_close_iid_key;
216
217             wsprintfW(buf, fmt, list->num_methods);
218             res = RegSetValueExW(key, NULL, 0, REG_SZ,
219                                  (CONST BYTE*)buf,
220                                  (lstrlenW(buf) + 1) * sizeof(WCHAR));
221             RegCloseKey(key);
222
223             if (res != ERROR_SUCCESS) goto error_close_iid_key;
224         }
225
226         if (list->ps_clsid) {
227             res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
228             if (res != ERROR_SUCCESS) goto error_close_iid_key;
229         }
230
231         if (list->ps_clsid32) {
232             res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
233             if (res != ERROR_SUCCESS) goto error_close_iid_key;
234         }
235
236     error_close_iid_key:
237         RegCloseKey(iid_key);
238     }
239
240 error_close_interface_key:
241     RegCloseKey(interface_key);
242 error_return:
243     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
244 }
245
246 /***********************************************************************
247  *              unregister_interfaces
248  */
249 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
250 {
251     LONG res = ERROR_SUCCESS;
252     HKEY interface_key;
253
254     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
255                         KEY_READ | KEY_WRITE, &interface_key);
256     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
257     if (res != ERROR_SUCCESS) goto error_return;
258
259     for (; res == ERROR_SUCCESS && list->iid; ++list) {
260         WCHAR buf[39];
261
262         StringFromGUID2(list->iid, buf, 39);
263         res = RegDeleteTreeW(interface_key, buf);
264         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
265     }
266
267     RegCloseKey(interface_key);
268 error_return:
269     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
270 }
271
272 /***********************************************************************
273  *              register_coclasses
274  */
275 static HRESULT register_coclasses(struct regsvr_coclass const *list)
276 {
277     LONG res = ERROR_SUCCESS;
278     HKEY coclass_key;
279
280     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
281                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
282     if (res != ERROR_SUCCESS) goto error_return;
283
284     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
285         WCHAR buf[39];
286         HKEY clsid_key;
287
288         StringFromGUID2(list->clsid, buf, 39);
289         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
290                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
291         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
292
293         if (list->name) {
294             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
295                                  (CONST BYTE*)(list->name),
296                                  strlen(list->name) + 1);
297             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
298         }
299
300         if (list->ips) {
301             res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
302             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
303         }
304
305         if (list->ips32) {
306             HKEY ips32_key;
307
308             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
309                                   KEY_READ | KEY_WRITE, NULL,
310                                   &ips32_key, NULL);
311             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
312
313             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
314                                  (CONST BYTE*)list->ips32,
315                                  lstrlenA(list->ips32) + 1);
316             if (res == ERROR_SUCCESS && list->ips32_tmodel)
317                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
318                                      (CONST BYTE*)list->ips32_tmodel,
319                                      strlen(list->ips32_tmodel) + 1);
320             RegCloseKey(ips32_key);
321             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
322         }
323
324         if (list->progid) {
325             res = register_key_defvalueA(clsid_key, progid_keyname,
326                                          list->progid);
327             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
328
329             res = register_progid(buf, list->progid, NULL,
330                                   list->name, list->progid_extra);
331             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
332         }
333
334         if (list->viprogid) {
335             res = register_key_defvalueA(clsid_key, viprogid_keyname,
336                                          list->viprogid);
337             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
338
339             res = register_progid(buf, list->viprogid, list->progid,
340                                   list->name, list->progid_extra);
341             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
342         }
343
344     error_close_clsid_key:
345         RegCloseKey(clsid_key);
346     }
347
348 error_close_coclass_key:
349     RegCloseKey(coclass_key);
350 error_return:
351     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
352 }
353
354 /***********************************************************************
355  *              unregister_coclasses
356  */
357 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
358 {
359     LONG res = ERROR_SUCCESS;
360     HKEY coclass_key;
361
362     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
363                         KEY_READ | KEY_WRITE, &coclass_key);
364     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
365     if (res != ERROR_SUCCESS) goto error_return;
366
367     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
368         WCHAR buf[39];
369
370         StringFromGUID2(list->clsid, buf, 39);
371         res = RegDeleteTreeW(coclass_key, buf);
372         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
373         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
374
375         if (list->progid) {
376             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
377             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
378             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
379         }
380
381         if (list->viprogid) {
382             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->viprogid);
383             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
384             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
385         }
386     }
387
388 error_close_coclass_key:
389     RegCloseKey(coclass_key);
390 error_return:
391     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
392 }
393
394 /***********************************************************************
395  *              register_mediatypes_parsing
396  */
397 static HRESULT register_mediatypes_parsing(struct regsvr_mediatype_parsing const *list)
398 {
399     LONG res = ERROR_SUCCESS;
400     HKEY mediatype_key;
401     WCHAR buf[39];
402     int i;
403
404     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, mediatype_name, 0, NULL, 0,
405                           KEY_READ | KEY_WRITE, NULL, &mediatype_key, NULL);
406     if (res != ERROR_SUCCESS) return HRESULT_FROM_WIN32(res);
407
408     for (; res == ERROR_SUCCESS && list->majortype; ++list) {
409         HKEY majortype_key = NULL;
410         HKEY subtype_key = NULL;
411
412         StringFromGUID2(list->majortype, buf, 39);
413         res = RegCreateKeyExW(mediatype_key, buf, 0, NULL, 0,
414                               KEY_READ | KEY_WRITE, NULL, &majortype_key, NULL);
415         if (res != ERROR_SUCCESS) goto error_close_keys;
416
417         StringFromGUID2(list->subtype, buf, 39);
418         res = RegCreateKeyExW(majortype_key, buf, 0, NULL, 0,
419                               KEY_READ | KEY_WRITE, NULL, &subtype_key, NULL);
420         if (res != ERROR_SUCCESS) goto error_close_keys;
421
422         StringFromGUID2(&CLSID_AsyncReader, buf, 39);
423         res = RegSetValueExW(subtype_key, sourcefilter_valuename, 0, REG_SZ, (CONST BYTE*)buf,
424                              (lstrlenW(buf) + 1) * sizeof(WCHAR));
425         if (res != ERROR_SUCCESS) goto error_close_keys;
426
427         for(i = 0; list->line[i]; i++) {
428             char buffer[3];
429             wsprintfA(buffer, "%d", i);
430             res = RegSetValueExA(subtype_key, buffer, 0, REG_SZ, (CONST BYTE*)list->line[i],
431                                  lstrlenA(list->line[i]));
432             if (res != ERROR_SUCCESS) goto error_close_keys;
433         }
434
435 error_close_keys:
436         if (majortype_key)
437             RegCloseKey(majortype_key);
438         if (subtype_key)
439             RegCloseKey(subtype_key);
440     }
441
442     RegCloseKey(mediatype_key);
443
444     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
445 }
446
447 /***********************************************************************
448  *              register_mediatypes_extension
449  */
450 static HRESULT register_mediatypes_extension(struct regsvr_mediatype_extension const *list)
451 {
452     LONG res = ERROR_SUCCESS;
453     HKEY mediatype_key;
454     HKEY extensions_root_key = NULL;
455     WCHAR buf[39];
456
457     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, mediatype_name, 0, NULL, 0,
458                           KEY_READ | KEY_WRITE, NULL, &mediatype_key, NULL);
459     if (res != ERROR_SUCCESS) return HRESULT_FROM_WIN32(res);
460
461     res = RegCreateKeyExW(mediatype_key, extensions_keyname, 0, NULL, 0,
462                           KEY_READ | KEY_WRITE, NULL, &extensions_root_key, NULL);
463     if (res != ERROR_SUCCESS) goto error_return;
464
465     for (; res == ERROR_SUCCESS && list->majortype; ++list) {
466         HKEY extension_key;
467
468         res = RegCreateKeyExA(extensions_root_key, list->extension, 0, NULL, 0,
469                               KEY_READ | KEY_WRITE, NULL, &extension_key, NULL);
470         if (res != ERROR_SUCCESS) break;
471
472         StringFromGUID2(list->majortype, buf, 39);
473         res = RegSetValueExW(extension_key, mediatype_name, 0, REG_SZ, (CONST BYTE*)buf,
474                              (lstrlenW(buf) + 1) * sizeof(WCHAR));
475         if (res != ERROR_SUCCESS) goto error_close_key;
476
477         StringFromGUID2(list->subtype, buf, 39);
478         res = RegSetValueExW(extension_key, subtype_valuename, 0, REG_SZ, (CONST BYTE*)buf,
479                              (lstrlenW(buf) + 1) * sizeof(WCHAR));
480         if (res != ERROR_SUCCESS) goto error_close_key;
481
482         StringFromGUID2(&CLSID_AsyncReader, buf, 39);
483         res = RegSetValueExW(extension_key, sourcefilter_valuename, 0, REG_SZ, (CONST BYTE*)buf,
484                              (lstrlenW(buf) + 1) * sizeof(WCHAR));
485         if (res != ERROR_SUCCESS) goto error_close_key;
486
487 error_close_key:
488         RegCloseKey(extension_key);
489     }
490
491 error_return:
492     RegCloseKey(mediatype_key);
493     if (extensions_root_key)
494         RegCloseKey(extensions_root_key);
495
496     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
497 }
498
499 /***********************************************************************
500  *              unregister_mediatypes_parsing
501  */
502 static HRESULT unregister_mediatypes_parsing(struct regsvr_mediatype_parsing const *list)
503 {
504     LONG res;
505     HKEY mediatype_key;
506     HKEY majortype_key;
507     WCHAR buf[39];
508
509     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, mediatype_name, 0,
510                         KEY_READ | KEY_WRITE, &mediatype_key);
511     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
512     if (res != ERROR_SUCCESS) return HRESULT_FROM_WIN32(res);
513
514     for (; res == ERROR_SUCCESS && list->majortype; ++list) {
515         StringFromGUID2(list->majortype, buf, 39);
516         res = RegOpenKeyExW(mediatype_key, buf, 0,
517                         KEY_READ | KEY_WRITE, &majortype_key);
518         if (res == ERROR_FILE_NOT_FOUND) {
519             res = ERROR_SUCCESS;
520             continue;
521         }
522         if (res != ERROR_SUCCESS) break;
523
524         StringFromGUID2(list->subtype, buf, 39);
525         res = RegDeleteTreeW(majortype_key, buf);
526         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
527
528         /* Removed majortype key if there is no more subtype key */
529         res = RegDeleteKeyW(majortype_key, 0);
530         if (res == ERROR_ACCESS_DENIED) res = ERROR_SUCCESS;
531
532         RegCloseKey(majortype_key);
533     }
534
535     RegCloseKey(mediatype_key);
536
537     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
538 }
539
540 /***********************************************************************
541  *              unregister_mediatypes_extension
542  */
543 static HRESULT unregister_mediatypes_extension(struct regsvr_mediatype_extension const *list)
544 {
545     LONG res;
546     HKEY mediatype_key;
547     HKEY extensions_root_key = NULL;
548
549     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, mediatype_name, 0,
550                         KEY_READ | KEY_WRITE, &mediatype_key);
551     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
552     if (res != ERROR_SUCCESS) return HRESULT_FROM_WIN32(res);
553
554     res = RegOpenKeyExW(mediatype_key, extensions_keyname, 0,
555                         KEY_READ | KEY_WRITE, &extensions_root_key);
556     if (res == ERROR_FILE_NOT_FOUND)
557         res = ERROR_SUCCESS;
558     else if (res == ERROR_SUCCESS)
559         for (; res == ERROR_SUCCESS && list->majortype; ++list) {
560             res = RegDeleteTreeA(extensions_root_key, list->extension);
561             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
562         }
563
564     RegCloseKey(mediatype_key);
565     if (extensions_root_key)
566         RegCloseKey(extensions_root_key);
567
568     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
569 }
570
571 /***********************************************************************
572  *              register_filters
573  */
574 static HRESULT register_filters(struct regsvr_filter const *list)
575 {
576     HRESULT hr;
577     IFilterMapper2* pFM2 = NULL;
578
579     CoInitialize(NULL);
580     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterMapper2, (LPVOID*)&pFM2);
581
582     if (SUCCEEDED(hr)) {
583         for (; SUCCEEDED(hr) && list->clsid; ++list) {
584             REGFILTER2 rf2;
585             REGFILTERPINS2* prfp2;
586             int i;
587
588             for (i = 0; list->pins[i].flags != 0xFFFFFFFF; i++) ;
589             rf2.dwVersion = 2;
590             rf2.dwMerit = list->merit;
591             rf2.u.s1.cPins2 = i;
592             rf2.u.s1.rgPins2 = prfp2 = CoTaskMemAlloc(i*sizeof(REGFILTERPINS2));
593             if (!prfp2) {
594                 hr = E_OUTOFMEMORY;
595                 break;
596             }
597             for (i = 0; list->pins[i].flags != 0xFFFFFFFF; i++) {
598                 REGPINTYPES* lpMediatype;
599                 CLSID* lpClsid;
600                 int j, nbmt;
601                 
602                 for (nbmt = 0; list->pins[i].mediatypes[nbmt].majortype; nbmt++) ;
603                 /* Allocate a single buffer for regpintypes struct and clsids */
604                 lpMediatype = CoTaskMemAlloc(nbmt*(sizeof(REGPINTYPES) + 2*sizeof(CLSID)));
605                 if (!lpMediatype) {
606                     hr = E_OUTOFMEMORY;
607                     break;
608                 }
609                 lpClsid = (CLSID*) (lpMediatype + nbmt);
610                 for (j = 0; j < nbmt; j++) {
611                     (lpMediatype + j)->clsMajorType = lpClsid + j*2;
612                     memcpy(lpClsid + j*2, list->pins[i].mediatypes[j].majortype, sizeof(CLSID));
613                     (lpMediatype + j)->clsMinorType = lpClsid + j*2 + 1;
614                     if (list->pins[i].mediatypes[j].subtype)
615                         memcpy(lpClsid + j*2 + 1, list->pins[i].mediatypes[j].subtype, sizeof(CLSID));
616                     else {
617                         /* Subtype are often a combination of major type + fourcc/tag */
618                         memcpy(lpClsid + j*2 + 1, list->pins[i].mediatypes[j].majortype, sizeof(CLSID));
619                         *(DWORD*)(lpClsid + j*2 + 1) = list->pins[i].mediatypes[j].fourcc;
620                     }
621                 }
622                 prfp2[i].dwFlags = list->pins[i].flags;
623                 prfp2[i].cInstances = 0;
624                 prfp2[i].nMediaTypes = j;
625                 prfp2[i].lpMediaType = lpMediatype;
626                 prfp2[i].nMediums = 0;
627                 prfp2[i].lpMedium = NULL;
628                 prfp2[i].clsPinCategory = NULL;
629             }
630
631             if (FAILED(hr)) {
632                 ERR("failed to register with hresult 0x%x\n", hr);
633                 CoTaskMemFree(prfp2);
634                 break;
635             }
636
637             hr = IFilterMapper2_RegisterFilter(pFM2, list->clsid, list->name, NULL, list->category, NULL, &rf2);
638
639             while (i) {
640                 CoTaskMemFree((REGPINTYPES*)prfp2[i-1].lpMediaType);
641                 i--;
642             }
643             CoTaskMemFree(prfp2);
644         }
645     }
646
647     if (pFM2)
648         IFilterMapper2_Release(pFM2);
649
650     CoUninitialize();
651
652     return hr;
653 }
654
655 /***********************************************************************
656  *              unregister_filters
657  */
658 static HRESULT unregister_filters(struct regsvr_filter const *list)
659 {
660     HRESULT hr;
661     IFilterMapper2* pFM2;
662
663     CoInitialize(NULL);
664     
665     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterMapper2, (LPVOID*)&pFM2);
666
667     if (SUCCEEDED(hr)) {
668         for (; SUCCEEDED(hr) && list->clsid; ++list)
669             hr = IFilterMapper2_UnregisterFilter(pFM2, list->category, NULL, list->clsid);
670         IFilterMapper2_Release(pFM2);
671     }
672
673     CoUninitialize();
674     
675     return hr;
676 }
677
678 /***********************************************************************
679  *              regsvr_key_guid
680  */
681 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
682 {
683     WCHAR buf[39];
684
685     StringFromGUID2(guid, buf, 39);
686     return register_key_defvalueW(base, name, buf);
687 }
688
689 /***********************************************************************
690  *              regsvr_key_defvalueW
691  */
692 static LONG register_key_defvalueW(
693     HKEY base,
694     WCHAR const *name,
695     WCHAR const *value)
696 {
697     LONG res;
698     HKEY key;
699
700     res = RegCreateKeyExW(base, name, 0, NULL, 0,
701                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
702     if (res != ERROR_SUCCESS) return res;
703     res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
704                          (lstrlenW(value) + 1) * sizeof(WCHAR));
705     RegCloseKey(key);
706     return res;
707 }
708
709 /***********************************************************************
710  *              regsvr_key_defvalueA
711  */
712 static LONG register_key_defvalueA(
713     HKEY base,
714     WCHAR const *name,
715     char const *value)
716 {
717     LONG res;
718     HKEY key;
719
720     res = RegCreateKeyExW(base, name, 0, NULL, 0,
721                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
722     if (res != ERROR_SUCCESS) return res;
723     res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
724                          lstrlenA(value) + 1);
725     RegCloseKey(key);
726     return res;
727 }
728
729 /***********************************************************************
730  *              regsvr_progid
731  */
732 static LONG register_progid(
733     WCHAR const *clsid,
734     char const *progid,
735     char const *curver_progid,
736     char const *name,
737     char const *extra)
738 {
739     LONG res;
740     HKEY progid_key;
741
742     res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
743                           NULL, 0, KEY_READ | KEY_WRITE, NULL,
744                           &progid_key, NULL);
745     if (res != ERROR_SUCCESS) return res;
746
747     if (name) {
748         res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
749                              (CONST BYTE*)name, strlen(name) + 1);
750         if (res != ERROR_SUCCESS) goto error_close_progid_key;
751     }
752
753     if (clsid) {
754         res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
755         if (res != ERROR_SUCCESS) goto error_close_progid_key;
756     }
757
758     if (curver_progid) {
759         res = register_key_defvalueA(progid_key, curver_keyname,
760                                      curver_progid);
761         if (res != ERROR_SUCCESS) goto error_close_progid_key;
762     }
763
764     if (extra) {
765         HKEY extra_key;
766
767         res = RegCreateKeyExA(progid_key, extra, 0,
768                               NULL, 0, KEY_READ | KEY_WRITE, NULL,
769                               &extra_key, NULL);
770         if (res == ERROR_SUCCESS)
771             RegCloseKey(extra_key);
772     }
773
774 error_close_progid_key:
775     RegCloseKey(progid_key);
776     return res;
777 }
778
779 static GUID const CLSID_PSFactoryBuffer = {
780     0x92a3a302, 0xda7c, 0x4a1f, {0xba,0x7e,0x18,0x02,0xbb,0x5d,0x2d,0x02} };
781
782 /***********************************************************************
783  *              coclass list
784  */
785 static struct regsvr_coclass const coclass_list[] = {
786     {   &CLSID_FilterGraph,
787         "Filter Graph",
788         NULL,
789         "quartz.dll",
790         "Both"
791     },
792     {   &CLSID_FilterGraphNoThread,
793         "Filter Graph",
794         NULL,
795         "quartz.dll",
796         "Both"
797     },
798     {   &CLSID_FilterMapper,
799         "Filter Mapper",
800         NULL,
801         "quartz.dll",
802         "Both"
803     },
804     {   &CLSID_FilterMapper2,
805         "Filter Mapper2",
806         NULL,
807         "quartz.dll",
808         "Both"
809     },
810     {   &CLSID_SystemClock,
811         "System Clock",
812         NULL,
813         "quartz.dll",
814         "Both"
815     },
816     {   &CLSID_MemoryAllocator,
817         "Memory Allocator",
818         NULL,
819         "quartz.dll",
820         "Both"
821     },
822     {   &CLSID_SeekingPassThru,
823        "Seeking",
824        NULL,
825        "quartz.dll",
826        "Both"
827     },
828     {   &CLSID_AsyncReader,
829         "File Source Filter",
830         NULL,
831         "quartz.dll",
832         "Both"
833     },
834     {   &CLSID_AviSplitter,
835         "AVI Splitter",
836         NULL,
837         "quartz.dll",
838         "Both"
839     },
840     {   &CLSID_MPEG1Splitter,
841         "MPEG-I Stream Splitter",
842         NULL,
843         "quartz.dll",
844         "Both"
845     },
846     {   &CLSID_AVIDec,
847         "AVI Decompressor",
848         NULL,
849         "quartz.dll",
850         "Both"
851     },
852     {   &CLSID_DSoundRender,
853         "DirectSound Audio Renderer",
854         NULL,
855         "quartz.dll",
856         "Both"
857     },
858     {   &CLSID_NullRenderer,
859         "Null Renderer",
860         NULL,
861         "quartz.dll",
862         "Both"
863     },
864     {   &CLSID_VideoRenderer,
865         "Video Renderer",
866         NULL,
867         "quartz.dll",
868         "Both"
869     },
870     {   &CLSID_VideoRendererDefault,
871         "Default Video Renderer",
872         NULL,
873         "quartz.dll",
874         "Both"
875     },
876     {   &CLSID_ACMWrapper,
877         "ACM wrapper",
878         NULL,
879         "quartz.dll",
880         "Both"
881     },
882     {   &CLSID_WAVEParser,
883         "Wave Parser",
884         NULL,
885         "quartz.dll",
886         "Both"
887     },
888     { NULL }                    /* list terminator */
889 };
890
891 /***********************************************************************
892  *              interface list
893  */
894
895 static struct regsvr_interface const interface_list[] = {
896     {   &IID_IFilterGraph,
897         "IFilterGraph",
898         NULL,
899         11,
900         NULL,
901         &CLSID_PSFactoryBuffer
902     },
903     {   &IID_IFilterGraph2,
904         "IFilterGraph2",
905         NULL,
906         21,
907         NULL,
908         &CLSID_PSFactoryBuffer
909     },
910     {   &IID_IFilterMapper,
911         "IFilterMapper",
912         NULL,
913         11,
914         NULL,
915         &CLSID_PSFactoryBuffer
916     },
917     {   &IID_IFilterMapper2,
918         "IFilterMapper2",
919         NULL,
920         7,
921         NULL,
922         &CLSID_PSFactoryBuffer
923     },
924     /* FIXME:
925     {   &IID_SeekingPassThru,
926         "ISeekingPassThru",
927         NULL,
928         4,
929         NULL,
930         &CLSID_PSFactoryBuffer
931     },
932     {   &IID_AsyncReader,
933         "IAsyncReader",
934         NULL,
935         11,
936         NULL,
937         &CLSID_PSFactoryBuffer
938     },
939     {   &IID_WAVEParser,
940         "IWAVEParser",
941         NULL,
942         11,
943         NULL,
944         &CLSID_PSFactoryBuffer
945     },*/
946     { NULL }                    /* list terminator */
947 };
948
949 /***********************************************************************
950  *              mediatype list
951  */
952
953 static struct regsvr_mediatype_parsing const mediatype_parsing_list[] = {
954     {   &MEDIATYPE_Stream,
955         &MEDIASUBTYPE_Avi,
956         {   "0,4,,52494646,8,4,,41564920",
957             NULL }
958     },
959     {   &MEDIATYPE_Stream,
960         &MEDIASUBTYPE_MPEG1System,
961         {   "0, 16, FFFFFFFFF100010001800001FFFFFFFF, 000001BA2100010001800001000001BB",
962             NULL }
963     },
964     {   &MEDIATYPE_Stream,
965         &MEDIASUBTYPE_MPEG1VideoCD,
966         {   "0, 4, , 52494646, 8, 8, , 43445841666D7420, 36, 20, FFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFF, 646174610000000000FFFFFFFFFFFFFFFFFFFF00",
967             NULL }
968     },
969     {   &MEDIATYPE_Stream,
970         &MEDIASUBTYPE_MPEG1Video,
971         {   "0, 4, , 000001B3",
972             NULL }
973     },
974     {   &MEDIATYPE_Stream,
975         &MEDIASUBTYPE_MPEG1Audio,
976         {   "0, 2, FFE0, FFE0",
977             "0, 10, FFFFFFFF000000000000, 494433030080808080",
978             NULL }
979     },
980     {   &MEDIATYPE_Stream,
981         &MEDIASUBTYPE_QTMovie,
982         {   "4, 4, , 6d646174",
983             "4, 4, , 6d6f6f76",
984             NULL }
985     },
986     {   &MEDIATYPE_Stream,
987         &MEDIASUBTYPE_WAVE,
988         {   "0,4,,52494646,8,4,,57415645",
989             NULL }
990     },
991     {   &MEDIATYPE_Stream,
992         &MEDIASUBTYPE_AU,
993         {   "0,4,,2e736e64",
994             NULL }
995     },
996     {   &MEDIATYPE_Stream,
997         &MEDIASUBTYPE_AIFF,
998         {   "0,4,,464f524d,8,4,,41494646",
999             "0,4,,464f524d,8,4,,41494643",
1000             NULL }
1001     },
1002     {   &MEDIATYPE_Stream,
1003         &MEDIATYPE_Text,
1004         {   "0,4,,4C595249",
1005             "0,4,,6C797269",
1006             NULL }
1007     },
1008     {   &MEDIATYPE_Stream,
1009         &MEDIATYPE_Midi,
1010         {   "0,4,,52494646,8,4,,524D4944",
1011             "0,4,,4D546864",
1012             NULL }
1013     },
1014     { NULL }                    /* list terminator */
1015 };
1016
1017 /***********************************************************************
1018  *              mediatype list
1019  */
1020
1021 static struct regsvr_mediatype_extension const mediatype_extension_list[] = {
1022     {   &MEDIATYPE_Stream,
1023         &MEDIASUBTYPE_MPEG1Audio,
1024         ".mp3"
1025     },
1026     { NULL }                    /* list terminator */
1027 };
1028
1029 /***********************************************************************
1030  *              filter list
1031  */
1032
1033 static struct regsvr_filter const filter_list[] = {
1034     {   &CLSID_AviSplitter,
1035         &CLSID_LegacyAmFilterCategory,
1036         {'A','V','I',' ','S','p','l','i','t','t','e','r',0},
1037         0x600000,
1038         {   {   0,
1039                 {   { &MEDIATYPE_Stream, &MEDIASUBTYPE_Avi },
1040                     { NULL }
1041                 },
1042             },
1043             {   REG_PINFLAG_B_OUTPUT,
1044                 {   { &MEDIATYPE_Video, &GUID_NULL },
1045                     { NULL }
1046                 },
1047             },
1048             { 0xFFFFFFFF },
1049         }
1050     },
1051     {   &CLSID_MPEG1Splitter,
1052         &CLSID_LegacyAmFilterCategory,
1053         {'M','P','E','G','-','I',' ','S','t','r','e','a','m',' ','S','p','l','i','t','t','e','r',0},
1054         0x600000,
1055         {   {   0,
1056                 {   { &MEDIATYPE_Stream, &MEDIASUBTYPE_MPEG1Audio },
1057                     { &MEDIATYPE_Stream, &MEDIASUBTYPE_MPEG1Video },
1058                     { &MEDIATYPE_Stream, &MEDIASUBTYPE_MPEG1System },
1059                     { &MEDIATYPE_Stream, &MEDIASUBTYPE_MPEG1VideoCD },
1060                     { NULL }
1061                 },
1062             },
1063             {   REG_PINFLAG_B_OUTPUT,
1064                 {   { &MEDIATYPE_Audio, &MEDIASUBTYPE_MPEG1Packet },
1065                     { &MEDIATYPE_Audio, &MEDIASUBTYPE_MPEG1AudioPayload },
1066                     { NULL }
1067                 },
1068             },
1069             {   REG_PINFLAG_B_OUTPUT,
1070                 {   { &MEDIATYPE_Video, &MEDIASUBTYPE_MPEG1Packet },
1071                     { &MEDIATYPE_Video, &MEDIASUBTYPE_MPEG1Payload },
1072                     { NULL }
1073                 },
1074             },
1075             { 0xFFFFFFFF },
1076         }
1077     },
1078     {   &CLSID_NullRenderer,
1079         &CLSID_LegacyAmFilterCategory,
1080         {'N','u','l','l',' ','R','e','n','d','e','r','e','r',0},
1081         0x200000,
1082         {   {   REG_PINFLAG_B_RENDERER,
1083                 {   { &MEDIATYPE_NULL, &GUID_NULL },
1084                     { NULL }
1085                 },
1086             },
1087             { 0xFFFFFFFF },
1088         }
1089     },
1090     {   &CLSID_VideoRenderer,
1091         &CLSID_LegacyAmFilterCategory,
1092         {'V','i','d','e','o',' ','R','e','n','d','e','r','e','r',0},
1093         0x800000,
1094         {   {   REG_PINFLAG_B_RENDERER,
1095                 {   { &MEDIATYPE_Video, &GUID_NULL },
1096                     { NULL }
1097                 },
1098             },
1099             { 0xFFFFFFFF },
1100         }
1101     },
1102     {   &CLSID_VideoRendererDefault,
1103         &CLSID_LegacyAmFilterCategory,
1104         {'V','i','d','e','o',' ','R','e','n','d','e','r','e','r',0},
1105         0x800000,
1106         {   {   REG_PINFLAG_B_RENDERER,
1107                 {   { &MEDIATYPE_Video, &GUID_NULL },
1108                     { NULL }
1109                 },
1110             },
1111             { 0xFFFFFFFF },
1112         }
1113     },
1114     {   &CLSID_DSoundRender,
1115         &CLSID_LegacyAmFilterCategory,
1116         {'A','u','d','i','o',' ','R','e','n','d','e','r','e','r',0},
1117         0x800000,
1118         {   {   REG_PINFLAG_B_RENDERER,
1119                 {   { &MEDIATYPE_Audio, &MEDIASUBTYPE_PCM },
1120 /*                  { &MEDIATYPE_Audio, &MEDIASUBTYPE_IEEE_FLOAT }, */
1121                     { NULL }
1122                 },
1123             },
1124             { 0xFFFFFFFF },
1125         }
1126     },
1127     {   &CLSID_AVIDec,
1128         &CLSID_LegacyAmFilterCategory,
1129         {'A','V','I',' ','D','e','c','o','m','p','r','e','s','s','o','r',0},
1130         0x600000,
1131         {   {   0,
1132                 {   { &MEDIATYPE_Video, &GUID_NULL },
1133                     { NULL }
1134                 },
1135             },
1136             {   REG_PINFLAG_B_OUTPUT,
1137                 {   { &MEDIATYPE_Video, &GUID_NULL },
1138                     { NULL }
1139                 },
1140             },
1141             { 0xFFFFFFFF },
1142         }
1143     },
1144     {   &CLSID_AsyncReader,
1145         &CLSID_LegacyAmFilterCategory,
1146         {'F','i','l','e',' ','S','o','u','r','c','e',' ','(','A','s','y','n','c','.',')',0},
1147         0x400000,
1148         {   {   REG_PINFLAG_B_OUTPUT,
1149                 {   { &MEDIATYPE_Stream, &GUID_NULL },
1150                     { NULL }
1151                 },
1152             },
1153             { 0xFFFFFFFF },
1154         }
1155     },
1156     {   &CLSID_ACMWrapper,
1157         &CLSID_LegacyAmFilterCategory,
1158         {'A','C','M',' ','W','r','a','p','p','e','r',0},
1159         0x600000,
1160         {   {   0,
1161                 {   { &MEDIATYPE_Audio, &GUID_NULL },
1162                     { NULL }
1163                 },
1164             },
1165             {   REG_PINFLAG_B_OUTPUT,
1166                 {   { &MEDIATYPE_Audio, &GUID_NULL },
1167                     { NULL }
1168                 },
1169             },
1170             { 0xFFFFFFFF },
1171         }
1172     },
1173     {   &CLSID_WAVEParser,
1174         &CLSID_LegacyAmFilterCategory,
1175         {'W','a','v','e',' ','P','a','r','s','e','r',0},
1176         0x400000,
1177         {   {   0,
1178                 {   { &MEDIATYPE_Stream, &MEDIASUBTYPE_WAVE },
1179                     { &MEDIATYPE_Stream, &MEDIASUBTYPE_AU },
1180                     { &MEDIATYPE_Stream, &MEDIASUBTYPE_AIFF },
1181                     { NULL }
1182                 },
1183             },
1184             {   REG_PINFLAG_B_OUTPUT,
1185                 {   { &MEDIATYPE_Audio, &GUID_NULL },
1186                     { NULL }
1187                 },
1188             },
1189             { 0xFFFFFFFF },
1190         }
1191     },
1192     { NULL }            /* list terminator */
1193 };
1194
1195 /***********************************************************************
1196  *              DllRegisterServer (QUARTZ.@)
1197  */
1198 HRESULT WINAPI DllRegisterServer(void)
1199 {
1200     HRESULT hr;
1201
1202     TRACE("\n");
1203
1204     hr = register_coclasses(coclass_list);
1205     if (SUCCEEDED(hr))
1206         hr = register_interfaces(interface_list);
1207     if (SUCCEEDED(hr))
1208         hr = register_mediatypes_parsing(mediatype_parsing_list);
1209     if (SUCCEEDED(hr))
1210         hr = register_mediatypes_extension(mediatype_extension_list);
1211     if (SUCCEEDED(hr))
1212         hr = register_filters(filter_list);
1213     return hr;
1214 }
1215
1216 /***********************************************************************
1217  *              DllUnregisterServer (QUARTZ.@)
1218  */
1219 HRESULT WINAPI DllUnregisterServer(void)
1220 {
1221     HRESULT hr;
1222
1223     TRACE("\n");
1224
1225     hr = unregister_filters(filter_list);
1226     if (SUCCEEDED(hr))
1227         hr = unregister_coclasses(coclass_list);
1228     if (SUCCEEDED(hr))
1229         hr = unregister_interfaces(interface_list);
1230     if (SUCCEEDED(hr))
1231         hr = unregister_mediatypes_parsing(mediatype_parsing_list);
1232     if (SUCCEEDED(hr))
1233         hr = unregister_mediatypes_extension(mediatype_extension_list);
1234     return hr;
1235 }