configure: Remove some unused but set variables (LLVM/Clang).
[wine.git] / dlls / atl / registrar.c
blobdad2eafa397f469c15605bc7aebb07aa701aff30
1 /*
2 * Copyright 2005 Jacek Caban
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "oaidl.h"
31 #define ATL_INITGUID
32 #include "atliface.h"
33 #include "atlbase.h"
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(atl);
40 static LONG dll_count;
42 /**************************************************************
43 * ATLRegistrar implementation
46 static const struct {
47 WCHAR name[22];
48 HKEY key;
49 } root_keys[] = {
50 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
51 HKEY_CLASSES_ROOT},
52 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
53 HKEY_CURRENT_USER},
54 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
55 HKEY_LOCAL_MACHINE},
56 {{'H','K','E','Y','_','U','S','E','R','S',0},
57 HKEY_USERS},
58 {{'H','K','E','Y','_','P','E','R','F','O','R','M','A','N','C','E','_','D','A','T','A',0},
59 HKEY_PERFORMANCE_DATA},
60 {{'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0},
61 HKEY_DYN_DATA},
62 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
63 HKEY_CURRENT_CONFIG},
64 {{'H','K','C','R',0}, HKEY_CLASSES_ROOT},
65 {{'H','K','C','U',0}, HKEY_CURRENT_USER},
66 {{'H','K','L','M',0}, HKEY_LOCAL_MACHINE},
67 {{'H','K','U',0}, HKEY_USERS},
68 {{'H','K','P','D',0}, HKEY_PERFORMANCE_DATA},
69 {{'H','K','D','D',0}, HKEY_DYN_DATA},
70 {{'H','K','C','C',0}, HKEY_CURRENT_CONFIG}
73 typedef struct rep_list_str {
74 LPOLESTR key;
75 LPOLESTR item;
76 int key_len;
77 struct rep_list_str *next;
78 } rep_list;
80 typedef struct {
81 IRegistrar IRegistrar_iface;
82 LONG ref;
83 rep_list *rep;
84 } Registrar;
86 typedef struct {
87 LPOLESTR str;
88 DWORD alloc;
89 DWORD len;
90 } strbuf;
92 static inline Registrar *impl_from_IRegistrar(IRegistrar *iface)
94 return CONTAINING_RECORD(iface, Registrar, IRegistrar_iface);
97 static void strbuf_init(strbuf *buf)
99 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
100 buf->alloc = 128;
101 buf->len = 0;
104 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
106 if(len == -1)
107 len = lstrlenW(str);
108 if(buf->len+len+1 >= buf->alloc) {
109 buf->alloc = (buf->len+len)<<1;
110 buf->str = HeapReAlloc(GetProcessHeap(), 0, buf->str, buf->alloc*sizeof(WCHAR));
112 memcpy(buf->str+buf->len, str, len*sizeof(OLECHAR));
113 buf->len += len;
114 buf->str[buf->len] = '\0';
117 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
119 LPCOLESTR iter, iter2 = *str;
121 buf->len = 0;
122 buf->str[0] = '\0';
124 while(isspaceW(*iter2))
125 iter2++;
126 iter = iter2;
127 if(!*iter) {
128 *str = iter;
129 return S_OK;
132 if(*iter == '}' || *iter == '=') {
133 strbuf_write(iter++, buf, 1);
134 }else if(*iter == '\'') {
135 iter2 = ++iter;
136 iter = strchrW(iter, '\'');
137 if(!iter) {
138 WARN("Unexpected end of script\n");
139 *str = iter;
140 return DISP_E_EXCEPTION;
142 strbuf_write(iter2, buf, iter-iter2);
143 iter++;
144 }else {
145 while(*iter && !isspaceW(*iter))
146 iter++;
147 strbuf_write(iter2, buf, iter-iter2);
150 while(isspaceW(*iter))
151 iter++;
152 *str = iter;
153 return S_OK;
156 static HRESULT do_preprocess(const Registrar *This, LPCOLESTR data, strbuf *buf)
158 LPCOLESTR iter, iter2 = data;
159 rep_list *rep_iter;
160 static const WCHAR wstr[] = {'%',0};
162 iter = strchrW(data, '%');
163 while(iter) {
164 strbuf_write(iter2, buf, iter-iter2);
166 iter2 = ++iter;
167 if(!*iter2)
168 return DISP_E_EXCEPTION;
169 iter = strchrW(iter2, '%');
170 if(!iter)
171 return DISP_E_EXCEPTION;
173 if(iter == iter2) {
174 strbuf_write(wstr, buf, 1);
175 }else {
176 for(rep_iter = This->rep; rep_iter; rep_iter = rep_iter->next) {
177 if(rep_iter->key_len == iter-iter2
178 && !memicmpW(iter2, rep_iter->key, rep_iter->key_len))
179 break;
181 if(!rep_iter) {
182 WARN("Could not find replacement: %s\n", debugstr_wn(iter2, iter-iter2));
183 return DISP_E_EXCEPTION;
186 strbuf_write(rep_iter->item, buf, -1);
189 iter2 = ++iter;
190 iter = strchrW(iter, '%');
193 strbuf_write(iter2, buf, -1);
194 TRACE("%s\n", debugstr_w(buf->str));
196 return S_OK;
199 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
201 LPCOLESTR iter = *pstr;
202 HRESULT hres;
203 LONG lres;
204 HKEY hkey = 0;
205 strbuf name;
207 enum {
208 NORMAL,
209 NO_REMOVE,
210 IS_VAL,
211 FORCE_REMOVE,
212 DO_DELETE
213 } key_type = NORMAL;
215 static const WCHAR wstrNoRemove[] = {'N','o','R','e','m','o','v','e',0};
216 static const WCHAR wstrForceRemove[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
217 static const WCHAR wstrDelete[] = {'D','e','l','e','t','e',0};
218 static const WCHAR wstrval[] = {'v','a','l',0};
220 iter = *pstr;
221 hres = get_word(&iter, buf);
222 if(FAILED(hres))
223 return hres;
224 strbuf_init(&name);
226 while(buf->str[1] || buf->str[0] != '}') {
227 key_type = NORMAL;
228 if(!lstrcmpiW(buf->str, wstrNoRemove))
229 key_type = NO_REMOVE;
230 else if(!lstrcmpiW(buf->str, wstrForceRemove))
231 key_type = FORCE_REMOVE;
232 else if(!lstrcmpiW(buf->str, wstrval))
233 key_type = IS_VAL;
234 else if(!lstrcmpiW(buf->str, wstrDelete))
235 key_type = DO_DELETE;
237 if(key_type != NORMAL) {
238 hres = get_word(&iter, buf);
239 if(FAILED(hres))
240 break;
242 TRACE("name = %s\n", debugstr_w(buf->str));
244 if(do_register) {
245 if(key_type == IS_VAL) {
246 hkey = parent_key;
247 strbuf_write(buf->str, &name, -1);
248 }else if(key_type == DO_DELETE) {
249 TRACE("Deleting %s\n", debugstr_w(buf->str));
250 RegDeleteTreeW(parent_key, buf->str);
251 }else {
252 if(key_type == FORCE_REMOVE)
253 RegDeleteTreeW(parent_key, buf->str);
254 lres = RegCreateKeyW(parent_key, buf->str, &hkey);
255 if(lres != ERROR_SUCCESS) {
256 WARN("Could not create(open) key: %08x\n", lres);
257 hres = HRESULT_FROM_WIN32(lres);
258 break;
261 }else if(key_type != IS_VAL && key_type != DO_DELETE) {
262 strbuf_write(buf->str, &name, -1);
263 lres = RegOpenKeyW(parent_key, buf->str, &hkey);
264 if(lres != ERROR_SUCCESS)
265 WARN("Could not open key %s: %08x\n", debugstr_w(name.str), lres);
268 if(key_type != DO_DELETE && *iter == '=') {
269 iter++;
270 hres = get_word(&iter, buf);
271 if(FAILED(hres))
272 break;
273 if(buf->len != 1) {
274 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
275 hres = DISP_E_EXCEPTION;
276 break;
278 if(do_register) {
279 switch(buf->str[0]) {
280 case 's':
281 hres = get_word(&iter, buf);
282 if(FAILED(hres))
283 break;
284 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_SZ, (PBYTE)buf->str,
285 (lstrlenW(buf->str)+1)*sizeof(WCHAR));
286 if(lres != ERROR_SUCCESS) {
287 WARN("Could set value of key: %08x\n", lres);
288 hres = HRESULT_FROM_WIN32(lres);
289 break;
291 break;
292 case 'd': {
293 DWORD dw;
294 hres = get_word(&iter, buf);
295 if(FAILED(hres))
296 break;
297 dw = atoiW(buf->str);
298 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_DWORD,
299 (PBYTE)&dw, sizeof(dw));
300 if(lres != ERROR_SUCCESS) {
301 WARN("Could set value of key: %08x\n", lres);
302 hres = HRESULT_FROM_WIN32(lres);
303 break;
305 break;
307 case 'b': {
308 BYTE *bytes;
309 DWORD count;
310 DWORD i;
311 hres = get_word(&iter, buf);
312 if(FAILED(hres))
313 break;
314 count = (lstrlenW(buf->str) + 1) / 2;
315 bytes = HeapAlloc(GetProcessHeap(), 0, count);
316 if(bytes == NULL) {
317 hres = E_OUTOFMEMORY;
318 break;
320 for(i = 0; i < count && buf->str[2*i]; i++) {
321 WCHAR digits[3];
322 if(!isxdigitW(buf->str[2*i]) || !isxdigitW(buf->str[2*i + 1])) {
323 hres = E_FAIL;
324 break;
326 digits[0] = buf->str[2*i];
327 digits[1] = buf->str[2*i + 1];
328 digits[2] = 0;
329 bytes[i] = (BYTE) strtoulW(digits, NULL, 16);
331 if(SUCCEEDED(hres)) {
332 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_BINARY,
333 bytes, count);
334 if(lres != ERROR_SUCCESS) {
335 WARN("Could not set value of key: 0x%08x\n", lres);
336 hres = HRESULT_FROM_WIN32(lres);
339 HeapFree(GetProcessHeap(), 0, bytes);
340 break;
342 default:
343 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
344 hres = DISP_E_EXCEPTION;
346 if(FAILED(hres))
347 break;
348 }else {
349 if(*iter == '-')
350 iter++;
351 hres = get_word(&iter, buf);
352 if(FAILED(hres))
353 break;
355 }else if(key_type == IS_VAL) {
356 WARN("value not set!\n");
357 hres = DISP_E_EXCEPTION;
358 break;
361 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
362 hres = get_word(&iter, buf);
363 if(FAILED(hres))
364 break;
365 hres = do_process_key(&iter, hkey, buf, do_register);
366 if(FAILED(hres))
367 break;
370 TRACE("%x %x\n", do_register, key_type);
371 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
372 TRACE("Deleting %s\n", debugstr_w(name.str));
373 RegDeleteKeyW(parent_key, name.str);
376 if(hkey && key_type != IS_VAL)
377 RegCloseKey(hkey);
378 hkey = 0;
379 name.len = 0;
381 hres = get_word(&iter, buf);
382 if(FAILED(hres))
383 break;
386 HeapFree(GetProcessHeap(), 0, name.str);
387 if(hkey && key_type != IS_VAL)
388 RegCloseKey(hkey);
389 *pstr = iter;
390 return hres;
393 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
395 LPCOLESTR iter = data;
396 strbuf buf;
397 HRESULT hres = S_OK;
398 unsigned int i;
400 strbuf_init(&buf);
401 hres = get_word(&iter, &buf);
402 if(FAILED(hres))
403 return hres;
405 while(*iter) {
406 if(!buf.len) {
407 WARN("ward.len == 0, failed\n");
408 hres = DISP_E_EXCEPTION;
409 break;
411 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
412 if(!lstrcmpiW(buf.str, root_keys[i].name))
413 break;
415 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
416 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
417 hres = DISP_E_EXCEPTION;
418 break;
420 hres = get_word(&iter, &buf);
421 if(FAILED(hres))
422 break;
423 if(buf.str[1] || buf.str[0] != '{') {
424 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
425 hres = DISP_E_EXCEPTION;
426 break;
428 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
429 if(FAILED(hres)) {
430 WARN("Processing key failed: %08x\n", hres);
431 break;
433 hres = get_word(&iter, &buf);
434 if(FAILED(hres))
435 break;
437 HeapFree(GetProcessHeap(), 0, buf.str);
438 return hres;
441 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
443 strbuf buf;
444 HRESULT hres;
446 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
448 strbuf_init(&buf);
449 hres = do_preprocess(This, data, &buf);
450 if(FAILED(hres)) {
451 WARN("preprocessing failed!\n");
452 HeapFree(GetProcessHeap(), 0, buf.str);
453 return hres;
456 hres = do_process_root_key(buf.str, do_register);
457 if(FAILED(hres) && do_register)
458 do_process_root_key(buf.str, FALSE);
460 HeapFree(GetProcessHeap(), 0, buf.str);
461 return hres;
464 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
465 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
467 HINSTANCE hins;
468 HRSRC src;
469 LPSTR regstra;
470 LPWSTR regstrw;
471 DWORD len, reslen;
472 HRESULT hres;
474 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
475 if(hins) {
476 src = FindResourceW(hins, szID, szType);
477 if(src) {
478 regstra = LoadResource(hins, src);
479 reslen = SizeofResource(hins, src);
480 if(regstra) {
481 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
482 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
483 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, len);
484 regstrw[len-1] = '\0';
486 hres = string_register(This, regstrw, do_register);
488 HeapFree(GetProcessHeap(), 0, regstrw);
489 }else {
490 WARN("could not load resource\n");
491 hres = HRESULT_FROM_WIN32(GetLastError());
493 }else {
494 WARN("Could not find source\n");
495 hres = HRESULT_FROM_WIN32(GetLastError());
497 FreeLibrary(hins);
498 }else {
499 WARN("Could not load resource file\n");
500 hres = HRESULT_FROM_WIN32(GetLastError());
503 return hres;
506 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
508 HANDLE file;
509 DWORD filelen, len;
510 LPWSTR regstrw;
511 LPSTR regstra;
512 LRESULT lres;
513 HRESULT hres;
515 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
516 if(file != INVALID_HANDLE_VALUE) {
517 filelen = GetFileSize(file, NULL);
518 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
519 lres = ReadFile(file, regstra, filelen, NULL, NULL);
520 if(lres == ERROR_SUCCESS) {
521 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
522 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
523 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, len);
524 regstrw[len-1] = '\0';
526 hres = string_register(This, regstrw, do_register);
528 HeapFree(GetProcessHeap(), 0, regstrw);
529 }else {
530 WARN("Failed to read faile\n");
531 hres = HRESULT_FROM_WIN32(lres);
533 HeapFree(GetProcessHeap(), 0, regstra);
534 CloseHandle(file);
535 }else {
536 WARN("Could not open file\n");
537 hres = HRESULT_FROM_WIN32(GetLastError());
540 return hres;
543 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
545 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
547 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
548 IRegistrar_AddRef(iface);
549 *ppvObject = iface;
550 return S_OK;
552 return E_NOINTERFACE;
555 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
557 Registrar *This = impl_from_IRegistrar(iface);
558 ULONG ref = InterlockedIncrement(&This->ref);
559 TRACE("(%p) ->%d\n", This, ref);
560 return ref;
563 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
565 Registrar *This = impl_from_IRegistrar(iface);
566 ULONG ref = InterlockedDecrement(&This->ref);
568 TRACE("(%p) ->%d\n", This, ref);
569 if(!ref) {
570 IRegistrar_ClearReplacements(iface);
571 HeapFree(GetProcessHeap(), 0, This);
572 InterlockedDecrement(&dll_count);
574 return ref;
577 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
579 Registrar *This = impl_from_IRegistrar(iface);
580 int len;
581 rep_list *new_rep;
583 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
585 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
587 new_rep->key_len = lstrlenW(Key);
588 new_rep->key = HeapAlloc(GetProcessHeap(), 0, (new_rep->key_len + 1) * sizeof(OLECHAR));
589 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
591 len = lstrlenW(item)+1;
592 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
593 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
595 new_rep->next = This->rep;
596 This->rep = new_rep;
598 return S_OK;
601 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
603 Registrar *This = impl_from_IRegistrar(iface);
604 rep_list *iter, *iter2;
606 TRACE("(%p)\n", This);
608 if(!This->rep)
609 return S_OK;
611 iter = This->rep;
612 while(iter) {
613 iter2 = iter->next;
614 HeapFree(GetProcessHeap(), 0, iter->key);
615 HeapFree(GetProcessHeap(), 0, iter->item);
616 HeapFree(GetProcessHeap(), 0, iter);
617 iter = iter2;
620 This->rep = NULL;
621 return S_OK;
624 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
625 LPCOLESTR szID, LPCOLESTR szType)
627 Registrar *This = impl_from_IRegistrar(iface);
628 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
629 return resource_register(This, resFileName, szID, szType, TRUE);
632 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
633 LPCOLESTR szID, LPCOLESTR szType)
635 Registrar *This = impl_from_IRegistrar(iface);
636 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
637 return resource_register(This, resFileName, szID, szType, FALSE);
640 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
642 Registrar *This = impl_from_IRegistrar(iface);
643 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
644 return file_register(This, fileName, TRUE);
647 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
649 Registrar *This = impl_from_IRegistrar(iface);
650 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
651 return file_register(This, fileName, FALSE);
654 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
656 Registrar *This = impl_from_IRegistrar(iface);
657 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
658 return string_register(This, data, TRUE);
661 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
663 Registrar *This = impl_from_IRegistrar(iface);
664 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
665 return string_register(This, data, FALSE);
668 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
669 UINT nID, LPCOLESTR szType)
671 Registrar *This = impl_from_IRegistrar(iface);
672 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
673 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, TRUE);
676 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
677 UINT nID, LPCOLESTR szType)
679 Registrar *This = impl_from_IRegistrar(iface);
680 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
681 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, FALSE);
684 static const IRegistrarVtbl RegistrarVtbl = {
685 Registrar_QueryInterface,
686 Registrar_AddRef,
687 Registrar_Release,
688 Registrar_AddReplacement,
689 Registrar_ClearReplacements,
690 Registrar_ResourceRegisterSz,
691 Registrar_ResourceUnregisterSz,
692 Registrar_FileRegister,
693 Registrar_FileUnregister,
694 Registrar_StringRegister,
695 Registrar_StringUnregister,
696 Registrar_ResourceRegister,
697 Registrar_ResourceUnregister,
700 static HRESULT Registrar_create(const IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
702 Registrar *ret;
704 if(!IsEqualGUID(&IID_IUnknown, riid) && !IsEqualGUID(&IID_IRegistrar, riid))
705 return E_NOINTERFACE;
707 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar));
708 ret->IRegistrar_iface.lpVtbl = &RegistrarVtbl;
709 ret->ref = 1;
710 ret->rep = NULL;
711 *ppvObject = ret;
713 InterlockedIncrement(&dll_count);
715 return S_OK;
718 /**************************************************************
719 * ClassFactory implementation
722 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
724 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
726 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
727 *ppvObject = iface;
728 IClassFactory_AddRef( iface );
729 return S_OK;
732 return E_NOINTERFACE;
735 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
737 InterlockedIncrement(&dll_count);
738 return 2;
741 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
743 InterlockedDecrement(&dll_count);
744 return 1;
747 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
748 REFIID riid, void **ppvObject)
750 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
751 return Registrar_create(pUnkOuter, riid, ppvObject);
754 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
756 TRACE("(%p)->(%x)\n", iface, lock);
758 if(lock)
759 InterlockedIncrement(&dll_count);
760 else
761 InterlockedDecrement(&dll_count);
763 return S_OK;
766 static const IClassFactoryVtbl IRegistrarCFVtbl = {
767 RegistrarCF_QueryInterface,
768 RegistrarCF_AddRef,
769 RegistrarCF_Release,
770 RegistrarCF_CreateInstance,
771 RegistrarCF_LockServer
774 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
776 /**************************************************************
777 * DllGetClassObject (ATL.2)
779 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
781 TRACE("(%s %s %p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
783 if(IsEqualGUID(&CLSID_Registrar, clsid))
784 return IClassFactory_QueryInterface( &RegistrarCF, riid, ppvObject );
786 FIXME("Not supported class %s\n", debugstr_guid(clsid));
787 return CLASS_E_CLASSNOTAVAILABLE;
790 extern HINSTANCE hInst;
792 /* this is a copy of the winecrt0 registration code that creates the registrar directly, */
793 /* since we can't do it through ole32 until it has been registered */
795 struct reg_info
797 IRegistrar *registrar;
798 BOOL do_register;
799 HRESULT result;
802 static IRegistrar *create_registrar( HMODULE inst, struct reg_info *info )
804 info->result = Registrar_create( NULL, &IID_IRegistrar, (void**)&info->registrar );
805 if (SUCCEEDED( info->result ))
807 static const WCHAR moduleW[] = {'M','O','D','U','L','E',0};
808 WCHAR str[MAX_PATH];
810 GetModuleFileNameW( hInst, str, MAX_PATH );
811 IRegistrar_AddReplacement( info->registrar, moduleW, str );
813 return info->registrar;
816 static BOOL CALLBACK register_resource( HMODULE module, LPCWSTR type, LPWSTR name, LONG_PTR arg )
818 struct reg_info *info = (struct reg_info *)arg;
819 WCHAR *buffer;
820 HRSRC rsrc = FindResourceW( module, name, type );
821 char *str = LoadResource( module, rsrc );
822 DWORD lenW, lenA = SizeofResource( module, rsrc );
824 if (!str) return FALSE;
825 if (!info->registrar && !create_registrar( module, info )) return FALSE;
826 lenW = MultiByteToWideChar( CP_UTF8, 0, str, lenA, NULL, 0 ) + 1;
827 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
829 info->result = E_OUTOFMEMORY;
830 return FALSE;
832 MultiByteToWideChar( CP_UTF8, 0, str, lenA, buffer, lenW );
833 buffer[lenW - 1] = 0;
835 if (info->do_register)
836 info->result = IRegistrar_StringRegister( info->registrar, buffer );
837 else
838 info->result = IRegistrar_StringUnregister( info->registrar, buffer );
840 HeapFree( GetProcessHeap(), 0, buffer );
841 return SUCCEEDED(info->result);
844 static HRESULT do_register_dll_server(IRegistrar *pRegistrar, LPCOLESTR wszDll,
845 LPCOLESTR wszId, BOOL do_register,
846 const struct _ATL_REGMAP_ENTRY* pMapEntries)
848 IRegistrar *registrar;
849 HRESULT hres;
850 const struct _ATL_REGMAP_ENTRY *pMapEntry;
852 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
853 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
855 if (pRegistrar)
856 registrar = pRegistrar;
857 else
858 Registrar_create(NULL, &IID_IRegistrar, (void**)&registrar);
860 IRegistrar_AddReplacement(registrar, wszModule, wszDll);
862 for (pMapEntry = pMapEntries; pMapEntry && pMapEntry->szKey; pMapEntry++)
863 IRegistrar_AddReplacement(registrar, pMapEntry->szKey, pMapEntry->szData);
865 if(do_register)
866 hres = IRegistrar_ResourceRegisterSz(registrar, wszDll, wszId, wszRegistry);
867 else
868 hres = IRegistrar_ResourceUnregisterSz(registrar, wszDll, wszId, wszRegistry);
870 if(registrar != pRegistrar)
871 IRegistrar_Release(registrar);
872 return hres;
875 /***********************************************************************
876 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
879 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
880 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
882 HINSTANCE lhInst = pM->m_hInst;
883 /* everything inside this function below this point
884 * should go into atl71.AtlUpdateRegistryFromResourceD
886 WCHAR module_name[MAX_PATH];
888 if(!GetModuleFileNameW(lhInst, module_name, MAX_PATH)) {
889 FIXME("hinst %p: did not get module name\n",
890 lhInst);
891 return E_FAIL;
894 TRACE("%p (%s), %s, %d, %p, %p\n", hInst, debugstr_w(module_name),
895 debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
897 return do_register_dll_server(pReg, module_name, lpszRes, bRegister, pMapEntries);
900 static const WCHAR regtypeW[] = {'W','I','N','E','_','R','E','G','I','S','T','R','Y',0};
902 /***********************************************************************
903 * DllRegisterServer (ATL.@)
905 HRESULT WINAPI DllRegisterServer(void)
907 struct reg_info info;
909 info.registrar = NULL;
910 info.do_register = TRUE;
911 info.result = S_OK;
912 EnumResourceNamesW( hInst, regtypeW, register_resource, (LONG_PTR)&info );
913 if (info.registrar) IRegistrar_Release( info.registrar );
914 return info.result;
917 /***********************************************************************
918 * DllUnRegisterServer (ATL.@)
920 HRESULT WINAPI DllUnregisterServer(void)
922 struct reg_info info;
924 info.registrar = NULL;
925 info.do_register = FALSE;
926 info.result = S_OK;
927 EnumResourceNamesW( hInst, regtypeW, register_resource, (LONG_PTR)&info );
928 if (info.registrar) IRegistrar_Release( info.registrar );
929 return info.result;
932 /***********************************************************************
933 * DllCanUnloadNow (ATL.@)
935 HRESULT WINAPI DllCanUnloadNow(void)
937 TRACE("dll_count = %u\n", dll_count);
938 return dll_count ? S_FALSE : S_OK;