shdocvw: Fix translations.
[wine/multimedia.git] / dlls / ddraw / regsvr.c
blob52a6fb77bda0f96e20719c4106e5fdf08ab5848d
1 /*
2 * self-registerable dll functions for ddraw.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include "ddraw_private.h"
25 #include "wine/unicode.h"
26 #include "winreg.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 static LSTATUS (WINAPI *pRegDeleteTreeW)(HKEY,LPCWSTR);
31 static LSTATUS (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
34 * Near the bottom of this file are the exported DllRegisterServer and
35 * DllUnregisterServer, which make all this worthwhile.
38 /***********************************************************************
39 * interface for self-registering
41 struct regsvr_interface
43 IID const *iid; /* NULL for end of list */
44 LPCSTR name; /* can be NULL to omit */
45 IID const *base_iid; /* can be NULL to omit */
46 int num_methods; /* can be <0 to omit */
47 CLSID const *ps_clsid; /* can be NULL to omit */
48 CLSID const *ps_clsid32; /* can be NULL to omit */
51 static HRESULT register_interfaces(struct regsvr_interface const *list);
52 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
54 struct regsvr_coclass
56 CLSID const *clsid; /* NULL for end of list */
57 LPCSTR name; /* can be NULL to omit */
58 LPCSTR ips; /* can be NULL to omit */
59 LPCSTR ips32; /* can be NULL to omit */
60 LPCSTR ips32_tmodel; /* can be NULL to omit */
61 LPCSTR clsid_str; /* can be NULL to omit */
62 LPCSTR progid; /* can be NULL to omit */
65 static HRESULT register_coclasses(struct regsvr_coclass const *list);
66 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
68 /***********************************************************************
69 * static string constants
71 static WCHAR const interface_keyname[10] = {
72 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
73 static WCHAR const base_ifa_keyname[14] = {
74 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
75 'e', 0 };
76 static WCHAR const num_methods_keyname[11] = {
77 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
78 static WCHAR const ps_clsid_keyname[15] = {
79 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
80 'i', 'd', 0 };
81 static WCHAR const ps_clsid32_keyname[17] = {
82 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
83 'i', 'd', '3', '2', 0 };
84 static WCHAR const clsid_keyname[6] = {
85 'C', 'L', 'S', 'I', 'D', 0 };
86 static WCHAR const ips_keyname[13] = {
87 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
88 0 };
89 static WCHAR const ips32_keyname[15] = {
90 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
91 '3', '2', 0 };
92 static WCHAR const progid_keyname[7] = {
93 'P', 'r', 'o', 'g', 'I', 'D', 0 };
94 static char const tmodel_valuename[] = "ThreadingModel";
96 /***********************************************************************
97 * static helper functions
99 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
100 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
101 WCHAR const *value);
102 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
103 char const *value);
105 /***********************************************************************
106 * register_interfaces
108 static HRESULT register_interfaces(struct regsvr_interface const *list)
110 LONG res = ERROR_SUCCESS;
111 HKEY interface_key;
113 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
114 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
115 if (res != ERROR_SUCCESS) goto error_return;
117 for (; res == ERROR_SUCCESS && list->iid; ++list) {
118 WCHAR buf[39];
119 HKEY iid_key;
121 StringFromGUID2(list->iid, buf, 39);
122 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
123 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
124 if (res != ERROR_SUCCESS) goto error_close_interface_key;
126 if (list->name) {
127 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
128 (CONST BYTE*)(list->name),
129 strlen(list->name) + 1);
130 if (res != ERROR_SUCCESS) goto error_close_iid_key;
133 if (list->base_iid) {
134 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
135 if (res != ERROR_SUCCESS) goto error_close_iid_key;
138 if (0 <= list->num_methods) {
139 static WCHAR const fmt[3] = { '%', 'd', 0 };
140 HKEY key;
142 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
143 KEY_READ | KEY_WRITE, NULL, &key, NULL);
144 if (res != ERROR_SUCCESS) goto error_close_iid_key;
146 sprintfW(buf, fmt, list->num_methods);
147 res = RegSetValueExW(key, NULL, 0, REG_SZ,
148 (CONST BYTE*)buf,
149 (lstrlenW(buf) + 1) * sizeof(WCHAR));
150 RegCloseKey(key);
152 if (res != ERROR_SUCCESS) goto error_close_iid_key;
155 if (list->ps_clsid) {
156 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
157 if (res != ERROR_SUCCESS) goto error_close_iid_key;
160 if (list->ps_clsid32) {
161 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
162 if (res != ERROR_SUCCESS) goto error_close_iid_key;
165 error_close_iid_key:
166 RegCloseKey(iid_key);
169 error_close_interface_key:
170 RegCloseKey(interface_key);
171 error_return:
172 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
175 /***********************************************************************
176 * unregister_interfaces
178 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
180 LONG res = ERROR_SUCCESS;
181 HKEY interface_key;
183 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
184 KEY_READ | KEY_WRITE, &interface_key);
185 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
186 if (res != ERROR_SUCCESS) goto error_return;
188 for (; res == ERROR_SUCCESS && list->iid; ++list) {
189 WCHAR buf[39];
191 StringFromGUID2(list->iid, buf, 39);
192 res = pRegDeleteTreeW(interface_key, buf);
193 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
196 RegCloseKey(interface_key);
197 error_return:
198 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
201 /***********************************************************************
202 * register_coclasses
204 static HRESULT register_coclasses(struct regsvr_coclass const *list)
206 LONG res = ERROR_SUCCESS;
207 HKEY coclass_key;
209 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
210 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
211 if (res != ERROR_SUCCESS) goto error_return;
213 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
214 WCHAR buf[39];
215 HKEY clsid_key;
217 StringFromGUID2(list->clsid, buf, 39);
218 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
219 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
220 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
222 if (list->name) {
223 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
224 (CONST BYTE*)(list->name),
225 strlen(list->name) + 1);
226 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
229 if (list->ips) {
230 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
231 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
234 if (list->ips32) {
235 HKEY ips32_key;
237 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
238 KEY_READ | KEY_WRITE, NULL,
239 &ips32_key, NULL);
240 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
242 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
243 (CONST BYTE*)list->ips32,
244 lstrlenA(list->ips32) + 1);
245 if (res == ERROR_SUCCESS && list->ips32_tmodel)
246 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
247 (CONST BYTE*)list->ips32_tmodel,
248 strlen(list->ips32_tmodel) + 1);
249 RegCloseKey(ips32_key);
250 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
253 if (list->clsid_str) {
254 res = register_key_defvalueA(clsid_key, clsid_keyname,
255 list->clsid_str);
256 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
259 if (list->progid) {
260 HKEY progid_key;
262 res = register_key_defvalueA(clsid_key, progid_keyname,
263 list->progid);
264 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
266 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
267 NULL, 0, KEY_READ | KEY_WRITE, NULL,
268 &progid_key, NULL);
269 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
271 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
272 RegCloseKey(progid_key);
273 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
276 error_close_clsid_key:
277 RegCloseKey(clsid_key);
280 error_close_coclass_key:
281 RegCloseKey(coclass_key);
282 error_return:
283 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
286 /***********************************************************************
287 * unregister_coclasses
289 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
291 LONG res = ERROR_SUCCESS;
292 HKEY coclass_key;
294 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
295 KEY_READ | KEY_WRITE, &coclass_key);
296 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
297 if (res != ERROR_SUCCESS) goto error_return;
299 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
300 WCHAR buf[39];
302 StringFromGUID2(list->clsid, buf, 39);
303 res = pRegDeleteTreeW(coclass_key, buf);
304 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
305 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
307 if (list->progid) {
308 res = pRegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
309 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
310 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
314 error_close_coclass_key:
315 RegCloseKey(coclass_key);
316 error_return:
317 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
320 /***********************************************************************
321 * regsvr_key_guid
323 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
325 WCHAR buf[39];
327 StringFromGUID2(guid, buf, 39);
328 return register_key_defvalueW(base, name, buf);
331 /***********************************************************************
332 * regsvr_key_defvalueW
334 static LONG register_key_defvalueW(
335 HKEY base,
336 WCHAR const *name,
337 WCHAR const *value)
339 LONG res;
340 HKEY key;
342 res = RegCreateKeyExW(base, name, 0, NULL, 0,
343 KEY_READ | KEY_WRITE, NULL, &key, NULL);
344 if (res != ERROR_SUCCESS) return res;
345 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
346 (lstrlenW(value) + 1) * sizeof(WCHAR));
347 RegCloseKey(key);
348 return res;
351 /***********************************************************************
352 * regsvr_key_defvalueA
354 static LONG register_key_defvalueA(
355 HKEY base,
356 WCHAR const *name,
357 char const *value)
359 LONG res;
360 HKEY key;
362 res = RegCreateKeyExW(base, name, 0, NULL, 0,
363 KEY_READ | KEY_WRITE, NULL, &key, NULL);
364 if (res != ERROR_SUCCESS) return res;
365 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
366 lstrlenA(value) + 1);
367 RegCloseKey(key);
368 return res;
371 /***********************************************************************
372 * coclass list
375 static struct regsvr_coclass const coclass_list[] = {
376 { &CLSID_DirectDraw,
377 "DirectDraw Object",
378 NULL,
379 "ddraw.dll",
380 "Both"
382 { &CLSID_DirectDraw7,
383 "DirectDraw 7 Object",
384 NULL,
385 "ddraw.dll",
386 "Both"
388 { &CLSID_DirectDrawClipper,
389 "DirectDraw Clipper Object",
390 NULL,
391 "ddraw.dll",
392 "Both"
394 { NULL } /* list terminator */
397 /***********************************************************************
398 * interface list
401 static struct regsvr_interface const interface_list[] = {
402 { NULL } /* list terminator */
405 /***********************************************************************
406 * DllRegisterServer (DDRAW.@)
408 HRESULT WINAPI DllRegisterServer(void)
410 HRESULT hr;
412 TRACE("\n");
414 hr = register_coclasses(coclass_list);
415 if (SUCCEEDED(hr))
416 hr = register_interfaces(interface_list);
417 return hr;
420 /***********************************************************************
421 * DllUnregisterServer (DDRAW.@)
423 HRESULT WINAPI DllUnregisterServer(void)
425 HRESULT hr;
427 HMODULE advapi32 = GetModuleHandleA("advapi32");
428 if (!advapi32) return E_FAIL;
429 pRegDeleteTreeA = (void *) GetProcAddress(advapi32, "RegDeleteTreeA");
430 pRegDeleteTreeW = (void *) GetProcAddress(advapi32, "RegDeleteTreeW");
431 if (!pRegDeleteTreeA || !pRegDeleteTreeW) return E_FAIL;
433 TRACE("\n");
435 hr = unregister_coclasses(coclass_list);
436 if (SUCCEEDED(hr))
437 hr = unregister_interfaces(interface_list);
438 return hr;