* Slap *some* sense into our header inclusions.
[reactos.git] / reactos / dll / win32 / hhctrl.ocx / regsvr.c
1 /*
2 * self-registerable dll functions for hhctrl.ocx
3 *
4 * Copyright (C) 2004 Stefan Leichter
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 WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <stdarg.h>
26 //#include <string.h>
27
28 #include <windef.h>
29 #include <winbase.h>
30 //#include "winuser.h"
31 //#include "wingdi.h"
32 #include <winreg.h>
33 //#include "winerror.h"
34
35 #include <ole2.h>
36
37 #include <wine/debug.h>
38 #include <wine/unicode.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
41
42 /*
43 * Near the bottom of this file are the exported DllRegisterServer and
44 * DllUnregisterServer, which make all this worthwhile.
45 */
46
47 /***********************************************************************
48 * interface for self-registering
49 */
50 struct regsvr_interface
51 {
52 IID const *iid; /* NULL for end of list */
53 LPCSTR name; /* can be NULL to omit */
54 IID const *base_iid; /* can be NULL to omit */
55 int num_methods; /* can be <0 to omit */
56 CLSID const *ps_clsid; /* can be NULL to omit */
57 CLSID const *ps_clsid32; /* can be NULL to omit */
58 };
59
60 static HRESULT register_interfaces(struct regsvr_interface const *list);
61 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
62
63 struct regsvr_coclass
64 {
65 CLSID const *clsid; /* NULL for end of list */
66 LPCSTR name; /* can be NULL to omit */
67 LPCSTR ips; /* can be NULL to omit */
68 LPCSTR ips32; /* can be NULL to omit */
69 LPCSTR ips32_tmodel; /* can be NULL to omit */
70 LPCSTR progid; /* can be NULL to omit */
71 LPCSTR viprogid; /* can be NULL to omit */
72 LPCSTR progid_extra; /* can be NULL to omit */
73 };
74
75 static HRESULT register_coclasses(struct regsvr_coclass const *list);
76 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
77
78 /***********************************************************************
79 * static string constants
80 */
81 static WCHAR const interface_keyname[10] = {
82 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
83 static WCHAR const base_ifa_keyname[14] = {
84 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
85 'e', 0 };
86 static WCHAR const num_methods_keyname[11] = {
87 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
88 static WCHAR const ps_clsid_keyname[15] = {
89 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
90 'i', 'd', 0 };
91 static WCHAR const ps_clsid32_keyname[17] = {
92 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
93 'i', 'd', '3', '2', 0 };
94 static WCHAR const clsid_keyname[6] = {
95 'C', 'L', 'S', 'I', 'D', 0 };
96 static WCHAR const curver_keyname[7] = {
97 'C', 'u', 'r', 'V', 'e', 'r', 0 };
98 static WCHAR const ips_keyname[13] = {
99 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
100 0 };
101 static WCHAR const ips32_keyname[15] = {
102 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
103 '3', '2', 0 };
104 static WCHAR const progid_keyname[7] = {
105 'P', 'r', 'o', 'g', 'I', 'D', 0 };
106 static WCHAR const viprogid_keyname[25] = {
107 'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
108 'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
109 0 };
110 static char const tmodel_valuename[] = "ThreadingModel";
111
112 /***********************************************************************
113 * static helper functions
114 */
115 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
116 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
117 WCHAR const *value);
118 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
119 char const *value);
120 static LONG register_progid(WCHAR const *clsid,
121 char const *progid, char const *curver_progid,
122 char const *name, char const *extra);
123
124 /***********************************************************************
125 * register_interfaces
126 */
127 static HRESULT register_interfaces(struct regsvr_interface const *list)
128 {
129 LONG res = ERROR_SUCCESS;
130 HKEY interface_key;
131
132 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
133 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
134 if (res != ERROR_SUCCESS) goto error_return;
135
136 for (; res == ERROR_SUCCESS && list->iid; ++list) {
137 WCHAR buf[39];
138 HKEY iid_key;
139
140 StringFromGUID2(list->iid, buf, 39);
141 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
142 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
143 if (res != ERROR_SUCCESS) goto error_close_interface_key;
144
145 if (list->name) {
146 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
147 (CONST BYTE*)(list->name),
148 strlen(list->name) + 1);
149 if (res != ERROR_SUCCESS) goto error_close_iid_key;
150 }
151
152 if (list->base_iid) {
153 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
154 if (res != ERROR_SUCCESS) goto error_close_iid_key;
155 }
156
157 if (0 <= list->num_methods) {
158 static WCHAR const fmt[3] = { '%', 'd', 0 };
159 HKEY key;
160
161 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
162 KEY_READ | KEY_WRITE, NULL, &key, NULL);
163 if (res != ERROR_SUCCESS) goto error_close_iid_key;
164
165 sprintfW(buf, fmt, list->num_methods);
166 res = RegSetValueExW(key, NULL, 0, REG_SZ,
167 (CONST BYTE*)buf,
168 (lstrlenW(buf) + 1) * sizeof(WCHAR));
169 RegCloseKey(key);
170
171 if (res != ERROR_SUCCESS) goto error_close_iid_key;
172 }
173
174 if (list->ps_clsid) {
175 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
176 if (res != ERROR_SUCCESS) goto error_close_iid_key;
177 }
178
179 if (list->ps_clsid32) {
180 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
181 if (res != ERROR_SUCCESS) goto error_close_iid_key;
182 }
183
184 error_close_iid_key:
185 RegCloseKey(iid_key);
186 }
187
188 error_close_interface_key:
189 RegCloseKey(interface_key);
190 error_return:
191 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
192 }
193
194 /***********************************************************************
195 * unregister_interfaces
196 */
197 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
198 {
199 LONG res = ERROR_SUCCESS;
200 HKEY interface_key;
201
202 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
203 KEY_READ | KEY_WRITE, &interface_key);
204 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
205 if (res != ERROR_SUCCESS) goto error_return;
206
207 for (; res == ERROR_SUCCESS && list->iid; ++list) {
208 WCHAR buf[39];
209
210 StringFromGUID2(list->iid, buf, 39);
211 res = RegDeleteTreeW(interface_key, buf);
212 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
213 }
214
215 RegCloseKey(interface_key);
216 error_return:
217 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
218 }
219
220 /***********************************************************************
221 * register_coclasses
222 */
223 static HRESULT register_coclasses(struct regsvr_coclass const *list)
224 {
225 LONG res = ERROR_SUCCESS;
226 HKEY coclass_key;
227
228 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
229 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
230 if (res != ERROR_SUCCESS) goto error_return;
231
232 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
233 WCHAR buf[39];
234 HKEY clsid_key;
235
236 StringFromGUID2(list->clsid, buf, 39);
237 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
238 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
239 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
240
241 if (list->name) {
242 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
243 (CONST BYTE*)(list->name),
244 strlen(list->name) + 1);
245 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
246 }
247
248 if (list->ips) {
249 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
250 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
251 }
252
253 if (list->ips32) {
254 HKEY ips32_key;
255
256 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
257 KEY_READ | KEY_WRITE, NULL,
258 &ips32_key, NULL);
259 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
260
261 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
262 (CONST BYTE*)list->ips32,
263 lstrlenA(list->ips32) + 1);
264 if (res == ERROR_SUCCESS && list->ips32_tmodel)
265 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
266 (CONST BYTE*)list->ips32_tmodel,
267 strlen(list->ips32_tmodel) + 1);
268 RegCloseKey(ips32_key);
269 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
270 }
271
272 if (list->progid) {
273 res = register_key_defvalueA(clsid_key, progid_keyname,
274 list->progid);
275 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
276
277 res = register_progid(buf, list->progid, NULL,
278 list->name, list->progid_extra);
279 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
280 }
281
282 if (list->viprogid) {
283 res = register_key_defvalueA(clsid_key, viprogid_keyname,
284 list->viprogid);
285 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
286
287 res = register_progid(buf, list->viprogid, list->progid,
288 list->name, list->progid_extra);
289 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
290 }
291
292 error_close_clsid_key:
293 RegCloseKey(clsid_key);
294 }
295
296 error_close_coclass_key:
297 RegCloseKey(coclass_key);
298 error_return:
299 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
300 }
301
302 /***********************************************************************
303 * unregister_coclasses
304 */
305 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
306 {
307 LONG res = ERROR_SUCCESS;
308 HKEY coclass_key;
309
310 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
311 KEY_READ | KEY_WRITE, &coclass_key);
312 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
313 if (res != ERROR_SUCCESS) goto error_return;
314
315 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
316 WCHAR buf[39];
317
318 StringFromGUID2(list->clsid, buf, 39);
319 res = RegDeleteTreeW(coclass_key, buf);
320 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
321 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
322
323 if (list->progid) {
324 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
325 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
326 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
327 }
328
329 if (list->viprogid) {
330 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->viprogid);
331 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
332 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
333 }
334 }
335
336 error_close_coclass_key:
337 RegCloseKey(coclass_key);
338 error_return:
339 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
340 }
341
342 /***********************************************************************
343 * regsvr_key_guid
344 */
345 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
346 {
347 WCHAR buf[39];
348
349 StringFromGUID2(guid, buf, 39);
350 return register_key_defvalueW(base, name, buf);
351 }
352
353 /***********************************************************************
354 * regsvr_key_defvalueW
355 */
356 static LONG register_key_defvalueW(
357 HKEY base,
358 WCHAR const *name,
359 WCHAR const *value)
360 {
361 LONG res;
362 HKEY key;
363
364 res = RegCreateKeyExW(base, name, 0, NULL, 0,
365 KEY_READ | KEY_WRITE, NULL, &key, NULL);
366 if (res != ERROR_SUCCESS) return res;
367 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
368 (lstrlenW(value) + 1) * sizeof(WCHAR));
369 RegCloseKey(key);
370 return res;
371 }
372
373 /***********************************************************************
374 * regsvr_key_defvalueA
375 */
376 static LONG register_key_defvalueA(
377 HKEY base,
378 WCHAR const *name,
379 char const *value)
380 {
381 LONG res;
382 HKEY key;
383
384 res = RegCreateKeyExW(base, name, 0, NULL, 0,
385 KEY_READ | KEY_WRITE, NULL, &key, NULL);
386 if (res != ERROR_SUCCESS) return res;
387 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
388 lstrlenA(value) + 1);
389 RegCloseKey(key);
390 return res;
391 }
392
393 /***********************************************************************
394 * regsvr_progid
395 */
396 static LONG register_progid(
397 WCHAR const *clsid,
398 char const *progid,
399 char const *curver_progid,
400 char const *name,
401 char const *extra)
402 {
403 LONG res;
404 HKEY progid_key;
405
406 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
407 NULL, 0, KEY_READ | KEY_WRITE, NULL,
408 &progid_key, NULL);
409 if (res != ERROR_SUCCESS) return res;
410
411 if (name) {
412 res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
413 (CONST BYTE*)name, strlen(name) + 1);
414 if (res != ERROR_SUCCESS) goto error_close_progid_key;
415 }
416
417 if (clsid) {
418 res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
419 if (res != ERROR_SUCCESS) goto error_close_progid_key;
420 }
421
422 if (curver_progid) {
423 res = register_key_defvalueA(progid_key, curver_keyname,
424 curver_progid);
425 if (res != ERROR_SUCCESS) goto error_close_progid_key;
426 }
427
428 if (extra) {
429 HKEY extra_key;
430
431 res = RegCreateKeyExA(progid_key, extra, 0,
432 NULL, 0, KEY_READ | KEY_WRITE, NULL,
433 &extra_key, NULL);
434 if (res == ERROR_SUCCESS)
435 RegCloseKey(extra_key);
436 }
437
438 error_close_progid_key:
439 RegCloseKey(progid_key);
440 return res;
441 }
442
443 /***********************************************************************
444 * coclass list
445 */
446 static struct regsvr_coclass const coclass_list[] = {
447 /* { 41B23C28-488E-4E5C-ACE2-BB0BBABE99E8,
448 * "HHCtrl Object",
449 * NULL,
450 * "hhctrl.ocx",
451 * "Apartment"
452 * },
453 * { 4662DAB0-D393-11D0-9A56-00C04FB68B66,
454 * "",
455 * NULL,
456 * "hhctrl.ocx",
457 * "Both"
458 * },
459 * { ADB880A4-D8FF-11CF-9377-00AA003B7A11,
460 * "",
461 * NULL,
462 * "hhctrl.ocx",
463 * "Both"
464 * },
465 * { ADB880A6-D8FF-11CF-9377-00AA003B7A11,
466 * "HHCtrl Object",
467 * NULL,
468 * "hhctrl.ocx",
469 * "Apartment"
470 * },
471 */
472 { NULL } /* list terminator */
473 };
474
475 /***********************************************************************
476 * interface list
477 */
478
479 static struct regsvr_interface const interface_list[] = {
480 { NULL } /* list terminator */
481 };
482
483 /***********************************************************************
484 * DllRegisterServer (HHCTRL.@)
485 */
486 HRESULT WINAPI DllRegisterServer(void)
487 {
488 HRESULT hr;
489
490 TRACE("\n");
491
492 hr = register_coclasses(coclass_list);
493 if (SUCCEEDED(hr))
494 hr = register_interfaces(interface_list);
495 return hr;
496 }
497
498 /***********************************************************************
499 * DllUnregisterServer (HHCTRL.@)
500 */
501 HRESULT WINAPI DllUnregisterServer(void)
502 {
503 HRESULT hr;
504
505 TRACE("\n");
506
507 hr = unregister_coclasses(coclass_list);
508 if (SUCCEEDED(hr))
509 hr = unregister_interfaces(interface_list);
510 return hr;
511 }