inetcomm: Implement IMimeMessage_Find{First,Next}.
[wine] / dlls / ddrawex / regsvr.c
1 /*
2  *      self-registerable dll functions for ddrawex.dll
3  *
4  * Copyright (C) 2003 John K. Hohm
5  * Copyright (C) 2007 Francois Gouget for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
31
32 #include "ddraw.h"
33 #include "ddrawex_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38
39 /*
40  * Near the bottom of this file are the exported DllRegisterServer and
41  * DllUnregisterServer, which make all this worthwhile.
42  */
43
44 /***********************************************************************
45  *              interface for self-registering
46  */
47
48 struct regsvr_coclass
49 {
50     CLSID const *clsid;         /* NULL for end of list */
51     LPCSTR name;                /* can be NULL to omit */
52     LPCSTR ips;                 /* can be NULL to omit */
53     LPCSTR ips32;               /* can be NULL to omit */
54     LPCSTR ips32_tmodel;        /* can be NULL to omit */
55     LPCSTR clsid_str;           /* can be NULL to omit */
56     LPCSTR progid;              /* can be NULL to omit */
57 };
58
59 static HRESULT register_coclasses(struct regsvr_coclass const *list);
60 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
61
62 /***********************************************************************
63  *              static string constants
64  */
65 static WCHAR const interface_keyname[10] = {
66     'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
67 static WCHAR const base_ifa_keyname[14] = {
68     'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
69     'e', 0 };
70 static WCHAR const num_methods_keyname[11] = {
71     'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
72 static WCHAR const ps_clsid_keyname[15] = {
73     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
74     'i', 'd', 0 };
75 static WCHAR const ps_clsid32_keyname[17] = {
76     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
77     'i', 'd', '3', '2', 0 };
78 static WCHAR const clsid_keyname[6] = {
79     'C', 'L', 'S', 'I', 'D', 0 };
80 static WCHAR const ips_keyname[13] = {
81     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
82     0 };
83 static WCHAR const ips32_keyname[15] = {
84     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
85     '3', '2', 0 };
86 static WCHAR const progid_keyname[7] = {
87     'P', 'r', 'o', 'g', 'I', 'D', 0 };
88 static char const tmodel_valuename[] = "ThreadingModel";
89
90 /***********************************************************************
91  *              static helper functions
92  */
93 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
94                                    WCHAR const *value);
95 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
96                                    char const *value);
97
98 /***********************************************************************
99  *              register_coclasses
100  */
101 static HRESULT register_coclasses(struct regsvr_coclass const *list)
102 {
103     LONG res = ERROR_SUCCESS;
104     HKEY coclass_key;
105
106     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
107                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
108     if (res != ERROR_SUCCESS) goto error_return;
109
110     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
111         WCHAR buf[39];
112         HKEY clsid_key;
113
114         StringFromGUID2(list->clsid, buf, 39);
115         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
116                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
117         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
118
119         if (list->name) {
120             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
121                                  (CONST BYTE*)(list->name),
122                                  strlen(list->name) + 1);
123             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
124         }
125
126         if (list->ips) {
127             res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
128             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
129         }
130
131         if (list->ips32) {
132             HKEY ips32_key;
133
134             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
135                                   KEY_READ | KEY_WRITE, NULL,
136                                   &ips32_key, NULL);
137             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
138
139             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
140                                  (CONST BYTE*)list->ips32,
141                                  lstrlenA(list->ips32) + 1);
142             if (res == ERROR_SUCCESS && list->ips32_tmodel)
143                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
144                                      (CONST BYTE*)list->ips32_tmodel,
145                                      strlen(list->ips32_tmodel) + 1);
146             RegCloseKey(ips32_key);
147             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
148         }
149
150         if (list->clsid_str) {
151             res = register_key_defvalueA(clsid_key, clsid_keyname,
152                                          list->clsid_str);
153             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
154         }
155
156         if (list->progid) {
157             HKEY progid_key;
158
159             res = register_key_defvalueA(clsid_key, progid_keyname,
160                                          list->progid);
161             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
162
163             res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
164                                   NULL, 0, KEY_READ | KEY_WRITE, NULL,
165                                   &progid_key, NULL);
166             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
167
168             res = register_key_defvalueW(progid_key, clsid_keyname, buf);
169             RegCloseKey(progid_key);
170             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
171         }
172
173     error_close_clsid_key:
174         RegCloseKey(clsid_key);
175     }
176
177 error_close_coclass_key:
178     RegCloseKey(coclass_key);
179 error_return:
180     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
181 }
182
183 /***********************************************************************
184  *              unregister_coclasses
185  */
186 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
187 {
188     LONG res = ERROR_SUCCESS;
189     HKEY coclass_key;
190
191     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
192                         KEY_READ | KEY_WRITE, &coclass_key);
193     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
194     if (res != ERROR_SUCCESS) goto error_return;
195
196     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
197         WCHAR buf[39];
198
199         StringFromGUID2(list->clsid, buf, 39);
200         res = RegDeleteTreeW(coclass_key, buf);
201         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
202         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
203
204         if (list->progid) {
205             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
206             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
207             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
208         }
209     }
210
211 error_close_coclass_key:
212     RegCloseKey(coclass_key);
213 error_return:
214     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
215 }
216
217 /***********************************************************************
218  *              regsvr_key_defvalueW
219  */
220 static LONG register_key_defvalueW(
221     HKEY base,
222     WCHAR const *name,
223     WCHAR const *value)
224 {
225     LONG res;
226     HKEY key;
227
228     res = RegCreateKeyExW(base, name, 0, NULL, 0,
229                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
230     if (res != ERROR_SUCCESS) return res;
231     res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
232                          (lstrlenW(value) + 1) * sizeof(WCHAR));
233     RegCloseKey(key);
234     return res;
235 }
236
237 /***********************************************************************
238  *              regsvr_key_defvalueA
239  */
240 static LONG register_key_defvalueA(
241     HKEY base,
242     WCHAR const *name,
243     char const *value)
244 {
245     LONG res;
246     HKEY key;
247
248     res = RegCreateKeyExW(base, name, 0, NULL, 0,
249                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
250     if (res != ERROR_SUCCESS) return res;
251     res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
252                          lstrlenA(value) + 1);
253     RegCloseKey(key);
254     return res;
255 }
256
257 /***********************************************************************
258  *              coclass list
259  */
260
261 static struct regsvr_coclass const coclass_list[] = {
262     {   &CLSID_DirectDrawFactory,
263         NULL,
264         NULL,
265         "ddrawex.dll",
266         "Both"
267     },
268     { NULL }                    /* list terminator */
269 };
270
271 /***********************************************************************
272  *              DllRegisterServer (DDRAWEX.@)
273  */
274 HRESULT WINAPI DllRegisterServer(void)
275 {
276     HRESULT hr;
277
278     TRACE("\n");
279
280     hr = register_coclasses(coclass_list);
281     return hr;
282 }
283
284 /***********************************************************************
285  *              DllUnregisterServer (DDRAWEX.@)
286  */
287 HRESULT WINAPI DllUnregisterServer(void)
288 {
289     HRESULT hr;
290
291     TRACE("\n");
292
293     hr = unregister_coclasses(coclass_list);
294     return hr;
295 }