wined3d: Check more thoroughly if a stage references a texture.
[wine/hacks.git] / dlls / dinput8 / regsvr.c
blobc278ed34702d4e7d9ff6bfc7cd14959afcfdeb51
1 /*
2 * self-registerable dll functions for dinput8.dll
4 * Copyright (C) 2003 John K. Hohm
5 * Copyright (C) 2007 Francois Gouget for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
32 #include "dinput.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
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
47 struct regsvr_coclass
49 CLSID const *clsid; /* NULL for end of list */
50 LPCSTR name; /* can be NULL to omit */
51 LPCSTR ips; /* can be NULL to omit */
52 LPCSTR ips32; /* can be NULL to omit */
53 LPCSTR ips32_tmodel; /* can be NULL to omit */
54 LPCSTR clsid_str; /* can be NULL to omit */
55 LPCSTR progid; /* can be NULL to omit */
58 static HRESULT register_coclasses(struct regsvr_coclass const *list);
59 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
61 /***********************************************************************
62 * static string constants
64 static WCHAR const interface_keyname[10] = {
65 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
66 static WCHAR const base_ifa_keyname[14] = {
67 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
68 'e', 0 };
69 static WCHAR const num_methods_keyname[11] = {
70 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
71 static WCHAR const ps_clsid_keyname[15] = {
72 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
73 'i', 'd', 0 };
74 static WCHAR const ps_clsid32_keyname[17] = {
75 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
76 'i', 'd', '3', '2', 0 };
77 static WCHAR const clsid_keyname[6] = {
78 'C', 'L', 'S', 'I', 'D', 0 };
79 static WCHAR const ips_keyname[13] = {
80 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
81 0 };
82 static WCHAR const ips32_keyname[15] = {
83 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
84 '3', '2', 0 };
85 static WCHAR const progid_keyname[7] = {
86 'P', 'r', 'o', 'g', 'I', 'D', 0 };
87 static char const tmodel_valuename[] = "ThreadingModel";
89 /***********************************************************************
90 * static helper functions
92 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
93 WCHAR const *value);
94 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
95 char const *value);
96 static LONG recursive_delete_key(HKEY key);
97 static LONG recursive_delete_keyA(HKEY base, char const *name);
98 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name);
100 /***********************************************************************
101 * register_coclasses
103 static HRESULT register_coclasses(struct regsvr_coclass const *list)
105 LONG res = ERROR_SUCCESS;
106 HKEY coclass_key;
108 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
109 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
110 if (res != ERROR_SUCCESS) goto error_return;
112 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
113 WCHAR buf[39];
114 HKEY clsid_key;
116 StringFromGUID2(list->clsid, buf, 39);
117 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
118 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
119 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
121 if (list->name) {
122 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
123 (CONST BYTE*)(list->name),
124 strlen(list->name) + 1);
125 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
128 if (list->ips) {
129 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
130 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
133 if (list->ips32) {
134 HKEY ips32_key;
136 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
137 KEY_READ | KEY_WRITE, NULL,
138 &ips32_key, NULL);
139 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
141 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
142 (CONST BYTE*)list->ips32,
143 lstrlenA(list->ips32) + 1);
144 if (res == ERROR_SUCCESS && list->ips32_tmodel)
145 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
146 (CONST BYTE*)list->ips32_tmodel,
147 strlen(list->ips32_tmodel) + 1);
148 RegCloseKey(ips32_key);
149 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
152 if (list->clsid_str) {
153 res = register_key_defvalueA(clsid_key, clsid_keyname,
154 list->clsid_str);
155 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
158 if (list->progid) {
159 HKEY progid_key;
161 res = register_key_defvalueA(clsid_key, progid_keyname,
162 list->progid);
163 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
165 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
166 NULL, 0, KEY_READ | KEY_WRITE, NULL,
167 &progid_key, NULL);
168 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
170 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
171 RegCloseKey(progid_key);
172 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
175 error_close_clsid_key:
176 RegCloseKey(clsid_key);
179 error_close_coclass_key:
180 RegCloseKey(coclass_key);
181 error_return:
182 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
185 /***********************************************************************
186 * unregister_coclasses
188 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
190 LONG res = ERROR_SUCCESS;
191 HKEY coclass_key;
193 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
194 KEY_READ | KEY_WRITE, &coclass_key);
195 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
196 if (res != ERROR_SUCCESS) goto error_return;
198 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
199 WCHAR buf[39];
201 StringFromGUID2(list->clsid, buf, 39);
202 res = recursive_delete_keyW(coclass_key, buf);
203 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
205 if (list->progid) {
206 res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->progid);
207 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
211 error_close_coclass_key:
212 RegCloseKey(coclass_key);
213 error_return:
214 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
217 /***********************************************************************
218 * regsvr_key_defvalueW
220 static LONG register_key_defvalueW(
221 HKEY base,
222 WCHAR const *name,
223 WCHAR const *value)
225 LONG res;
226 HKEY key;
228 res = RegCreateKeyExW(base, name, 0, NULL, 0,
229 KEY_READ | KEY_WRITE, NULL, &key, NULL);
230 if (res != ERROR_SUCCESS) return res;
231 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
232 (lstrlenW(value) + 1) * sizeof(WCHAR));
233 RegCloseKey(key);
234 return res;
237 /***********************************************************************
238 * regsvr_key_defvalueA
240 static LONG register_key_defvalueA(
241 HKEY base,
242 WCHAR const *name,
243 char const *value)
245 LONG res;
246 HKEY key;
248 res = RegCreateKeyExW(base, name, 0, NULL, 0,
249 KEY_READ | KEY_WRITE, NULL, &key, NULL);
250 if (res != ERROR_SUCCESS) return res;
251 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
252 lstrlenA(value) + 1);
253 RegCloseKey(key);
254 return res;
257 /***********************************************************************
258 * recursive_delete_key
260 static LONG recursive_delete_key(HKEY key)
262 LONG res;
263 WCHAR subkey_name[MAX_PATH];
264 DWORD cName;
265 HKEY subkey;
267 for (;;) {
268 cName = sizeof(subkey_name) / sizeof(WCHAR);
269 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
270 NULL, NULL, NULL, NULL);
271 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
272 res = ERROR_SUCCESS; /* presumably we're done enumerating */
273 break;
275 res = RegOpenKeyExW(key, subkey_name, 0,
276 KEY_READ | KEY_WRITE, &subkey);
277 if (res == ERROR_FILE_NOT_FOUND) continue;
278 if (res != ERROR_SUCCESS) break;
280 res = recursive_delete_key(subkey);
281 RegCloseKey(subkey);
282 if (res != ERROR_SUCCESS) break;
285 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
286 return res;
289 /***********************************************************************
290 * recursive_delete_keyA
292 static LONG recursive_delete_keyA(HKEY base, char const *name)
294 LONG res;
295 HKEY key;
297 res = RegOpenKeyExA(base, name, 0, KEY_READ | KEY_WRITE, &key);
298 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
299 if (res != ERROR_SUCCESS) return res;
300 res = recursive_delete_key(key);
301 RegCloseKey(key);
302 return res;
305 /***********************************************************************
306 * recursive_delete_keyW
308 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name)
310 LONG res;
311 HKEY key;
313 res = RegOpenKeyExW(base, name, 0, KEY_READ | KEY_WRITE, &key);
314 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
315 if (res != ERROR_SUCCESS) return res;
316 res = recursive_delete_key(key);
317 RegCloseKey(key);
318 return res;
321 /***********************************************************************
322 * coclass list
325 static struct regsvr_coclass const coclass_list[] = {
326 { &CLSID_DirectInput8,
327 "DirectInput8 Object",
328 NULL,
329 "dinput8.dll",
330 "Both"
332 { NULL } /* list terminator */
335 /***********************************************************************
336 * DllRegisterServer (DINPUT8.@)
338 HRESULT WINAPI DllRegisterServer(void)
340 HRESULT hr;
342 TRACE("\n");
344 hr = register_coclasses(coclass_list);
345 return hr;
348 /***********************************************************************
349 * DllUnregisterServer (DINPUT8.@)
351 HRESULT WINAPI DllUnregisterServer(void)
353 HRESULT hr;
355 TRACE("\n");
357 hr = unregister_coclasses(coclass_list);
358 return hr;