2 * self-registerable dll functions for ole32.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 "compobj_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
43 * Near the bottom of this file are the exported DllRegisterServer and
44 * DllUnregisterServer, which make all this worthwhile.
47 /***********************************************************************
48 * interface for self-registering
50 struct regsvr_interface
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 */
60 static HRESULT
register_interfaces(struct regsvr_interface
const *list
);
61 static HRESULT
unregister_interfaces(struct regsvr_interface
const *list
);
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 */
72 static HRESULT
register_coclasses(struct regsvr_coclass
const *list
);
73 static HRESULT
unregister_coclasses(struct regsvr_coclass
const *list
);
75 /***********************************************************************
76 * static string constants
78 static WCHAR
const interface_keyname
[10] = {
79 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
80 static WCHAR
const base_ifa_keyname
[14] = {
81 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
83 static WCHAR
const num_methods_keyname
[11] = {
84 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
85 static WCHAR
const ps_clsid_keyname
[15] = {
86 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
88 static WCHAR
const ps_clsid32_keyname
[17] = {
89 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
90 'i', 'd', '3', '2', 0 };
91 static WCHAR
const clsid_keyname
[6] = {
92 'C', 'L', 'S', 'I', 'D', 0 };
93 static WCHAR
const ips_keyname
[13] = {
94 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
96 static WCHAR
const ips32_keyname
[15] = {
97 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
99 static char const tmodel_valuename
[] = "ThreadingModel";
101 /***********************************************************************
102 * static helper functions
104 static LONG
register_key_guid(HKEY base
, WCHAR
const *name
, GUID
const *guid
);
105 static LONG
register_key_defvalueW(HKEY base
, WCHAR
const *name
,
107 static LONG
register_key_defvalueA(HKEY base
, WCHAR
const *name
,
109 static LONG
recursive_delete_key(HKEY key
);
112 /***********************************************************************
113 * register_interfaces
115 static HRESULT
register_interfaces(struct regsvr_interface
const *list
)
117 LONG res
= ERROR_SUCCESS
;
120 res
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, interface_keyname
, 0, NULL
, 0,
121 KEY_READ
| KEY_WRITE
, NULL
, &interface_key
, NULL
);
122 if (res
!= ERROR_SUCCESS
) goto error_return
;
124 for (; res
== ERROR_SUCCESS
&& list
->iid
; ++list
) {
128 StringFromGUID2(list
->iid
, buf
, 39);
129 res
= RegCreateKeyExW(interface_key
, buf
, 0, NULL
, 0,
130 KEY_READ
| KEY_WRITE
, NULL
, &iid_key
, NULL
);
131 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
134 res
= RegSetValueExA(iid_key
, NULL
, 0, REG_SZ
,
135 (CONST BYTE
*)(list
->name
),
136 strlen(list
->name
) + 1);
137 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
140 if (list
->base_iid
) {
141 res
= register_key_guid(iid_key
, base_ifa_keyname
, list
->base_iid
);
142 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
145 if (0 <= list
->num_methods
) {
146 static WCHAR
const fmt
[3] = { '%', 'd', 0 };
149 res
= RegCreateKeyExW(iid_key
, num_methods_keyname
, 0, NULL
, 0,
150 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
151 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
153 wsprintfW(buf
, fmt
, list
->num_methods
);
154 res
= RegSetValueExW(key
, NULL
, 0, REG_SZ
,
156 (lstrlenW(buf
) + 1) * sizeof(WCHAR
));
159 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
162 if (list
->ps_clsid
) {
163 register_key_guid(iid_key
, ps_clsid_keyname
, list
->ps_clsid
);
164 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
167 if (list
->ps_clsid32
) {
168 register_key_guid(iid_key
, ps_clsid32_keyname
, list
->ps_clsid32
);
169 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
173 RegCloseKey(iid_key
);
176 error_close_interface_key
:
177 RegCloseKey(interface_key
);
179 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
182 /***********************************************************************
183 * unregister_interfaces
185 static HRESULT
unregister_interfaces(struct regsvr_interface
const *list
)
187 LONG res
= ERROR_SUCCESS
;
190 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, interface_keyname
, 0,
191 KEY_READ
| KEY_WRITE
, &interface_key
);
192 if (res
== ERROR_FILE_NOT_FOUND
) return S_OK
;
193 if (res
!= ERROR_SUCCESS
) goto error_return
;
195 for (; res
== ERROR_SUCCESS
&& list
->iid
; ++list
) {
199 StringFromGUID2(list
->iid
, buf
, 39);
200 res
= RegOpenKeyExW(interface_key
, buf
, 0,
201 KEY_READ
| KEY_WRITE
, &iid_key
);
202 if (res
== ERROR_FILE_NOT_FOUND
) {
206 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
207 res
= recursive_delete_key(iid_key
);
208 RegCloseKey(iid_key
);
209 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
212 error_close_interface_key
:
213 RegCloseKey(interface_key
);
215 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
218 /***********************************************************************
221 static HRESULT
register_coclasses(struct regsvr_coclass
const *list
)
223 LONG res
= ERROR_SUCCESS
;
226 res
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, clsid_keyname
, 0, NULL
, 0,
227 KEY_READ
| KEY_WRITE
, NULL
, &coclass_key
, NULL
);
228 if (res
!= ERROR_SUCCESS
) goto error_return
;
230 for (; res
== ERROR_SUCCESS
&& list
->clsid
; ++list
) {
234 StringFromGUID2(list
->clsid
, buf
, 39);
235 res
= RegCreateKeyExW(coclass_key
, buf
, 0, NULL
, 0,
236 KEY_READ
| KEY_WRITE
, NULL
, &clsid_key
, NULL
);
237 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
240 res
= RegSetValueExA(clsid_key
, NULL
, 0, REG_SZ
,
241 (CONST BYTE
*)(list
->name
),
242 strlen(list
->name
) + 1);
243 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
247 res
= register_key_defvalueA(clsid_key
, ips_keyname
, list
->ips
);
248 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
254 res
= RegCreateKeyExW(clsid_key
, ips32_keyname
, 0, NULL
, 0,
255 KEY_READ
| KEY_WRITE
, NULL
,
257 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
259 res
= RegSetValueExA(ips32_key
, NULL
, 0, REG_SZ
,
260 (CONST BYTE
*)list
->ips32
,
261 lstrlenA(list
->ips32
) + 1);
262 if (res
== ERROR_SUCCESS
&& list
->ips32_tmodel
)
263 res
= RegSetValueExA(ips32_key
, tmodel_valuename
, 0, REG_SZ
,
264 (CONST BYTE
*)list
->ips32_tmodel
,
265 strlen(list
->ips32_tmodel
) + 1);
266 RegCloseKey(ips32_key
);
267 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
270 error_close_clsid_key
:
271 RegCloseKey(clsid_key
);
274 error_close_coclass_key
:
275 RegCloseKey(coclass_key
);
277 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
280 /***********************************************************************
281 * unregister_coclasses
283 static HRESULT
unregister_coclasses(struct regsvr_coclass
const *list
)
285 LONG res
= ERROR_SUCCESS
;
288 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, clsid_keyname
, 0,
289 KEY_READ
| KEY_WRITE
, &coclass_key
);
290 if (res
== ERROR_FILE_NOT_FOUND
) return S_OK
;
291 if (res
!= ERROR_SUCCESS
) goto error_return
;
293 for (; res
== ERROR_SUCCESS
&& list
->clsid
; ++list
) {
297 StringFromGUID2(list
->clsid
, buf
, 39);
298 res
= RegOpenKeyExW(coclass_key
, buf
, 0,
299 KEY_READ
| KEY_WRITE
, &clsid_key
);
300 if (res
== ERROR_FILE_NOT_FOUND
) {
304 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
305 res
= recursive_delete_key(clsid_key
);
306 RegCloseKey(clsid_key
);
307 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
310 error_close_coclass_key
:
311 RegCloseKey(coclass_key
);
313 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
316 /***********************************************************************
319 static LONG
register_key_guid(HKEY base
, WCHAR
const *name
, GUID
const *guid
)
323 StringFromGUID2(guid
, buf
, 39);
324 return register_key_defvalueW(base
, name
, buf
);
327 /***********************************************************************
328 * regsvr_key_defvalueW
330 static LONG
register_key_defvalueW(
338 res
= RegCreateKeyExW(base
, name
, 0, NULL
, 0,
339 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
340 if (res
!= ERROR_SUCCESS
) return res
;
341 res
= RegSetValueExW(key
, NULL
, 0, REG_SZ
, (CONST BYTE
*)value
,
342 (lstrlenW(value
) + 1) * sizeof(WCHAR
));
347 /***********************************************************************
348 * regsvr_key_defvalueA
350 static LONG
register_key_defvalueA(
358 res
= RegCreateKeyExW(base
, name
, 0, NULL
, 0,
359 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
360 if (res
!= ERROR_SUCCESS
) return res
;
361 res
= RegSetValueExA(key
, NULL
, 0, REG_SZ
, (CONST BYTE
*)value
,
362 lstrlenA(value
) + 1);
367 /***********************************************************************
368 * recursive_delete_key
370 static LONG
recursive_delete_key(HKEY key
)
373 WCHAR subkey_name
[MAX_PATH
];
378 cName
= sizeof(subkey_name
) / sizeof(WCHAR
);
379 res
= RegEnumKeyExW(key
, 0, subkey_name
, &cName
,
380 NULL
, NULL
, NULL
, NULL
);
381 if (res
!= ERROR_SUCCESS
&& res
!= ERROR_MORE_DATA
) {
382 res
= ERROR_SUCCESS
; /* presumably we're done enumerating */
385 res
= RegOpenKeyExW(key
, subkey_name
, 0,
386 KEY_READ
| KEY_WRITE
, &subkey
);
387 if (res
== ERROR_FILE_NOT_FOUND
) continue;
388 if (res
!= ERROR_SUCCESS
) break;
390 res
= recursive_delete_key(subkey
);
392 if (res
!= ERROR_SUCCESS
) break;
395 if (res
== ERROR_SUCCESS
) res
= RegDeleteKeyW(key
, 0);
399 /***********************************************************************
402 static GUID
const CLSID_PointerMoniker
= {
403 0x00000306, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
405 static GUID
const CLSID_PackagerMoniker
= {
406 0x00000308, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
408 static struct regsvr_coclass
const coclass_list
[] = {
409 { &CLSID_FileMoniker
,
415 { &CLSID_ItemMoniker
,
421 { &CLSID_AntiMoniker
,
427 { &CLSID_PointerMoniker
,
433 { &CLSID_PackagerMoniker
,
439 { &CLSID_CompositeMoniker
,
451 { &CLSID_ClassMoniker
,
457 { &CLSID_PSFactoryBuffer
,
463 { &CLSID_StdGlobalInterfaceTable
,
464 "StdGlobalInterfaceTable",
469 { NULL
} /* list terminator */
472 /***********************************************************************
476 #define INTERFACE_ENTRY(interface, base, clsid32, clsid16) { &IID_##interface, #interface, base, sizeof(interface##Vtbl)/sizeof(void*), clsid16, clsid32 }
477 #define STD_INTERFACE_ENTRY(interface) INTERFACE_ENTRY(interface, NULL, &CLSID_PSFactoryBuffer, NULL)
478 #define LCL_INTERFACE_ENTRY(interface) INTERFACE_ENTRY(interface, NULL, NULL, NULL)
480 static const struct regsvr_interface interface_list
[] = {
481 LCL_INTERFACE_ENTRY(IUnknown
),
482 STD_INTERFACE_ENTRY(IClassFactory
),
483 LCL_INTERFACE_ENTRY(IMalloc
),
484 LCL_INTERFACE_ENTRY(IMarshal
),
485 STD_INTERFACE_ENTRY(IStorage
),
486 LCL_INTERFACE_ENTRY(IMessageFilter
),
487 LCL_INTERFACE_ENTRY(IStdMarshalInfo
),
488 LCL_INTERFACE_ENTRY(IExternalConnection
),
489 LCL_INTERFACE_ENTRY(IMallocSpy
),
490 LCL_INTERFACE_ENTRY(IMultiQI
),
491 STD_INTERFACE_ENTRY(IStream
),
492 STD_INTERFACE_ENTRY(IPersistStorage
),
493 STD_INTERFACE_ENTRY(IDataObject
),
494 STD_INTERFACE_ENTRY(IAdviseSink
),
495 LCL_INTERFACE_ENTRY(IDataAdviseHolder
),
496 LCL_INTERFACE_ENTRY(IOleAdviseHolder
),
497 STD_INTERFACE_ENTRY(IOleObject
),
498 STD_INTERFACE_ENTRY(IOleClientSite
),
499 LCL_INTERFACE_ENTRY(IDropSource
),
500 STD_INTERFACE_ENTRY(IRemUnknown
),
501 LCL_INTERFACE_ENTRY(IClientSecurity
),
502 LCL_INTERFACE_ENTRY(IServerSecurity
),
503 { NULL
} /* list terminator */
506 /***********************************************************************
507 * DllRegisterServer (OLE32.@)
509 HRESULT WINAPI
DllRegisterServer()
515 hr
= register_coclasses(coclass_list
);
517 hr
= register_interfaces(interface_list
);
521 /***********************************************************************
522 * DllUnregisterServer (OLE32.@)
524 HRESULT WINAPI
DllUnregisterServer()
530 hr
= unregister_coclasses(coclass_list
);
532 hr
= unregister_interfaces(interface_list
);