mmdevapi: Add a warning if openal is not available.
[wine] / dlls / mmdevapi / regsvr.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  * Copyright 2009 Maarten Lankhorst
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
22 #define COBJMACROS
23 #include <stdarg.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "guiddef.h"
33 #include "objbase.h"
34 #include "ocidl.h"
35 #include "mmdeviceapi.h"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
41
42 /***********************************************************************
43  *              interface for self-registering
44  */
45 struct regsvr_coclass
46 {
47     CLSID const *clsid;         /* NULL for end of list */
48     LPCSTR name;                /* can be NULL to omit */
49     LPCSTR ips32;               /* can be NULL to omit */
50     LPCSTR ips32_tmodel;        /* can be NULL to omit */
51 };
52
53 static HRESULT register_coclasses(struct regsvr_coclass const *list);
54 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
55
56 /***********************************************************************
57  *              static string constants
58  */
59 static WCHAR const clsid_keyname[] = {
60     'C', 'L', 'S', 'I', 'D', 0 };
61 static WCHAR const ips32_keyname[] = {
62     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
63     '3', '2', 0 };
64 static WCHAR const progid_keyname[] = {
65     'P', 'r', 'o', 'g', 'I', 'D', 0 };
66 static WCHAR const viprogid_keyname[] = {
67     'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
68     'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
69     0 };
70 static char const tmodel_valuename[] = "ThreadingModel";
71
72 /***********************************************************************
73  *              register_coclasses
74  */
75 static HRESULT register_coclasses(struct regsvr_coclass const *list)
76 {
77     LONG res = ERROR_SUCCESS;
78     HKEY coclass_key;
79
80     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
81                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
82     if (res != ERROR_SUCCESS) goto error_return;
83
84     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
85         WCHAR buf[39];
86         HKEY clsid_key;
87
88         StringFromGUID2(list->clsid, buf, 39);
89         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
90                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
91         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
92
93         if (list->name) {
94             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
95                                  (CONST BYTE*)(list->name),
96                                  strlen(list->name) + 1);
97             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
98         }
99
100         if (list->ips32) {
101             HKEY ips32_key;
102
103             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
104                                   KEY_READ | KEY_WRITE, NULL,
105                                   &ips32_key, NULL);
106             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
107
108             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
109                                  (CONST BYTE*)list->ips32,
110                                  lstrlenA(list->ips32) + 1);
111             if (res == ERROR_SUCCESS && list->ips32_tmodel)
112                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
113                                      (CONST BYTE*)list->ips32_tmodel,
114                                      strlen(list->ips32_tmodel) + 1);
115             RegCloseKey(ips32_key);
116             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
117         }
118
119     error_close_clsid_key:
120         RegCloseKey(clsid_key);
121     }
122
123 error_close_coclass_key:
124     RegCloseKey(coclass_key);
125 error_return:
126     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
127 }
128
129 /***********************************************************************
130  *              unregister_coclasses
131  */
132 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
133 {
134     LONG res = ERROR_SUCCESS;
135     HKEY coclass_key;
136
137     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
138                         KEY_READ | KEY_WRITE, &coclass_key);
139     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
140     if (res != ERROR_SUCCESS) goto error_return;
141
142     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
143         WCHAR buf[39];
144
145         StringFromGUID2(list->clsid, buf, 39);
146         res = RegDeleteTreeW(coclass_key, buf);
147         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
148         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
149     }
150
151 error_close_coclass_key:
152     RegCloseKey(coclass_key);
153 error_return:
154     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
155 }
156
157 /***********************************************************************
158  *              coclass list
159  */
160 static struct regsvr_coclass const coclass_list[] = {
161     {   &CLSID_MMDeviceEnumerator,
162         "MMDeviceEnumerator class",
163         "mmdevapi.dll",
164         "both"
165     },
166     { NULL }                    /* list terminator */
167 };
168
169 HRESULT WINAPI DllRegisterServer(void)
170 {
171     TRACE("\n");
172
173     return register_coclasses(coclass_list);
174 }
175
176 HRESULT WINAPI DllUnregisterServer(void)
177 {
178     TRACE("\n");
179
180     return unregister_coclasses(coclass_list);
181 }