d3drm: Implement D3DRMVectorReflect.
[wine/dibdrv.git] / dlls / atl / registrar.c
blob2bc078d968c7c8538d2706787ba4c11fa88bb6dc
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"
30 #include "shlwapi.h"
32 #define ATL_INITGUID
33 #include "atliface.h"
34 #include "atlbase.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(atl);
41 static LONG dll_count;
43 /**************************************************************
44 * ATLRegistrar implementation
47 static const struct {
48 WCHAR name[22];
49 HKEY key;
50 } root_keys[] = {
51 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
52 HKEY_CLASSES_ROOT},
53 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
54 HKEY_CURRENT_USER},
55 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
56 HKEY_LOCAL_MACHINE},
57 {{'H','K','E','Y','_','U','S','E','R','S',0},
58 HKEY_USERS},
59 {{'H','K','E','Y','_','P','E','R','F','O','R','M','A','N','C','E','_','D','A','T','A',0},
60 HKEY_PERFORMANCE_DATA},
61 {{'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0},
62 HKEY_DYN_DATA},
63 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
64 HKEY_CURRENT_CONFIG},
65 {{'H','K','C','R',0}, HKEY_CLASSES_ROOT},
66 {{'H','K','C','U',0}, HKEY_CURRENT_USER},
67 {{'H','K','L','M',0}, HKEY_LOCAL_MACHINE},
68 {{'H','K','U',0}, HKEY_USERS},
69 {{'H','K','P','D',0}, HKEY_PERFORMANCE_DATA},
70 {{'H','K','D','D',0}, HKEY_DYN_DATA},
71 {{'H','K','C','C',0}, HKEY_CURRENT_CONFIG}
74 typedef struct rep_list_str {
75 LPOLESTR key;
76 LPOLESTR item;
77 int key_len;
78 struct rep_list_str *next;
79 } rep_list;
81 typedef struct {
82 const IRegistrarVtbl *lpVtbl;
83 LONG ref;
84 rep_list *rep;
85 } Registrar;
87 typedef struct {
88 LPOLESTR str;
89 DWORD alloc;
90 DWORD len;
91 } strbuf;
93 static void strbuf_init(strbuf *buf)
95 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
96 buf->alloc = 128;
97 buf->len = 0;
100 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
102 if(len == -1)
103 len = lstrlenW(str);
104 if(buf->len+len+1 >= buf->alloc) {
105 buf->alloc = (buf->len+len)<<1;
106 buf->str = HeapReAlloc(GetProcessHeap(), 0, buf->str, buf->alloc*sizeof(WCHAR));
108 memcpy(buf->str+buf->len, str, len*sizeof(OLECHAR));
109 buf->len += len;
110 buf->str[buf->len] = '\0';
113 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
115 LPCOLESTR iter, iter2 = *str;
117 buf->len = 0;
118 buf->str[0] = '\0';
120 while(isspaceW(*iter2))
121 iter2++;
122 iter = iter2;
123 if(!*iter) {
124 *str = iter;
125 return S_OK;
128 if(*iter == '}' || *iter == '=') {
129 strbuf_write(iter++, buf, 1);
130 }else if(*iter == '\'') {
131 iter2 = ++iter;
132 iter = strchrW(iter, '\'');
133 if(!iter) {
134 WARN("Unexpected end of script\n");
135 *str = iter;
136 return DISP_E_EXCEPTION;
138 strbuf_write(iter2, buf, iter-iter2);
139 iter++;
140 }else {
141 while(*iter && !isspaceW(*iter))
142 iter++;
143 strbuf_write(iter2, buf, iter-iter2);
146 while(isspaceW(*iter))
147 iter++;
148 *str = iter;
149 return S_OK;
152 static HRESULT do_preprocess(const Registrar *This, LPCOLESTR data, strbuf *buf)
154 LPCOLESTR iter, iter2 = data;
155 rep_list *rep_iter;
156 static const WCHAR wstr[] = {'%',0};
158 iter = strchrW(data, '%');
159 while(iter) {
160 strbuf_write(iter2, buf, iter-iter2);
162 iter2 = ++iter;
163 if(!*iter2)
164 return DISP_E_EXCEPTION;
165 iter = strchrW(iter2, '%');
166 if(!iter)
167 return DISP_E_EXCEPTION;
169 if(iter == iter2) {
170 strbuf_write(wstr, buf, 1);
171 }else {
172 for(rep_iter = This->rep; rep_iter; rep_iter = rep_iter->next) {
173 if(rep_iter->key_len == iter-iter2
174 && !memicmpW(iter2, rep_iter->key, rep_iter->key_len))
175 break;
177 if(!rep_iter) {
178 WARN("Could not find replacement: %s\n", debugstr_wn(iter2, iter-iter2));
179 return DISP_E_EXCEPTION;
182 strbuf_write(rep_iter->item, buf, -1);
185 iter2 = ++iter;
186 iter = strchrW(iter, '%');
189 strbuf_write(iter2, buf, -1);
190 TRACE("%s\n", debugstr_w(buf->str));
192 return S_OK;
195 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
197 LPCOLESTR iter = *pstr;
198 HRESULT hres;
199 LONG lres;
200 HKEY hkey = 0;
201 strbuf name;
203 enum {
204 NORMAL,
205 NO_REMOVE,
206 IS_VAL,
207 FORCE_REMOVE,
208 DO_DELETE
209 } key_type = NORMAL;
211 static const WCHAR wstrNoRemove[] = {'N','o','R','e','m','o','v','e',0};
212 static const WCHAR wstrForceRemove[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
213 static const WCHAR wstrDelete[] = {'D','e','l','e','t','e',0};
214 static const WCHAR wstrval[] = {'v','a','l',0};
216 iter = *pstr;
217 hres = get_word(&iter, buf);
218 if(FAILED(hres))
219 return hres;
220 strbuf_init(&name);
222 while(buf->str[1] || buf->str[0] != '}') {
223 key_type = NORMAL;
224 if(!lstrcmpiW(buf->str, wstrNoRemove))
225 key_type = NO_REMOVE;
226 else if(!lstrcmpiW(buf->str, wstrForceRemove))
227 key_type = FORCE_REMOVE;
228 else if(!lstrcmpiW(buf->str, wstrval))
229 key_type = IS_VAL;
230 else if(!lstrcmpiW(buf->str, wstrDelete))
231 key_type = DO_DELETE;
233 if(key_type != NORMAL) {
234 hres = get_word(&iter, buf);
235 if(FAILED(hres))
236 break;
238 TRACE("name = %s\n", debugstr_w(buf->str));
240 if(do_register) {
241 if(key_type == IS_VAL) {
242 hkey = parent_key;
243 strbuf_write(buf->str, &name, -1);
244 }else if(key_type == DO_DELETE) {
245 TRACE("Deleting %s\n", debugstr_w(buf->str));
246 lres = SHDeleteKeyW(parent_key, buf->str);
247 }else {
248 if(key_type == FORCE_REMOVE)
249 SHDeleteKeyW(parent_key, buf->str);
250 lres = RegCreateKeyW(parent_key, buf->str, &hkey);
251 if(lres != ERROR_SUCCESS) {
252 WARN("Could not create(open) key: %08x\n", lres);
253 hres = HRESULT_FROM_WIN32(lres);
254 break;
257 }else if(key_type != IS_VAL && key_type != DO_DELETE) {
258 strbuf_write(buf->str, &name, -1);
259 lres = RegOpenKeyW(parent_key, buf->str, &hkey);
260 if(lres != ERROR_SUCCESS)
261 WARN("Could not open key %s: %08x\n", debugstr_w(name.str), lres);
264 if(key_type != DO_DELETE && *iter == '=') {
265 iter++;
266 hres = get_word(&iter, buf);
267 if(FAILED(hres))
268 break;
269 if(buf->len != 1) {
270 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
271 hres = DISP_E_EXCEPTION;
272 break;
274 if(do_register) {
275 switch(buf->str[0]) {
276 case 's':
277 hres = get_word(&iter, buf);
278 if(FAILED(hres))
279 break;
280 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_SZ, (PBYTE)buf->str,
281 (lstrlenW(buf->str)+1)*sizeof(WCHAR));
282 if(lres != ERROR_SUCCESS) {
283 WARN("Could set value of key: %08x\n", lres);
284 hres = HRESULT_FROM_WIN32(lres);
285 break;
287 break;
288 case 'd': {
289 WCHAR *end;
290 DWORD dw;
291 if(*iter == '0' && iter[1] == 'x') {
292 iter += 2;
293 dw = strtolW(iter, &end, 16);
294 }else {
295 dw = strtolW(iter, &end, 10);
297 iter = end;
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 default:
308 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
309 hres = DISP_E_EXCEPTION;
311 if(FAILED(hres))
312 break;
313 }else {
314 if(*iter == '-')
315 iter++;
316 hres = get_word(&iter, buf);
317 if(FAILED(hres))
318 break;
320 }else if(key_type == IS_VAL) {
321 WARN("value not set!\n");
322 hres = DISP_E_EXCEPTION;
323 break;
326 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
327 hres = get_word(&iter, buf);
328 if(FAILED(hres))
329 break;
330 hres = do_process_key(&iter, hkey, buf, do_register);
331 if(FAILED(hres))
332 break;
335 TRACE("%x %x\n", do_register, key_type);
336 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
337 TRACE("Deleting %s\n", debugstr_w(name.str));
338 RegDeleteKeyW(parent_key, name.str);
341 if(hkey && key_type != IS_VAL)
342 RegCloseKey(hkey);
343 hkey = 0;
344 name.len = 0;
346 hres = get_word(&iter, buf);
347 if(FAILED(hres))
348 break;
351 HeapFree(GetProcessHeap(), 0, name.str);
352 if(hkey && key_type != IS_VAL)
353 RegCloseKey(hkey);
354 *pstr = iter;
355 return hres;
358 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
360 LPCOLESTR iter = data;
361 strbuf buf;
362 HRESULT hres = S_OK;
363 unsigned int i;
365 strbuf_init(&buf);
366 hres = get_word(&iter, &buf);
367 if(FAILED(hres))
368 return hres;
370 while(*iter) {
371 if(!buf.len) {
372 WARN("ward.len == 0, failed\n");
373 hres = DISP_E_EXCEPTION;
374 break;
376 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
377 if(!lstrcmpiW(buf.str, root_keys[i].name))
378 break;
380 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
381 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
382 hres = DISP_E_EXCEPTION;
383 break;
385 hres = get_word(&iter, &buf);
386 if(FAILED(hres))
387 break;
388 if(buf.str[1] || buf.str[0] != '{') {
389 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
390 hres = DISP_E_EXCEPTION;
391 break;
393 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
394 if(FAILED(hres)) {
395 WARN("Processing key failed: %08x\n", hres);
396 break;
398 hres = get_word(&iter, &buf);
399 if(FAILED(hres))
400 break;
402 HeapFree(GetProcessHeap(), 0, buf.str);
403 return hres;
406 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
408 strbuf buf;
409 HRESULT hres;
411 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
413 strbuf_init(&buf);
414 hres = do_preprocess(This, data, &buf);
415 if(FAILED(hres)) {
416 WARN("preprocessing failed!\n");
417 HeapFree(GetProcessHeap(), 0, buf.str);
418 return hres;
421 hres = do_process_root_key(buf.str, do_register);
422 if(FAILED(hres) && do_register)
423 do_process_root_key(buf.str, FALSE);
425 HeapFree(GetProcessHeap(), 0, buf.str);
426 return hres;
429 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
430 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
432 HINSTANCE hins;
433 HRSRC src;
434 LPSTR regstra;
435 LPWSTR regstrw;
436 DWORD len, reslen;
437 HRESULT hres;
439 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
440 if(hins) {
441 src = FindResourceW(hins, szID, szType);
442 if(src) {
443 regstra = (LPSTR)LoadResource(hins, src);
444 reslen = SizeofResource(hins, src);
445 if(regstra) {
446 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
447 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
448 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, -1);
449 regstrw[len-1] = '\0';
451 hres = string_register(This, regstrw, do_register);
453 HeapFree(GetProcessHeap(), 0, regstrw);
454 }else {
455 WARN("could not load resource\n");
456 hres = HRESULT_FROM_WIN32(GetLastError());
458 HeapFree(GetProcessHeap(), 0, regstra);
459 }else {
460 WARN("Could not find source\n");
461 hres = HRESULT_FROM_WIN32(GetLastError());
463 FreeLibrary(hins);
464 }else {
465 WARN("Could not load resource file\n");
466 hres = HRESULT_FROM_WIN32(GetLastError());
469 return hres;
472 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
474 HANDLE file;
475 DWORD filelen, len;
476 LPWSTR regstrw;
477 LPSTR regstra;
478 LRESULT lres;
479 HRESULT hres;
481 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
482 if(file != INVALID_HANDLE_VALUE) {
483 filelen = GetFileSize(file, NULL);
484 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
485 lres = ReadFile(file, regstra, filelen, NULL, NULL);
486 if(lres == ERROR_SUCCESS) {
487 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
488 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
489 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, -1);
490 regstrw[len-1] = '\0';
492 hres = string_register(This, regstrw, do_register);
494 HeapFree(GetProcessHeap(), 0, regstrw);
495 }else {
496 WARN("Failed to read faile\n");
497 hres = HRESULT_FROM_WIN32(lres);
499 HeapFree(GetProcessHeap(), 0, regstra);
500 CloseHandle(file);
501 }else {
502 WARN("Could not open file\n");
503 hres = HRESULT_FROM_WIN32(GetLastError());
506 return hres;
509 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
511 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
513 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
514 IRegistrar_AddRef(iface);
515 *ppvObject = iface;
516 return S_OK;
518 return E_NOINTERFACE;
521 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
523 Registrar *This = (Registrar*)iface;
524 ULONG ref = InterlockedIncrement(&This->ref);
525 TRACE("(%p) ->%d\n", This, ref);
526 return ref;
529 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
531 Registrar *This = (Registrar*)iface;
532 ULONG ref = InterlockedDecrement(&This->ref);
534 TRACE("(%p) ->%d\n", This, ref);
535 if(!ref) {
536 IRegistrar_ClearReplacements(iface);
537 HeapFree(GetProcessHeap(), 0, This);
538 InterlockedDecrement(&dll_count);
540 return ref;
543 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
545 Registrar *This = (Registrar*)iface;
546 int len;
547 rep_list *new_rep;
549 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
551 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
553 new_rep->key_len = lstrlenW(Key);
554 new_rep->key = HeapAlloc(GetProcessHeap(), 0, new_rep->key_len*sizeof(OLECHAR)+1);
555 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
557 len = lstrlenW(item)+1;
558 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
559 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
561 new_rep->next = This->rep;
562 This->rep = new_rep;
564 return S_OK;
567 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
569 Registrar *This = (Registrar*)iface;
570 rep_list *iter, *iter2;
572 TRACE("(%p)\n", This);
574 if(!This->rep)
575 return S_OK;
577 iter = This->rep;
578 while(iter) {
579 iter2 = iter->next;
580 HeapFree(GetProcessHeap(), 0, iter->key);
581 HeapFree(GetProcessHeap(), 0, iter->item);
582 HeapFree(GetProcessHeap(), 0, iter);
583 iter = iter2;
586 This->rep = NULL;
587 return S_OK;
590 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
591 LPCOLESTR szID, LPCOLESTR szType)
593 Registrar *This = (Registrar*)iface;
594 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
595 return resource_register(This, resFileName, szID, szType, TRUE);
598 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
599 LPCOLESTR szID, LPCOLESTR szType)
601 Registrar *This = (Registrar*)iface;
602 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
603 return resource_register(This, resFileName, szID, szType, FALSE);
606 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
608 Registrar *This = (Registrar*)iface;
609 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
610 return file_register(This, fileName, TRUE);
613 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
615 Registrar *This = (Registrar*)iface;
616 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
617 return file_register(This, fileName, FALSE);
620 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
622 Registrar *This = (Registrar*)iface;
623 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
624 return string_register(This, data, TRUE);
627 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
629 Registrar *This = (Registrar*)iface;
630 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
631 return string_register(This, data, FALSE);
634 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
635 UINT nID, LPCOLESTR szType)
637 Registrar *This = (Registrar*)iface;
638 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
639 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, TRUE);
642 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
643 UINT nID, LPCOLESTR szType)
645 Registrar *This = (Registrar*)iface;
646 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
647 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, FALSE);
650 static const IRegistrarVtbl RegistrarVtbl = {
651 Registrar_QueryInterface,
652 Registrar_AddRef,
653 Registrar_Release,
654 Registrar_AddReplacement,
655 Registrar_ClearReplacements,
656 Registrar_ResourceRegisterSz,
657 Registrar_ResourceUnregisterSz,
658 Registrar_FileRegister,
659 Registrar_FileUnregister,
660 Registrar_StringRegister,
661 Registrar_StringUnregister,
662 Registrar_ResourceRegister,
663 Registrar_ResourceUnregister,
666 static HRESULT Registrar_create(const IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
668 Registrar *ret;
670 if(!IsEqualGUID(&IID_IUnknown, riid) && !IsEqualGUID(&IID_IRegistrar, riid))
671 return E_NOINTERFACE;
673 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar));
674 ret->lpVtbl = &RegistrarVtbl;
675 ret->ref = 1;
676 ret->rep = NULL;
677 *ppvObject = ret;
679 InterlockedIncrement(&dll_count);
681 return S_OK;
684 /**************************************************************
685 * ClassFactory implementation
688 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
690 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
692 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
693 *ppvObject = iface;
694 return S_OK;
697 return E_NOINTERFACE;
700 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
702 InterlockedIncrement(&dll_count);
703 return 2;
706 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
708 InterlockedDecrement(&dll_count);
709 return 1;
712 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
713 REFIID riid, void **ppvObject)
715 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
716 return Registrar_create(pUnkOuter, riid, ppvObject);
719 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
721 TRACE("(%p)->(%x)\n", iface, lock);
723 if(lock)
724 InterlockedIncrement(&dll_count);
725 else
726 InterlockedDecrement(&dll_count);
728 return S_OK;
731 static const IClassFactoryVtbl IRegistrarCFVtbl = {
732 RegistrarCF_QueryInterface,
733 RegistrarCF_AddRef,
734 RegistrarCF_Release,
735 RegistrarCF_CreateInstance,
736 RegistrarCF_LockServer
739 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
741 /**************************************************************
742 * DllGetClassObject (ATL.2)
744 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
746 TRACE("(%s %s %p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
748 if(IsEqualGUID(&CLSID_ATLRegistrar, clsid)) {
749 *ppvObject = &RegistrarCF;
750 return S_OK;
753 FIXME("Not supported class %s\n", debugstr_guid(clsid));
754 return CLASS_E_CLASSNOTAVAILABLE;
757 extern HINSTANCE hInst;
759 static HRESULT do_register_dll_server(IRegistrar *pRegistrar, LPCOLESTR wszDll,
760 LPCOLESTR wszId, BOOL do_register,
761 const struct _ATL_REGMAP_ENTRY* pMapEntries)
763 WCHAR buf[MAX_PATH];
764 HRESULT hres;
765 const struct _ATL_REGMAP_ENTRY *pMapEntry;
767 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
768 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
769 static const WCHAR wszCLSID_ATLRegistrar[] =
770 {'C','L','S','I','D','_','A','T','L','R','e','g','i','s','t','r','a','r',0};
772 if (!pRegistrar)
773 Registrar_create(NULL, &IID_IRegistrar, (void**)&pRegistrar);
775 IRegistrar_AddReplacement(pRegistrar, wszModule, wszDll);
777 for (pMapEntry = pMapEntries; pMapEntry && pMapEntry->szKey; pMapEntry++)
778 IRegistrar_AddReplacement(pRegistrar, pMapEntry->szKey, pMapEntry->szData);
780 StringFromGUID2(&CLSID_ATLRegistrar, buf, sizeof(buf)/sizeof(buf[0]));
781 IRegistrar_AddReplacement(pRegistrar, wszCLSID_ATLRegistrar, buf);
783 if(do_register)
784 hres = IRegistrar_ResourceRegisterSz(pRegistrar, wszDll, wszId, wszRegistry);
785 else
786 hres = IRegistrar_ResourceUnregisterSz(pRegistrar, wszDll, wszId, wszRegistry);
788 IRegistrar_Release(pRegistrar);
789 return hres;
792 static HRESULT do_register_server(BOOL do_register)
794 static const WCHAR wszDll[] = {'a','t','l','.','d','l','l',0};
795 return do_register_dll_server(NULL, wszDll, MAKEINTRESOURCEW(101), do_register, NULL);
798 /***********************************************************************
799 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
802 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
803 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
805 HINSTANCE lhInst = pM->m_hInst;
806 /* everything inside this function below this point
807 * should go into atl71.AtlUpdateRegistryFromResourceD
809 WCHAR module_name[MAX_PATH];
811 if(!GetModuleFileNameW(lhInst, module_name, MAX_PATH)) {
812 FIXME("hinst %p: did not get module name\n",
813 lhInst);
814 return E_FAIL;
817 TRACE("%p (%s), %s, %d, %p, %p\n", hInst, debugstr_w(module_name),
818 debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
820 return do_register_dll_server(pReg, module_name, lpszRes, bRegister, pMapEntries);
823 /***********************************************************************
824 * DllRegisterServer (ATL.@)
826 HRESULT WINAPI DllRegisterServer(void)
828 TRACE("\n");
829 return do_register_server(TRUE);
832 /***********************************************************************
833 * DllRegisterServer (ATL.@)
835 HRESULT WINAPI DllUnregisterServer(void)
837 TRACE("\n");
838 return do_register_server(FALSE);
841 /***********************************************************************
842 * DllCanUnloadNow (ATL.@)
844 HRESULT WINAPI DllCanUnloadNow(void)
846 TRACE("dll_count = %u\n", dll_count);
847 return dll_count ? S_FALSE : S_OK;