Avoid accessing the htask16 TEB field from ntdll.
[wine/dcerpc.git] / dlls / shell32 / regsvr.c
blob1abcbf6e35cd33219aedcfe27e06c23462c87df6
1 /*
2 * self-registerable dll functions for shell32.dll
4 * Copyright (C) 2003 John K. Hohm
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winerror.h"
30 #include "ole2.h"
31 #include "shlguid.h"
32 #include "shell32_main.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 * Near the bottom of this file are the exported DllRegisterServer and
40 * DllUnregisterServer, which make all this worthwhile.
43 /***********************************************************************
44 * interface for self-registering
46 struct regsvr_interface
48 IID const *iid; /* NULL for end of list */
49 LPCSTR name; /* can be NULL to omit */
50 IID const *base_iid; /* can be NULL to omit */
51 int num_methods; /* can be <0 to omit */
52 CLSID const *ps_clsid; /* can be NULL to omit */
53 CLSID const *ps_clsid32; /* can be NULL to omit */
56 static HRESULT register_interfaces(struct regsvr_interface const *list);
57 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
59 struct regsvr_coclass
61 CLSID const *clsid; /* NULL for end of list */
62 LPCSTR name; /* can be NULL to omit */
63 LPCSTR ips; /* can be NULL to omit */
64 LPCSTR ips32; /* can be NULL to omit */
65 LPCSTR ips32_tmodel; /* can be NULL to omit */
66 DWORD flags;
67 LPCSTR clsid_str; /* can be NULL to omit */
68 LPCSTR progid; /* can be NULL to omit */
71 /* flags for regsvr_coclass.flags */
72 #define SHELLEX_MAYCHANGEDEFAULTMENU 0x00000001
73 #define SHELLFOLDER_WANTSFORPARSING 0x00000002
75 static HRESULT register_coclasses(struct regsvr_coclass const *list);
76 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
78 /***********************************************************************
79 * static string constants
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 ips_keyname[13] = {
97 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
98 0 };
99 static WCHAR const ips32_keyname[15] = {
100 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
101 '3', '2', 0 };
102 static WCHAR const progid_keyname[7] = {
103 'P', 'r', 'o', 'g', 'I', 'D', 0 };
104 static WCHAR const shellex_keyname[8] = {
105 's', 'h', 'e', 'l', 'l', 'e', 'x', 0 };
106 static WCHAR const shellfolder_keyname[12] = {
107 'S', 'h', 'e', 'l', 'l', 'F', 'o', 'l', 'd', 'e', 'r', 0 };
108 static WCHAR const mcdm_keyname[21] = {
109 'M', 'a', 'y', 'C', 'h', 'a', 'n', 'g', 'e', 'D', 'e', 'f',
110 'a', 'u', 'l', 't', 'M', 'e', 'n', 'u', 0 };
111 static char const tmodel_valuename[] = "ThreadingModel";
112 static char const wfparsing_valuename[] = "WantsFORPARSING";
114 /***********************************************************************
115 * static helper functions
117 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
118 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
119 WCHAR const *value);
120 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
121 char const *value);
122 static LONG recursive_delete_key(HKEY key);
123 static LONG recursive_delete_keyA(HKEY base, char const *name);
124 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name);
126 /***********************************************************************
127 * register_interfaces
129 static HRESULT register_interfaces(struct regsvr_interface const *list)
131 LONG res = ERROR_SUCCESS;
132 HKEY interface_key;
134 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
135 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
136 if (res != ERROR_SUCCESS) goto error_return;
138 for (; res == ERROR_SUCCESS && list->iid; ++list) {
139 WCHAR buf[39];
140 HKEY iid_key;
142 StringFromGUID2(list->iid, buf, 39);
143 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
144 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
145 if (res != ERROR_SUCCESS) goto error_close_interface_key;
147 if (list->name) {
148 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
149 (CONST BYTE*)(list->name),
150 strlen(list->name) + 1);
151 if (res != ERROR_SUCCESS) goto error_close_iid_key;
154 if (list->base_iid) {
155 register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
156 if (res != ERROR_SUCCESS) goto error_close_iid_key;
159 if (0 <= list->num_methods) {
160 static WCHAR const fmt[3] = { '%', 'd', 0 };
161 HKEY key;
163 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
164 KEY_READ | KEY_WRITE, NULL, &key, NULL);
165 if (res != ERROR_SUCCESS) goto error_close_iid_key;
167 wsprintfW(buf, fmt, list->num_methods);
168 res = RegSetValueExW(key, NULL, 0, REG_SZ,
169 (CONST BYTE*)buf,
170 (lstrlenW(buf) + 1) * sizeof(WCHAR));
171 RegCloseKey(key);
173 if (res != ERROR_SUCCESS) goto error_close_iid_key;
176 if (list->ps_clsid) {
177 register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
178 if (res != ERROR_SUCCESS) goto error_close_iid_key;
181 if (list->ps_clsid32) {
182 register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
183 if (res != ERROR_SUCCESS) goto error_close_iid_key;
186 error_close_iid_key:
187 RegCloseKey(iid_key);
190 error_close_interface_key:
191 RegCloseKey(interface_key);
192 error_return:
193 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
196 /***********************************************************************
197 * unregister_interfaces
199 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
201 LONG res = ERROR_SUCCESS;
202 HKEY interface_key;
204 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
205 KEY_READ | KEY_WRITE, &interface_key);
206 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
207 if (res != ERROR_SUCCESS) goto error_return;
209 for (; res == ERROR_SUCCESS && list->iid; ++list) {
210 WCHAR buf[39];
212 StringFromGUID2(list->iid, buf, 39);
213 res = recursive_delete_keyW(interface_key, buf);
216 RegCloseKey(interface_key);
217 error_return:
218 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
221 /***********************************************************************
222 * register_coclasses
224 static HRESULT register_coclasses(struct regsvr_coclass const *list)
226 LONG res = ERROR_SUCCESS;
227 HKEY coclass_key;
229 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
230 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
231 if (res != ERROR_SUCCESS) goto error_return;
233 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
234 WCHAR buf[39];
235 HKEY clsid_key;
237 StringFromGUID2(list->clsid, buf, 39);
238 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
239 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
240 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
242 if (list->name) {
243 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
244 (CONST BYTE*)(list->name),
245 strlen(list->name) + 1);
246 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
249 if (list->ips) {
250 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
251 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
254 if (list->ips32) {
255 HKEY ips32_key;
257 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
258 KEY_READ | KEY_WRITE, NULL,
259 &ips32_key, NULL);
260 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
262 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
263 (CONST BYTE*)list->ips32,
264 lstrlenA(list->ips32) + 1);
265 if (res == ERROR_SUCCESS && list->ips32_tmodel)
266 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
267 (CONST BYTE*)list->ips32_tmodel,
268 strlen(list->ips32_tmodel) + 1);
269 RegCloseKey(ips32_key);
270 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
273 if (list->flags & SHELLEX_MAYCHANGEDEFAULTMENU) {
274 HKEY shellex_key, mcdm_key;
276 res = RegCreateKeyExW(clsid_key, shellex_keyname, 0, NULL, 0,
277 KEY_READ | KEY_WRITE, NULL,
278 &shellex_key, NULL);
279 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
280 res = RegCreateKeyExW(shellex_key, mcdm_keyname, 0, NULL, 0,
281 KEY_READ | KEY_WRITE, NULL,
282 &mcdm_key, NULL);
283 RegCloseKey(shellex_key);
284 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
285 RegCloseKey(mcdm_key);
288 if (list->flags & SHELLFOLDER_WANTSFORPARSING) {
289 HKEY shellfolder_key;
291 res = RegCreateKeyExW(clsid_key, shellfolder_keyname, 0, NULL, 0,
292 KEY_READ | KEY_WRITE, NULL,
293 &shellfolder_key, NULL);
294 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
295 res = RegSetValueExA(shellfolder_key, wfparsing_valuename, 0, REG_SZ,
296 "", 1);
297 RegCloseKey(shellfolder_key);
298 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
301 if (list->clsid_str) {
302 res = register_key_defvalueA(clsid_key, clsid_keyname,
303 list->clsid_str);
304 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
307 if (list->progid) {
308 HKEY progid_key;
310 res = register_key_defvalueA(clsid_key, progid_keyname,
311 list->progid);
312 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
314 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
315 NULL, 0, KEY_READ | KEY_WRITE, NULL,
316 &progid_key, NULL);
317 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
319 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
320 RegCloseKey(progid_key);
321 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
324 error_close_clsid_key:
325 RegCloseKey(clsid_key);
328 error_close_coclass_key:
329 RegCloseKey(coclass_key);
330 error_return:
331 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
334 /***********************************************************************
335 * unregister_coclasses
337 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
339 LONG res = ERROR_SUCCESS;
340 HKEY coclass_key;
342 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
343 KEY_READ | KEY_WRITE, &coclass_key);
344 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
345 if (res != ERROR_SUCCESS) goto error_return;
347 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
348 WCHAR buf[39];
350 StringFromGUID2(list->clsid, buf, 39);
351 res = recursive_delete_keyW(coclass_key, buf);
352 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
354 if (list->progid) {
355 res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->progid);
356 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
360 error_close_coclass_key:
361 RegCloseKey(coclass_key);
362 error_return:
363 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
366 /***********************************************************************
367 * regsvr_key_guid
369 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
371 WCHAR buf[39];
373 StringFromGUID2(guid, buf, 39);
374 return register_key_defvalueW(base, name, buf);
377 /***********************************************************************
378 * regsvr_key_defvalueW
380 static LONG register_key_defvalueW(
381 HKEY base,
382 WCHAR const *name,
383 WCHAR const *value)
385 LONG res;
386 HKEY key;
388 res = RegCreateKeyExW(base, name, 0, NULL, 0,
389 KEY_READ | KEY_WRITE, NULL, &key, NULL);
390 if (res != ERROR_SUCCESS) return res;
391 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
392 (lstrlenW(value) + 1) * sizeof(WCHAR));
393 RegCloseKey(key);
394 return res;
397 /***********************************************************************
398 * regsvr_key_defvalueA
400 static LONG register_key_defvalueA(
401 HKEY base,
402 WCHAR const *name,
403 char const *value)
405 LONG res;
406 HKEY key;
408 res = RegCreateKeyExW(base, name, 0, NULL, 0,
409 KEY_READ | KEY_WRITE, NULL, &key, NULL);
410 if (res != ERROR_SUCCESS) return res;
411 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
412 lstrlenA(value) + 1);
413 RegCloseKey(key);
414 return res;
417 /***********************************************************************
418 * recursive_delete_key
420 static LONG recursive_delete_key(HKEY key)
422 LONG res;
423 WCHAR subkey_name[MAX_PATH];
424 DWORD cName;
425 HKEY subkey;
427 for (;;) {
428 cName = sizeof(subkey_name) / sizeof(WCHAR);
429 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
430 NULL, NULL, NULL, NULL);
431 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
432 res = ERROR_SUCCESS; /* presumably we're done enumerating */
433 break;
435 res = RegOpenKeyExW(key, subkey_name, 0,
436 KEY_READ | KEY_WRITE, &subkey);
437 if (res == ERROR_FILE_NOT_FOUND) continue;
438 if (res != ERROR_SUCCESS) break;
440 res = recursive_delete_key(subkey);
441 RegCloseKey(subkey);
442 if (res != ERROR_SUCCESS) break;
445 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
446 return res;
449 /***********************************************************************
450 * recursive_delete_keyA
452 static LONG recursive_delete_keyA(HKEY base, char const *name)
454 LONG res;
455 HKEY key;
457 res = RegOpenKeyExA(base, name, 0, KEY_READ | KEY_WRITE, &key);
458 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
459 if (res != ERROR_SUCCESS) return res;
460 res = recursive_delete_key(key);
461 RegCloseKey(key);
462 return res;
465 /***********************************************************************
466 * recursive_delete_keyW
468 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name)
470 LONG res;
471 HKEY key;
473 res = RegOpenKeyExW(base, name, 0, KEY_READ | KEY_WRITE, &key);
474 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
475 if (res != ERROR_SUCCESS) return res;
476 res = recursive_delete_key(key);
477 RegCloseKey(key);
478 return res;
481 /***********************************************************************
482 * coclass list
484 static GUID const CLSID_Desktop = {
485 0x00021400, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
487 static GUID const CLSID_Shortcut = {
488 0x00021401, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
490 static struct regsvr_coclass const coclass_list[] = {
491 { &CLSID_Desktop,
492 "Desktop",
493 NULL,
494 "shell32.dll",
495 "Apartment"
497 { &CLSID_DragDropHelper,
498 "Shell Drag and Drop Helper",
499 NULL,
500 "shell32.dll",
501 "Apartment"
503 { &CLSID_MyComputer,
504 "My Computer",
505 NULL,
506 "shell32.dll",
507 "Apartment"
509 { &CLSID_Shortcut,
510 "Shortcut",
511 NULL,
512 "shell32.dll",
513 "Apartment",
514 SHELLEX_MAYCHANGEDEFAULTMENU
516 { &CLSID_AutoComplete,
517 "AutoComplete",
518 NULL,
519 "shell32.dll",
520 "Apartment",
522 { &CLSID_UnixFolder,
523 "/",
524 NULL,
525 "shell32.dll",
526 "Apartment",
527 SHELLFOLDER_WANTSFORPARSING
529 { &CLSID_UnixDosFolder,
530 "/",
531 NULL,
532 "shell32.dll",
533 "Apartment",
534 SHELLFOLDER_WANTSFORPARSING
536 { NULL } /* list terminator */
539 /***********************************************************************
540 * interface list
543 static struct regsvr_interface const interface_list[] = {
544 { NULL } /* list terminator */
547 /***********************************************************************
548 * DllRegisterServer (SHELL32.@)
550 HRESULT WINAPI SHELL32_DllRegisterServer()
552 HRESULT hr;
554 TRACE("\n");
556 hr = register_coclasses(coclass_list);
557 if (SUCCEEDED(hr))
558 hr = register_interfaces(interface_list);
559 if (SUCCEEDED(hr))
560 hr = SHELL_RegisterShellFolders();
561 return hr;
564 /***********************************************************************
565 * DllUnregisterServer (SHELL32.@)
567 HRESULT WINAPI SHELL32_DllUnregisterServer()
569 HRESULT hr;
571 TRACE("\n");
573 hr = unregister_coclasses(coclass_list);
574 if (SUCCEEDED(hr))
575 hr = unregister_interfaces(interface_list);
576 return hr;