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
33 #include "shell32_main.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 * Near the bottom of this file are the exported DllRegisterServer and
42 * DllUnregisterServer, which make all this worthwhile.
45 /***********************************************************************
46 * interface for self-registering
48 struct regsvr_interface
50 IID const *iid; /* NULL for end of list */
51 LPCSTR name; /* can be NULL to omit */
52 IID const *base_iid; /* can be NULL to omit */
53 int num_methods; /* can be <0 to omit */
54 CLSID const *ps_clsid; /* can be NULL to omit */
55 CLSID const *ps_clsid32; /* can be NULL to omit */
58 static HRESULT register_interfaces(struct regsvr_interface const *list);
59 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
63 CLSID const *clsid; /* NULL for end of list */
64 LPCSTR name; /* can be NULL to omit */
65 LPCSTR ips; /* can be NULL to omit */
66 LPCSTR ips32; /* can be NULL to omit */
67 LPCSTR ips32_tmodel; /* can be NULL to omit */
70 DWORD dwCallForAttributes;
71 LPCSTR clsid_str; /* can be NULL to omit */
72 LPCSTR progid; /* can be NULL to omit */
75 /* flags for regsvr_coclass.flags */
76 #define SHELLEX_MAYCHANGEDEFAULTMENU 0x00000001
77 #define SHELLFOLDER_WANTSFORPARSING 0x00000002
78 #define SHELLFOLDER_ATTRIBUTES 0x00000004
79 #define SHELLFOLDER_CALLFORATTRIBUTES 0x00000008
81 static HRESULT register_coclasses(struct regsvr_coclass const *list);
82 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
84 struct regsvr_namespace
86 CLSID const *clsid; /* CLSID of the namespace extension. NULL for end of list */
87 LPCWSTR parent; /* Mount point (MyComputer, Desktop, ..). */
88 LPCWSTR value; /* Display name of the extension. */
91 static HRESULT register_namespace_extensions(struct regsvr_namespace const *list);
92 static HRESULT unregister_namespace_extensions(struct regsvr_namespace const *list);
94 /***********************************************************************
95 * static string constants
97 static WCHAR const interface_keyname[10] = {
98 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
99 static WCHAR const base_ifa_keyname[14] = {
100 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
102 static WCHAR const num_methods_keyname[11] = {
103 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
104 static WCHAR const ps_clsid_keyname[15] = {
105 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
107 static WCHAR const ps_clsid32_keyname[17] = {
108 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
109 'i', 'd', '3', '2', 0 };
110 static WCHAR const clsid_keyname[6] = {
111 'C', 'L', 'S', 'I', 'D', 0 };
112 static WCHAR const ips_keyname[13] = {
113 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
115 static WCHAR const ips32_keyname[15] = {
116 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
118 static WCHAR const progid_keyname[7] = {
119 'P', 'r', 'o', 'g', 'I', 'D', 0 };
120 static WCHAR const shellex_keyname[8] = {
121 's', 'h', 'e', 'l', 'l', 'e', 'x', 0 };
122 static WCHAR const shellfolder_keyname[12] = {
123 'S', 'h', 'e', 'l', 'l', 'F', 'o', 'l', 'd', 'e', 'r', 0 };
124 static WCHAR const mcdm_keyname[21] = {
125 'M', 'a', 'y', 'C', 'h', 'a', 'n', 'g', 'e', 'D', 'e', 'f',
126 'a', 'u', 'l', 't', 'M', 'e', 'n', 'u', 0 };
127 static char const tmodel_valuename[] = "ThreadingModel";
128 static char const wfparsing_valuename[] = "WantsFORPARSING";
129 static char const attributes_valuename[] = "Attributes";
130 static char const cfattributes_valuename[] = "CallForAttributes";
132 /***********************************************************************
133 * static helper functions
135 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
136 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
138 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
140 static LONG recursive_delete_key(HKEY key);
141 static LONG recursive_delete_keyA(HKEY base, char const *name);
142 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name);
144 /***********************************************************************
145 * register_interfaces
147 static HRESULT register_interfaces(struct regsvr_interface const *list)
149 LONG res = ERROR_SUCCESS;
152 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
153 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
154 if (res != ERROR_SUCCESS) goto error_return;
156 for (; res == ERROR_SUCCESS && list->iid; ++list) {
160 StringFromGUID2(list->iid, buf, 39);
161 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
162 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
163 if (res != ERROR_SUCCESS) goto error_close_interface_key;
166 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
167 (CONST BYTE*)(list->name),
168 strlen(list->name) + 1);
169 if (res != ERROR_SUCCESS) goto error_close_iid_key;
172 if (list->base_iid) {
173 register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
174 if (res != ERROR_SUCCESS) goto error_close_iid_key;
177 if (0 <= list->num_methods) {
178 static WCHAR const fmt[3] = { '%', 'd', 0 };
181 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
182 KEY_READ | KEY_WRITE, NULL, &key, NULL);
183 if (res != ERROR_SUCCESS) goto error_close_iid_key;
185 wsprintfW(buf, fmt, list->num_methods);
186 res = RegSetValueExW(key, NULL, 0, REG_SZ,
188 (lstrlenW(buf) + 1) * sizeof(WCHAR));
191 if (res != ERROR_SUCCESS) goto error_close_iid_key;
194 if (list->ps_clsid) {
195 register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
196 if (res != ERROR_SUCCESS) goto error_close_iid_key;
199 if (list->ps_clsid32) {
200 register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
201 if (res != ERROR_SUCCESS) goto error_close_iid_key;
205 RegCloseKey(iid_key);
208 error_close_interface_key:
209 RegCloseKey(interface_key);
211 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
214 /***********************************************************************
215 * unregister_interfaces
217 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
219 LONG res = ERROR_SUCCESS;
222 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
223 KEY_READ | KEY_WRITE, &interface_key);
224 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
225 if (res != ERROR_SUCCESS) goto error_return;
227 for (; res == ERROR_SUCCESS && list->iid; ++list) {
230 StringFromGUID2(list->iid, buf, 39);
231 res = recursive_delete_keyW(interface_key, buf);
234 RegCloseKey(interface_key);
236 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
239 /***********************************************************************
242 static HRESULT register_coclasses(struct regsvr_coclass const *list)
244 LONG res = ERROR_SUCCESS;
247 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
248 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
249 if (res != ERROR_SUCCESS) goto error_return;
251 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
255 StringFromGUID2(list->clsid, buf, 39);
256 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
257 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
258 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
261 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
262 (CONST BYTE*)(list->name),
263 strlen(list->name) + 1);
264 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
268 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
269 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
275 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
276 KEY_READ | KEY_WRITE, NULL,
278 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
280 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
281 (CONST BYTE*)list->ips32,
282 lstrlenA(list->ips32) + 1);
283 if (res == ERROR_SUCCESS && list->ips32_tmodel)
284 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
285 (CONST BYTE*)list->ips32_tmodel,
286 strlen(list->ips32_tmodel) + 1);
287 RegCloseKey(ips32_key);
288 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
291 if (list->flags & SHELLEX_MAYCHANGEDEFAULTMENU) {
292 HKEY shellex_key, mcdm_key;
294 res = RegCreateKeyExW(clsid_key, shellex_keyname, 0, NULL, 0,
295 KEY_READ | KEY_WRITE, NULL,
297 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
298 res = RegCreateKeyExW(shellex_key, mcdm_keyname, 0, NULL, 0,
299 KEY_READ | KEY_WRITE, NULL,
301 RegCloseKey(shellex_key);
302 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
303 RegCloseKey(mcdm_key);
307 (SHELLFOLDER_WANTSFORPARSING|SHELLFOLDER_ATTRIBUTES|SHELLFOLDER_CALLFORATTRIBUTES))
309 HKEY shellfolder_key;
311 res = RegCreateKeyExW(clsid_key, shellfolder_keyname, 0, NULL, 0,
312 KEY_READ | KEY_WRITE, NULL,
313 &shellfolder_key, NULL);
314 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
315 if (list->flags & SHELLFOLDER_WANTSFORPARSING)
316 res = RegSetValueExA(shellfolder_key, wfparsing_valuename, 0, REG_SZ, (LPBYTE)"", 1);
317 if (list->flags & SHELLFOLDER_ATTRIBUTES)
318 res = RegSetValueExA(shellfolder_key, attributes_valuename, 0, REG_DWORD,
319 (LPBYTE)&list->dwAttributes, sizeof(DWORD));
320 if (list->flags & SHELLFOLDER_CALLFORATTRIBUTES)
321 res = RegSetValueExA(shellfolder_key, cfattributes_valuename, 0, REG_DWORD,
322 (LPBYTE)&list->dwCallForAttributes, sizeof(DWORD));
323 RegCloseKey(shellfolder_key);
324 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
327 if (list->clsid_str) {
328 res = register_key_defvalueA(clsid_key, clsid_keyname,
330 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
336 res = register_key_defvalueA(clsid_key, progid_keyname,
338 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
340 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
341 NULL, 0, KEY_READ | KEY_WRITE, NULL,
343 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
345 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
346 RegCloseKey(progid_key);
347 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
350 error_close_clsid_key:
351 RegCloseKey(clsid_key);
354 error_close_coclass_key:
355 RegCloseKey(coclass_key);
357 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
360 /***********************************************************************
361 * unregister_coclasses
363 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
365 LONG res = ERROR_SUCCESS;
368 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
369 KEY_READ | KEY_WRITE, &coclass_key);
370 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
371 if (res != ERROR_SUCCESS) goto error_return;
373 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
376 StringFromGUID2(list->clsid, buf, 39);
377 res = recursive_delete_keyW(coclass_key, buf);
378 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
381 res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->progid);
382 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
386 error_close_coclass_key:
387 RegCloseKey(coclass_key);
389 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
392 /**********************************************************************
393 * register_namespace_extensions
395 static WCHAR *get_namespace_key(struct regsvr_namespace const *list) {
396 static const WCHAR wszExplorerKey[] = {
397 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
398 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
399 'E','x','p','l','o','r','e','r','\\',0 };
400 static const WCHAR wszNamespace[] = { '\\','N','a','m','e','s','p','a','c','e','\\',0 };
401 WCHAR *pwszKey, *pwszCLSID;
403 pwszKey = HeapAlloc(GetProcessHeap(), 0, sizeof(wszExplorerKey)+sizeof(wszNamespace)+
404 sizeof(WCHAR)*(lstrlenW(list->parent)+CHARS_IN_GUID));
408 lstrcpyW(pwszKey, wszExplorerKey);
409 lstrcatW(pwszKey, list->parent);
410 lstrcatW(pwszKey, wszNamespace);
411 if (FAILED(StringFromCLSID(list->clsid, &pwszCLSID))) {
412 HeapFree(GetProcessHeap(), 0, pwszKey);
415 lstrcatW(pwszKey, pwszCLSID);
416 CoTaskMemFree(pwszCLSID);
421 static HRESULT register_namespace_extensions(struct regsvr_namespace const *list) {
425 for (; list->clsid; list++) {
426 pwszKey = get_namespace_key(list);
428 /* Create the key and set the value. */
429 if (pwszKey && ERROR_SUCCESS ==
430 RegCreateKeyExW(HKEY_LOCAL_MACHINE, pwszKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL))
432 RegSetValueExW(hKey, NULL, 0, REG_SZ, (LPBYTE)list->value, sizeof(WCHAR)*(lstrlenW(list->value)+1));
436 HeapFree(GetProcessHeap(), 0, pwszKey);
441 static HRESULT unregister_namespace_extensions(struct regsvr_namespace const *list) {
444 for (; list->clsid; list++) {
445 pwszKey = get_namespace_key(list);
446 RegDeleteKeyW(HKEY_LOCAL_MACHINE, pwszKey);
447 HeapFree(GetProcessHeap(), 0, pwszKey);
452 /***********************************************************************
455 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
459 StringFromGUID2(guid, buf, 39);
460 return register_key_defvalueW(base, name, buf);
463 /***********************************************************************
464 * regsvr_key_defvalueW
466 static LONG register_key_defvalueW(
474 res = RegCreateKeyExW(base, name, 0, NULL, 0,
475 KEY_READ | KEY_WRITE, NULL, &key, NULL);
476 if (res != ERROR_SUCCESS) return res;
477 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
478 (lstrlenW(value) + 1) * sizeof(WCHAR));
483 /***********************************************************************
484 * regsvr_key_defvalueA
486 static LONG register_key_defvalueA(
494 res = RegCreateKeyExW(base, name, 0, NULL, 0,
495 KEY_READ | KEY_WRITE, NULL, &key, NULL);
496 if (res != ERROR_SUCCESS) return res;
497 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
498 lstrlenA(value) + 1);
503 /***********************************************************************
504 * recursive_delete_key
506 static LONG recursive_delete_key(HKEY key)
509 WCHAR subkey_name[MAX_PATH];
514 cName = sizeof(subkey_name) / sizeof(WCHAR);
515 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
516 NULL, NULL, NULL, NULL);
517 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
518 res = ERROR_SUCCESS; /* presumably we're done enumerating */
521 res = RegOpenKeyExW(key, subkey_name, 0,
522 KEY_READ | KEY_WRITE, &subkey);
523 if (res == ERROR_FILE_NOT_FOUND) continue;
524 if (res != ERROR_SUCCESS) break;
526 res = recursive_delete_key(subkey);
528 if (res != ERROR_SUCCESS) break;
531 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
535 /***********************************************************************
536 * recursive_delete_keyA
538 static LONG recursive_delete_keyA(HKEY base, char const *name)
543 res = RegOpenKeyExA(base, name, 0, KEY_READ | KEY_WRITE, &key);
544 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
545 if (res != ERROR_SUCCESS) return res;
546 res = recursive_delete_key(key);
551 /***********************************************************************
552 * recursive_delete_keyW
554 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name)
559 res = RegOpenKeyExW(base, name, 0, KEY_READ | KEY_WRITE, &key);
560 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
561 if (res != ERROR_SUCCESS) return res;
562 res = recursive_delete_key(key);
567 /***********************************************************************
570 static GUID const CLSID_Desktop = {
571 0x00021400, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
573 static GUID const CLSID_Shortcut = {
574 0x00021401, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
576 static struct regsvr_coclass const coclass_list[] = {
583 { &CLSID_DragDropHelper,
584 "Shell Drag and Drop Helper",
600 SHELLEX_MAYCHANGEDEFAULTMENU
602 { &CLSID_AutoComplete,
613 SHELLFOLDER_WANTSFORPARSING
615 { &CLSID_UnixDosFolder,
620 SHELLFOLDER_WANTSFORPARSING|SHELLFOLDER_ATTRIBUTES|SHELLFOLDER_CALLFORATTRIBUTES,
621 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER,
624 { &CLSID_FolderShortcut,
629 SHELLFOLDER_ATTRIBUTES|SHELLFOLDER_CALLFORATTRIBUTES,
630 SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_LINK,
631 SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR
633 { &CLSID_MyDocuments,
638 SHELLFOLDER_WANTSFORPARSING|SHELLFOLDER_ATTRIBUTES|SHELLFOLDER_CALLFORATTRIBUTES,
639 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER,
642 { NULL } /* list terminator */
645 /***********************************************************************
649 static struct regsvr_interface const interface_list[] = {
650 { NULL } /* list terminator */
653 /***********************************************************************
654 * namespace extensions list
656 static const WCHAR wszDesktop[] = { 'D','e','s','k','t','o','p',0 };
657 static const WCHAR wszSlash[] = { '/', 0 };
658 static const WCHAR wszMyDocuments[] = { 'M','y',' ','D','o','c','u','m','e','n','t','s', 0 };
660 static struct regsvr_namespace const namespace_extensions_list[] = {
662 &CLSID_UnixDosFolder,
674 /***********************************************************************
675 * DllRegisterServer (SHELL32.@)
677 HRESULT WINAPI DllRegisterServer(void)
683 hr = register_coclasses(coclass_list);
685 hr = register_interfaces(interface_list);
687 hr = SHELL_RegisterShellFolders();
689 hr = register_namespace_extensions(namespace_extensions_list);
693 /***********************************************************************
694 * DllUnregisterServer (SHELL32.@)
696 HRESULT WINAPI DllUnregisterServer(void)
702 hr = unregister_coclasses(coclass_list);
704 hr = unregister_interfaces(interface_list);
706 hr = unregister_namespace_extensions(namespace_extensions_list);