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