2 * self-registerable dll functions for comcat.dll
4 * Copyright (C) 2002-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 #define COM_NO_WINDOWS_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
);
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 */
68 static HRESULT
register_coclasses(struct regsvr_coclass
const *list
);
69 static HRESULT
unregister_coclasses(struct regsvr_coclass
const *list
);
71 /***********************************************************************
72 * static string constants
74 static WCHAR
const interface_keyname
[10] = {
75 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
76 static WCHAR
const base_ifa_keyname
[14] = {
77 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
79 static WCHAR
const num_methods_keyname
[11] = {
80 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
81 static WCHAR
const ps_clsid_keyname
[15] = {
82 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
84 static WCHAR
const ps_clsid32_keyname
[17] = {
85 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
86 'i', 'd', '3', '2', 0 };
87 static WCHAR
const clsid_keyname
[6] = {
88 'C', 'L', 'S', 'I', 'D', 0 };
89 static WCHAR
const ips_keyname
[13] = {
90 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
92 static WCHAR
const ips32_keyname
[15] = {
93 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
95 static char const tmodel_valuename
[] = "ThreadingModel";
97 /***********************************************************************
98 * static helper functions
100 static LONG
register_key_guid(HKEY base
, WCHAR
const *name
, GUID
const *guid
);
101 static LONG
register_key_defvalueW(HKEY base
, WCHAR
const *name
,
103 static LONG
register_key_defvalueA(HKEY base
, WCHAR
const *name
,
105 static LONG
recursive_delete_key(HKEY key
);
108 /***********************************************************************
109 * register_interfaces
111 static HRESULT
register_interfaces(struct regsvr_interface
const *list
)
113 LONG res
= ERROR_SUCCESS
;
116 res
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, interface_keyname
, 0, NULL
, 0,
117 KEY_READ
| KEY_WRITE
, NULL
, &interface_key
, NULL
);
118 if (res
!= ERROR_SUCCESS
) goto error_return
;
120 for (; res
== ERROR_SUCCESS
&& list
->iid
; ++list
) {
124 StringFromGUID2(list
->iid
, buf
, 39);
125 res
= RegCreateKeyExW(interface_key
, buf
, 0, NULL
, 0,
126 KEY_READ
| KEY_WRITE
, NULL
, &iid_key
, NULL
);
127 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
130 res
= RegSetValueExA(iid_key
, NULL
, 0, REG_SZ
,
131 (CONST BYTE
*)(list
->name
),
132 strlen(list
->name
) + 1);
133 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
136 if (list
->base_iid
) {
137 register_key_guid(iid_key
, base_ifa_keyname
, list
->base_iid
);
138 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
141 if (0 <= list
->num_methods
) {
142 static WCHAR
const fmt
[3] = { '%', 'd', 0 };
145 res
= RegCreateKeyExW(iid_key
, num_methods_keyname
, 0, NULL
, 0,
146 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
147 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
149 wsprintfW(buf
, fmt
, list
->num_methods
);
150 res
= RegSetValueExW(key
, NULL
, 0, REG_SZ
,
152 (lstrlenW(buf
) + 1) * sizeof(WCHAR
));
155 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
158 if (list
->ps_clsid
) {
159 register_key_guid(iid_key
, ps_clsid_keyname
, list
->ps_clsid
);
160 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
163 if (list
->ps_clsid32
) {
164 register_key_guid(iid_key
, ps_clsid32_keyname
, list
->ps_clsid32
);
165 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
169 RegCloseKey(iid_key
);
172 error_close_interface_key
:
173 RegCloseKey(interface_key
);
175 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
178 /***********************************************************************
179 * unregister_interfaces
181 static HRESULT
unregister_interfaces(struct regsvr_interface
const *list
)
183 LONG res
= ERROR_SUCCESS
;
186 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, interface_keyname
, 0,
187 KEY_READ
| KEY_WRITE
, &interface_key
);
188 if (res
== ERROR_FILE_NOT_FOUND
) return S_OK
;
189 if (res
!= ERROR_SUCCESS
) goto error_return
;
191 for (; res
== ERROR_SUCCESS
&& list
->iid
; ++list
) {
195 StringFromGUID2(list
->iid
, buf
, 39);
196 res
= RegOpenKeyExW(interface_key
, buf
, 0,
197 KEY_READ
| KEY_WRITE
, &iid_key
);
198 if (res
== ERROR_FILE_NOT_FOUND
) {
202 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
203 res
= recursive_delete_key(iid_key
);
204 RegCloseKey(iid_key
);
205 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
208 error_close_interface_key
:
209 RegCloseKey(interface_key
);
211 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
214 /***********************************************************************
217 static HRESULT
register_coclasses(struct regsvr_coclass
const *list
)
219 LONG res
= ERROR_SUCCESS
;
222 res
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, clsid_keyname
, 0, NULL
, 0,
223 KEY_READ
| KEY_WRITE
, NULL
, &coclass_key
, NULL
);
224 if (res
!= ERROR_SUCCESS
) goto error_return
;
226 for (; res
== ERROR_SUCCESS
&& list
->clsid
; ++list
) {
230 StringFromGUID2(list
->clsid
, buf
, 39);
231 res
= RegCreateKeyExW(coclass_key
, buf
, 0, NULL
, 0,
232 KEY_READ
| KEY_WRITE
, NULL
, &clsid_key
, NULL
);
233 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
236 res
= RegSetValueExA(clsid_key
, NULL
, 0, REG_SZ
,
237 (CONST BYTE
*)(list
->name
),
238 strlen(list
->name
) + 1);
239 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
243 res
= register_key_defvalueA(clsid_key
, ips_keyname
, list
->ips
);
244 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
250 res
= RegCreateKeyExW(clsid_key
, ips32_keyname
, 0, NULL
, 0,
251 KEY_READ
| KEY_WRITE
, NULL
,
253 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
255 res
= RegSetValueExA(ips32_key
, NULL
, 0, REG_SZ
,
256 (CONST BYTE
*)list
->ips32
,
257 lstrlenA(list
->ips32
) + 1);
258 if (res
== ERROR_SUCCESS
&& list
->ips32_tmodel
)
259 res
= RegSetValueExA(ips32_key
, tmodel_valuename
, 0, REG_SZ
,
260 (CONST BYTE
*)list
->ips32_tmodel
,
261 strlen(list
->ips32_tmodel
) + 1);
262 RegCloseKey(ips32_key
);
263 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
266 error_close_clsid_key
:
267 RegCloseKey(clsid_key
);
270 error_close_coclass_key
:
271 RegCloseKey(coclass_key
);
273 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
276 /***********************************************************************
277 * unregister_coclasses
279 static HRESULT
unregister_coclasses(struct regsvr_coclass
const *list
)
281 LONG res
= ERROR_SUCCESS
;
284 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, clsid_keyname
, 0,
285 KEY_READ
| KEY_WRITE
, &coclass_key
);
286 if (res
== ERROR_FILE_NOT_FOUND
) return S_OK
;
287 if (res
!= ERROR_SUCCESS
) goto error_return
;
289 for (; res
== ERROR_SUCCESS
&& list
->clsid
; ++list
) {
293 StringFromGUID2(list
->clsid
, buf
, 39);
294 res
= RegOpenKeyExW(coclass_key
, buf
, 0,
295 KEY_READ
| KEY_WRITE
, &clsid_key
);
296 if (res
== ERROR_FILE_NOT_FOUND
) {
300 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
301 res
= recursive_delete_key(clsid_key
);
302 RegCloseKey(clsid_key
);
303 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
306 error_close_coclass_key
:
307 RegCloseKey(coclass_key
);
309 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
312 /***********************************************************************
315 static LONG
register_key_guid(HKEY base
, WCHAR
const *name
, GUID
const *guid
)
319 StringFromGUID2(guid
, buf
, 39);
320 return register_key_defvalueW(base
, name
, buf
);
323 /***********************************************************************
324 * regsvr_key_defvalueW
326 static LONG
register_key_defvalueW(
334 res
= RegCreateKeyExW(base
, name
, 0, NULL
, 0,
335 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
336 if (res
!= ERROR_SUCCESS
) return res
;
337 res
= RegSetValueExW(key
, NULL
, 0, REG_SZ
, (CONST BYTE
*)value
,
338 (lstrlenW(value
) + 1) * sizeof(WCHAR
));
343 /***********************************************************************
344 * regsvr_key_defvalueA
346 static LONG
register_key_defvalueA(
354 res
= RegCreateKeyExW(base
, name
, 0, NULL
, 0,
355 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
356 if (res
!= ERROR_SUCCESS
) return res
;
357 res
= RegSetValueExA(key
, NULL
, 0, REG_SZ
, (CONST BYTE
*)value
,
358 lstrlenA(value
) + 1);
363 /***********************************************************************
364 * recursive_delete_key
366 static LONG
recursive_delete_key(HKEY key
)
369 WCHAR subkey_name
[MAX_PATH
];
374 cName
= sizeof(subkey_name
) / sizeof(WCHAR
);
375 res
= RegEnumKeyExW(key
, 0, subkey_name
, &cName
,
376 NULL
, NULL
, NULL
, NULL
);
377 if (res
!= ERROR_SUCCESS
&& res
!= ERROR_MORE_DATA
) {
378 res
= ERROR_SUCCESS
; /* presumably we're done enumerating */
381 res
= RegOpenKeyExW(key
, subkey_name
, 0,
382 KEY_READ
| KEY_WRITE
, &subkey
);
383 if (res
== ERROR_FILE_NOT_FOUND
) continue;
384 if (res
!= ERROR_SUCCESS
) break;
386 res
= recursive_delete_key(subkey
);
388 if (res
!= ERROR_SUCCESS
) break;
391 if (res
== ERROR_SUCCESS
) res
= RegDeleteKeyW(key
, 0);
395 /***********************************************************************
398 static struct regsvr_coclass
const coclass_list
[] = {
399 { &CLSID_StdComponentCategoriesMgr
,
400 "StdComponentCategoriesMgr",
405 { NULL
} /* list terminator */
408 /***********************************************************************
411 static GUID
const CLSID_PSFactoryBuffer_actxprxy
= {
412 0xB8DA6310, 0xE19B, 0x11D0, {0x93,0x3C,0x00,0xA0,0xC9,0x0D,0xCA,0xA9} };
414 static struct regsvr_interface
const interface_list
[] = {
420 &CLSID_PSFactoryBuffer_actxprxy
422 { &IID_IEnumCATEGORYINFO
,
427 &CLSID_PSFactoryBuffer_actxprxy
434 &CLSID_PSFactoryBuffer_actxprxy
436 { &IID_ICatInformation
,
441 &CLSID_PSFactoryBuffer_actxprxy
443 { NULL
} /* list terminator */
446 /***********************************************************************
447 * DllRegisterServer (COMCAT.@)
449 HRESULT WINAPI
COMCAT_DllRegisterServer()
455 hr
= register_coclasses(coclass_list
);
457 hr
= register_interfaces(interface_list
);
461 /***********************************************************************
462 * DllUnregisterServer (COMCAT.@)
464 HRESULT WINAPI
COMCAT_DllUnregisterServer()
470 hr
= unregister_coclasses(coclass_list
);
472 hr
= unregister_interfaces(interface_list
);