2 * Gameux library self-registerable dll functions
4 * Copyright (C) 2010 Mariusz PluciĆski
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gameux);
42 * Near the bottom of this file are the exported DllRegisterServer and
43 * DllUnregisterServer, which make all this worthwhile.
46 /***********************************************************************
47 * interface for self-registering
49 struct regsvr_interface
51 IID const *iid; /* NULL for end of list */
52 LPCSTR name; /* can be NULL to omit */
53 IID const *base_iid; /* can be NULL to omit */
54 int num_methods; /* can be <0 to omit */
55 CLSID const *ps_clsid; /* can be NULL to omit */
56 CLSID const *ps_clsid32; /* can be NULL to omit */
59 static HRESULT register_interfaces(struct regsvr_interface const *list);
60 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
64 CLSID const *clsid; /* NULL for end of list */
65 LPCSTR name; /* can be NULL to omit */
66 LPCSTR ips; /* can be NULL to omit */
67 LPCSTR ips32; /* can be NULL to omit */
68 LPCSTR ips32_tmodel; /* can be NULL to omit */
69 LPCSTR progid; /* can be NULL to omit */
70 LPCSTR version; /* can be NULL to omit */
73 static HRESULT register_coclasses(struct regsvr_coclass const *list);
74 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
78 LPCSTR name; /* NULL for end of list */
79 LPCSTR description; /* can be NULL to omit */
81 LPCSTR curver; /* can be NULL to omit */
84 static HRESULT register_progids(struct progid const *list);
85 static HRESULT unregister_progids(struct progid const *list);
87 /***********************************************************************
88 * static string constants
90 static WCHAR const interface_keyname[10] = {
91 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
92 static WCHAR const base_ifa_keyname[14] = {
93 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
95 static WCHAR const num_methods_keyname[11] = {
96 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
97 static WCHAR const ps_clsid_keyname[15] = {
98 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
100 static WCHAR const ps_clsid32_keyname[17] = {
101 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
102 'i', 'd', '3', '2', 0 };
103 static WCHAR const clsid_keyname[6] = {
104 'C', 'L', 'S', 'I', 'D', 0 };
105 static WCHAR const ips_keyname[13] = {
106 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
108 static WCHAR const ips32_keyname[15] = {
109 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
111 static WCHAR const progid_keyname[7] = {
112 'P', 'r', 'o', 'g', 'I', 'D', 0 };
113 static WCHAR const versionindependentprogid_keyname[] = {
114 'V', 'e', 'r', 's', 'i', 'o', 'n',
115 'I', 'n', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 't',
116 'P', 'r', 'o', 'g', 'I', 'D', 0 };
117 static WCHAR const version_keyname[] = {
118 'V', 'e', 'r', 's', 'i', 'o', 'n', 0 };
119 static WCHAR const curver_keyname[] = {
120 'C', 'u', 'r', 'V', 'e', 'r', 0 };
121 static char const tmodel_valuename[] = "ThreadingModel";
123 /***********************************************************************
124 * static helper functions
126 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
127 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
129 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
132 /***********************************************************************
133 * register_interfaces
135 static HRESULT register_interfaces(struct regsvr_interface const *list)
137 LONG res = ERROR_SUCCESS;
140 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
141 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
142 if (res != ERROR_SUCCESS) goto error_return;
144 for (; res == ERROR_SUCCESS && list->iid; ++list)
149 StringFromGUID2(list->iid, buf, 39);
150 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
151 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
152 if (res != ERROR_SUCCESS) goto error_close_interface_key;
155 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
156 (CONST BYTE*)(list->name),
157 strlen(list->name) + 1);
158 if (res != ERROR_SUCCESS) goto error_close_iid_key;
161 if (list->base_iid) {
162 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
163 if (res != ERROR_SUCCESS) goto error_close_iid_key;
166 if (0 <= list->num_methods) {
167 static WCHAR const fmt[3] = { '%', 'd', 0 };
170 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
171 KEY_READ | KEY_WRITE, NULL, &key, NULL);
172 if (res != ERROR_SUCCESS) goto error_close_iid_key;
174 wsprintfW(buf, fmt, list->num_methods);
175 res = RegSetValueExW(key, NULL, 0, REG_SZ,
177 (lstrlenW(buf) + 1) * sizeof(WCHAR));
180 if (res != ERROR_SUCCESS) goto error_close_iid_key;
183 if (list->ps_clsid) {
184 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
185 if (res != ERROR_SUCCESS) goto error_close_iid_key;
188 if (list->ps_clsid32) {
189 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
190 if (res != ERROR_SUCCESS) goto error_close_iid_key;
194 RegCloseKey(iid_key);
197 error_close_interface_key:
198 RegCloseKey(interface_key);
200 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
203 /***********************************************************************
204 * unregister_interfaces
206 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
208 LONG res = ERROR_SUCCESS;
211 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
212 KEY_READ | KEY_WRITE, &interface_key);
213 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
214 if (res != ERROR_SUCCESS) goto error_return;
216 for (; res == ERROR_SUCCESS && list->iid; ++list) {
219 StringFromGUID2(list->iid, buf, 39);
220 res = RegDeleteTreeW(interface_key, buf);
221 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
224 RegCloseKey(interface_key);
226 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
229 /***********************************************************************
232 static HRESULT register_coclasses(struct regsvr_coclass const *list)
234 LONG res = ERROR_SUCCESS;
237 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
238 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
239 if (res != ERROR_SUCCESS) goto error_return;
241 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
245 StringFromGUID2(list->clsid, buf, 39);
246 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
247 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
248 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
251 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
252 (CONST BYTE*)(list->name),
253 strlen(list->name) + 1);
254 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
258 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
259 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
265 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
266 KEY_READ | KEY_WRITE, NULL,
268 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
270 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
271 (CONST BYTE*)list->ips32,
272 lstrlenA(list->ips32) + 1);
273 if (res == ERROR_SUCCESS && list->ips32_tmodel)
274 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
275 (CONST BYTE*)list->ips32_tmodel,
276 strlen(list->ips32_tmodel) + 1);
277 RegCloseKey(ips32_key);
278 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
286 buffer = HeapAlloc(GetProcessHeap(), 0, strlen(list->progid) + strlen(list->version) + 2);
288 res = ERROR_OUTOFMEMORY;
289 goto error_close_clsid_key;
291 strcpy(buffer, list->progid);
293 strcat(buffer, list->version);
296 progid = list->progid;
298 res = register_key_defvalueA(clsid_key, progid_keyname, progid);
299 HeapFree(GetProcessHeap(), 0, buffer);
300 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
303 res = register_key_defvalueA(clsid_key, versionindependentprogid_keyname, list->progid);
304 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
309 res = register_key_defvalueA(clsid_key, version_keyname, list->version);
310 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
313 error_close_clsid_key:
314 RegCloseKey(clsid_key);
317 error_close_coclass_key:
318 RegCloseKey(coclass_key);
320 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
323 /***********************************************************************
324 * unregister_coclasses
326 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
328 LONG res = ERROR_SUCCESS;
331 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
332 KEY_READ | KEY_WRITE, &coclass_key);
333 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
334 if (res != ERROR_SUCCESS) goto error_return;
336 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
339 StringFromGUID2(list->clsid, buf, 39);
340 res = RegDeleteTreeW(coclass_key, buf);
341 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
342 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
345 error_close_coclass_key:
346 RegCloseKey(coclass_key);
348 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
351 /***********************************************************************
354 static HRESULT register_progids(struct progid const *list)
356 LONG res = ERROR_SUCCESS;
358 for (; res == ERROR_SUCCESS && list->name; ++list) {
362 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->name, 0,
363 NULL, 0, KEY_READ | KEY_WRITE, NULL,
365 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
367 res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
368 (CONST BYTE*)list->description,
369 strlen(list->description) + 1);
370 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
372 StringFromGUID2(list->clsid, buf, 39);
374 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
375 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
378 res = register_key_defvalueA(progid_key, curver_keyname, list->curver);
379 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
382 error_close_clsid_key:
383 RegCloseKey(progid_key);
386 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
389 /***********************************************************************
392 static HRESULT unregister_progids(struct progid const *list)
394 LONG res = ERROR_SUCCESS;
396 for (; res == ERROR_SUCCESS && list->name; ++list) {
397 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->name);
398 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
401 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
404 /***********************************************************************
407 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
411 StringFromGUID2(guid, buf, 39);
412 return register_key_defvalueW(base, name, buf);
415 /***********************************************************************
416 * regsvr_key_defvalueW
418 static LONG register_key_defvalueW(
426 res = RegCreateKeyExW(base, name, 0, NULL, 0,
427 KEY_READ | KEY_WRITE, NULL, &key, NULL);
428 if (res != ERROR_SUCCESS) return res;
429 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
430 (lstrlenW(value) + 1) * sizeof(WCHAR));
435 /***********************************************************************
436 * regsvr_key_defvalueA
438 static LONG register_key_defvalueA(
446 res = RegCreateKeyExW(base, name, 0, NULL, 0,
447 KEY_READ | KEY_WRITE, NULL, &key, NULL);
448 if (res != ERROR_SUCCESS) return res;
449 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
450 lstrlenA(value) + 1);
455 /***********************************************************************
458 static struct regsvr_coclass const coclass_list[] = {
459 { &CLSID_GameExplorer,
460 "GameExplorer Class",
464 "gameux.GameExplorer.1",
468 &CLSID_GameStatistics,
469 "GameStatistics Class",
473 "gameux.GameStatistics.1",
476 { NULL } /* list terminator */
479 /***********************************************************************
482 static struct regsvr_interface const interface_list[] = {
483 { NULL } /* list terminator */
486 /***********************************************************************
489 static struct progid const progid_list[] = {
490 { "gameux.GameExplorer",
491 "GameExplorer Class",
495 { NULL } /* list terminator */
498 /***********************************************************************
499 * DllRegisterServer (GAMEUX.@)
501 HRESULT WINAPI DllRegisterServer(void)
507 hr = register_coclasses(coclass_list);
509 hr = register_interfaces(interface_list);
511 hr = register_progids(progid_list);
516 /***********************************************************************
517 * DllUnregisterServer (GAMEUX.@)
519 HRESULT WINAPI DllUnregisterServer(void)
525 hr = unregister_coclasses(coclass_list);
527 hr = unregister_interfaces(interface_list);
529 hr = unregister_progids(progid_list);