wined3d: Add a test for fixed function bump mapping.
[wine/multimedia.git] / dlls / atl / registrar.c
blob9c0c73b6d851c3740f0009f3556b0ca16cf3fb02
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 const IRegistrarVtbl *lpVtbl;
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 void strbuf_init(strbuf *buf)
94 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
95 buf->alloc = 128;
96 buf->len = 0;
99 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
101 if(len == -1)
102 len = lstrlenW(str);
103 if(buf->len+len+1 >= buf->alloc) {
104 buf->alloc = (buf->len+len)<<1;
105 buf->str = HeapReAlloc(GetProcessHeap(), 0, buf->str, buf->alloc*sizeof(WCHAR));
107 memcpy(buf->str+buf->len, str, len*sizeof(OLECHAR));
108 buf->len += len;
109 buf->str[buf->len] = '\0';
112 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
114 LPCOLESTR iter, iter2 = *str;
116 buf->len = 0;
117 buf->str[0] = '\0';
119 while(isspaceW(*iter2))
120 iter2++;
121 iter = iter2;
122 if(!*iter) {
123 *str = iter;
124 return S_OK;
127 if(*iter == '}' || *iter == '=') {
128 strbuf_write(iter++, buf, 1);
129 }else if(*iter == '\'') {
130 iter2 = ++iter;
131 iter = strchrW(iter, '\'');
132 if(!iter) {
133 WARN("Unexpected end of script\n");
134 *str = iter;
135 return DISP_E_EXCEPTION;
137 strbuf_write(iter2, buf, iter-iter2);
138 iter++;
139 }else {
140 while(*iter && !isspaceW(*iter))
141 iter++;
142 strbuf_write(iter2, buf, iter-iter2);
145 while(isspaceW(*iter))
146 iter++;
147 *str = iter;
148 return S_OK;
151 static HRESULT do_preprocess(const Registrar *This, LPCOLESTR data, strbuf *buf)
153 LPCOLESTR iter, iter2 = data;
154 rep_list *rep_iter;
155 static const WCHAR wstr[] = {'%',0};
157 iter = strchrW(data, '%');
158 while(iter) {
159 strbuf_write(iter2, buf, iter-iter2);
161 iter2 = ++iter;
162 if(!*iter2)
163 return DISP_E_EXCEPTION;
164 iter = strchrW(iter2, '%');
165 if(!iter)
166 return DISP_E_EXCEPTION;
168 if(iter == iter2) {
169 strbuf_write(wstr, buf, 1);
170 }else {
171 for(rep_iter = This->rep; rep_iter; rep_iter = rep_iter->next) {
172 if(rep_iter->key_len == iter-iter2
173 && !memicmpW(iter2, rep_iter->key, rep_iter->key_len))
174 break;
176 if(!rep_iter) {
177 WARN("Could not find replacement: %s\n", debugstr_wn(iter2, iter-iter2));
178 return DISP_E_EXCEPTION;
181 strbuf_write(rep_iter->item, buf, -1);
184 iter2 = ++iter;
185 iter = strchrW(iter, '%');
188 strbuf_write(iter2, buf, -1);
189 TRACE("%s\n", debugstr_w(buf->str));
191 return S_OK;
194 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
196 LPCOLESTR iter = *pstr;
197 HRESULT hres;
198 LONG lres;
199 HKEY hkey = 0;
200 strbuf name;
202 enum {
203 NORMAL,
204 NO_REMOVE,
205 IS_VAL,
206 FORCE_REMOVE,
207 DO_DELETE
208 } key_type = NORMAL;
210 static const WCHAR wstrNoRemove[] = {'N','o','R','e','m','o','v','e',0};
211 static const WCHAR wstrForceRemove[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
212 static const WCHAR wstrDelete[] = {'D','e','l','e','t','e',0};
213 static const WCHAR wstrval[] = {'v','a','l',0};
215 iter = *pstr;
216 hres = get_word(&iter, buf);
217 if(FAILED(hres))
218 return hres;
219 strbuf_init(&name);
221 while(buf->str[1] || buf->str[0] != '}') {
222 key_type = NORMAL;
223 if(!lstrcmpiW(buf->str, wstrNoRemove))
224 key_type = NO_REMOVE;
225 else if(!lstrcmpiW(buf->str, wstrForceRemove))
226 key_type = FORCE_REMOVE;
227 else if(!lstrcmpiW(buf->str, wstrval))
228 key_type = IS_VAL;
229 else if(!lstrcmpiW(buf->str, wstrDelete))
230 key_type = DO_DELETE;
232 if(key_type != NORMAL) {
233 hres = get_word(&iter, buf);
234 if(FAILED(hres))
235 break;
237 TRACE("name = %s\n", debugstr_w(buf->str));
239 if(do_register) {
240 if(key_type == IS_VAL) {
241 hkey = parent_key;
242 strbuf_write(buf->str, &name, -1);
243 }else if(key_type == DO_DELETE) {
244 TRACE("Deleting %s\n", debugstr_w(buf->str));
245 lres = RegDeleteTreeW(parent_key, buf->str);
246 }else {
247 if(key_type == FORCE_REMOVE)
248 RegDeleteTreeW(parent_key, buf->str);
249 lres = RegCreateKeyW(parent_key, buf->str, &hkey);
250 if(lres != ERROR_SUCCESS) {
251 WARN("Could not create(open) key: %08x\n", lres);
252 hres = HRESULT_FROM_WIN32(lres);
253 break;
256 }else if(key_type != IS_VAL && key_type != DO_DELETE) {
257 strbuf_write(buf->str, &name, -1);
258 lres = RegOpenKeyW(parent_key, buf->str, &hkey);
259 if(lres != ERROR_SUCCESS)
260 WARN("Could not open key %s: %08x\n", debugstr_w(name.str), lres);
263 if(key_type != DO_DELETE && *iter == '=') {
264 iter++;
265 hres = get_word(&iter, buf);
266 if(FAILED(hres))
267 break;
268 if(buf->len != 1) {
269 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
270 hres = DISP_E_EXCEPTION;
271 break;
273 if(do_register) {
274 switch(buf->str[0]) {
275 case 's':
276 hres = get_word(&iter, buf);
277 if(FAILED(hres))
278 break;
279 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_SZ, (PBYTE)buf->str,
280 (lstrlenW(buf->str)+1)*sizeof(WCHAR));
281 if(lres != ERROR_SUCCESS) {
282 WARN("Could set value of key: %08x\n", lres);
283 hres = HRESULT_FROM_WIN32(lres);
284 break;
286 break;
287 case 'd': {
288 WCHAR *end;
289 DWORD dw;
290 if(*iter == '0' && iter[1] == 'x') {
291 iter += 2;
292 dw = strtolW(iter, &end, 16);
293 }else {
294 dw = strtolW(iter, &end, 10);
296 iter = end;
297 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_DWORD,
298 (PBYTE)&dw, sizeof(dw));
299 if(lres != ERROR_SUCCESS) {
300 WARN("Could set value of key: %08x\n", lres);
301 hres = HRESULT_FROM_WIN32(lres);
302 break;
304 break;
306 default:
307 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
308 hres = DISP_E_EXCEPTION;
310 if(FAILED(hres))
311 break;
312 }else {
313 if(*iter == '-')
314 iter++;
315 hres = get_word(&iter, buf);
316 if(FAILED(hres))
317 break;
319 }else if(key_type == IS_VAL) {
320 WARN("value not set!\n");
321 hres = DISP_E_EXCEPTION;
322 break;
325 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
326 hres = get_word(&iter, buf);
327 if(FAILED(hres))
328 break;
329 hres = do_process_key(&iter, hkey, buf, do_register);
330 if(FAILED(hres))
331 break;
334 TRACE("%x %x\n", do_register, key_type);
335 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
336 TRACE("Deleting %s\n", debugstr_w(name.str));
337 RegDeleteKeyW(parent_key, name.str);
340 if(hkey && key_type != IS_VAL)
341 RegCloseKey(hkey);
342 hkey = 0;
343 name.len = 0;
345 hres = get_word(&iter, buf);
346 if(FAILED(hres))
347 break;
350 HeapFree(GetProcessHeap(), 0, name.str);
351 if(hkey && key_type != IS_VAL)
352 RegCloseKey(hkey);
353 *pstr = iter;
354 return hres;
357 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
359 LPCOLESTR iter = data;
360 strbuf buf;
361 HRESULT hres = S_OK;
362 unsigned int i;
364 strbuf_init(&buf);
365 hres = get_word(&iter, &buf);
366 if(FAILED(hres))
367 return hres;
369 while(*iter) {
370 if(!buf.len) {
371 WARN("ward.len == 0, failed\n");
372 hres = DISP_E_EXCEPTION;
373 break;
375 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
376 if(!lstrcmpiW(buf.str, root_keys[i].name))
377 break;
379 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
380 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
381 hres = DISP_E_EXCEPTION;
382 break;
384 hres = get_word(&iter, &buf);
385 if(FAILED(hres))
386 break;
387 if(buf.str[1] || buf.str[0] != '{') {
388 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
389 hres = DISP_E_EXCEPTION;
390 break;
392 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
393 if(FAILED(hres)) {
394 WARN("Processing key failed: %08x\n", hres);
395 break;
397 hres = get_word(&iter, &buf);
398 if(FAILED(hres))
399 break;
401 HeapFree(GetProcessHeap(), 0, buf.str);
402 return hres;
405 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
407 strbuf buf;
408 HRESULT hres;
410 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
412 strbuf_init(&buf);
413 hres = do_preprocess(This, data, &buf);
414 if(FAILED(hres)) {
415 WARN("preprocessing failed!\n");
416 HeapFree(GetProcessHeap(), 0, buf.str);
417 return hres;
420 hres = do_process_root_key(buf.str, do_register);
421 if(FAILED(hres) && do_register)
422 do_process_root_key(buf.str, FALSE);
424 HeapFree(GetProcessHeap(), 0, buf.str);
425 return hres;
428 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
429 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
431 HINSTANCE hins;
432 HRSRC src;
433 LPSTR regstra;
434 LPWSTR regstrw;
435 DWORD len, reslen;
436 HRESULT hres;
438 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
439 if(hins) {
440 src = FindResourceW(hins, szID, szType);
441 if(src) {
442 regstra = (LPSTR)LoadResource(hins, src);
443 reslen = SizeofResource(hins, src);
444 if(regstra) {
445 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
446 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
447 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, -1);
448 regstrw[len-1] = '\0';
450 hres = string_register(This, regstrw, do_register);
452 HeapFree(GetProcessHeap(), 0, regstrw);
453 }else {
454 WARN("could not load resource\n");
455 hres = HRESULT_FROM_WIN32(GetLastError());
457 HeapFree(GetProcessHeap(), 0, regstra);
458 }else {
459 WARN("Could not find source\n");
460 hres = HRESULT_FROM_WIN32(GetLastError());
462 FreeLibrary(hins);
463 }else {
464 WARN("Could not load resource file\n");
465 hres = HRESULT_FROM_WIN32(GetLastError());
468 return hres;
471 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
473 HANDLE file;
474 DWORD filelen, len;
475 LPWSTR regstrw;
476 LPSTR regstra;
477 LRESULT lres;
478 HRESULT hres;
480 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
481 if(file != INVALID_HANDLE_VALUE) {
482 filelen = GetFileSize(file, NULL);
483 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
484 lres = ReadFile(file, regstra, filelen, NULL, NULL);
485 if(lres == ERROR_SUCCESS) {
486 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
487 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
488 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, -1);
489 regstrw[len-1] = '\0';
491 hres = string_register(This, regstrw, do_register);
493 HeapFree(GetProcessHeap(), 0, regstrw);
494 }else {
495 WARN("Failed to read faile\n");
496 hres = HRESULT_FROM_WIN32(lres);
498 HeapFree(GetProcessHeap(), 0, regstra);
499 CloseHandle(file);
500 }else {
501 WARN("Could not open file\n");
502 hres = HRESULT_FROM_WIN32(GetLastError());
505 return hres;
508 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
510 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
512 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
513 IRegistrar_AddRef(iface);
514 *ppvObject = iface;
515 return S_OK;
517 return E_NOINTERFACE;
520 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
522 Registrar *This = (Registrar*)iface;
523 ULONG ref = InterlockedIncrement(&This->ref);
524 TRACE("(%p) ->%d\n", This, ref);
525 return ref;
528 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
530 Registrar *This = (Registrar*)iface;
531 ULONG ref = InterlockedDecrement(&This->ref);
533 TRACE("(%p) ->%d\n", This, ref);
534 if(!ref) {
535 IRegistrar_ClearReplacements(iface);
536 HeapFree(GetProcessHeap(), 0, This);
537 InterlockedDecrement(&dll_count);
539 return ref;
542 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
544 Registrar *This = (Registrar*)iface;
545 int len;
546 rep_list *new_rep;
548 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
550 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
552 new_rep->key_len = lstrlenW(Key);
553 new_rep->key = HeapAlloc(GetProcessHeap(), 0, new_rep->key_len*sizeof(OLECHAR)+1);
554 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
556 len = lstrlenW(item)+1;
557 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
558 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
560 new_rep->next = This->rep;
561 This->rep = new_rep;
563 return S_OK;
566 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
568 Registrar *This = (Registrar*)iface;
569 rep_list *iter, *iter2;
571 TRACE("(%p)\n", This);
573 if(!This->rep)
574 return S_OK;
576 iter = This->rep;
577 while(iter) {
578 iter2 = iter->next;
579 HeapFree(GetProcessHeap(), 0, iter->key);
580 HeapFree(GetProcessHeap(), 0, iter->item);
581 HeapFree(GetProcessHeap(), 0, iter);
582 iter = iter2;
585 This->rep = NULL;
586 return S_OK;
589 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
590 LPCOLESTR szID, LPCOLESTR szType)
592 Registrar *This = (Registrar*)iface;
593 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
594 return resource_register(This, resFileName, szID, szType, TRUE);
597 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
598 LPCOLESTR szID, LPCOLESTR szType)
600 Registrar *This = (Registrar*)iface;
601 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
602 return resource_register(This, resFileName, szID, szType, FALSE);
605 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
607 Registrar *This = (Registrar*)iface;
608 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
609 return file_register(This, fileName, TRUE);
612 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
614 Registrar *This = (Registrar*)iface;
615 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
616 return file_register(This, fileName, FALSE);
619 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
621 Registrar *This = (Registrar*)iface;
622 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
623 return string_register(This, data, TRUE);
626 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
628 Registrar *This = (Registrar*)iface;
629 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
630 return string_register(This, data, FALSE);
633 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
634 UINT nID, LPCOLESTR szType)
636 Registrar *This = (Registrar*)iface;
637 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
638 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, TRUE);
641 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
642 UINT nID, LPCOLESTR szType)
644 Registrar *This = (Registrar*)iface;
645 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
646 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, FALSE);
649 static const IRegistrarVtbl RegistrarVtbl = {
650 Registrar_QueryInterface,
651 Registrar_AddRef,
652 Registrar_Release,
653 Registrar_AddReplacement,
654 Registrar_ClearReplacements,
655 Registrar_ResourceRegisterSz,
656 Registrar_ResourceUnregisterSz,
657 Registrar_FileRegister,
658 Registrar_FileUnregister,
659 Registrar_StringRegister,
660 Registrar_StringUnregister,
661 Registrar_ResourceRegister,
662 Registrar_ResourceUnregister,
665 static HRESULT Registrar_create(const IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
667 Registrar *ret;
669 if(!IsEqualGUID(&IID_IUnknown, riid) && !IsEqualGUID(&IID_IRegistrar, riid))
670 return E_NOINTERFACE;
672 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar));
673 ret->lpVtbl = &RegistrarVtbl;
674 ret->ref = 1;
675 ret->rep = NULL;
676 *ppvObject = ret;
678 InterlockedIncrement(&dll_count);
680 return S_OK;
683 /**************************************************************
684 * ClassFactory implementation
687 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
689 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
691 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
692 *ppvObject = iface;
693 return S_OK;
696 return E_NOINTERFACE;
699 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
701 InterlockedIncrement(&dll_count);
702 return 2;
705 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
707 InterlockedDecrement(&dll_count);
708 return 1;
711 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
712 REFIID riid, void **ppvObject)
714 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
715 return Registrar_create(pUnkOuter, riid, ppvObject);
718 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
720 TRACE("(%p)->(%x)\n", iface, lock);
722 if(lock)
723 InterlockedIncrement(&dll_count);
724 else
725 InterlockedDecrement(&dll_count);
727 return S_OK;
730 static const IClassFactoryVtbl IRegistrarCFVtbl = {
731 RegistrarCF_QueryInterface,
732 RegistrarCF_AddRef,
733 RegistrarCF_Release,
734 RegistrarCF_CreateInstance,
735 RegistrarCF_LockServer
738 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
740 /**************************************************************
741 * DllGetClassObject (ATL.2)
743 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
745 TRACE("(%s %s %p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
747 if(IsEqualGUID(&CLSID_ATLRegistrar, clsid)) {
748 *ppvObject = &RegistrarCF;
749 return S_OK;
752 FIXME("Not supported class %s\n", debugstr_guid(clsid));
753 return CLASS_E_CLASSNOTAVAILABLE;
756 extern HINSTANCE hInst;
758 static HRESULT do_register_dll_server(IRegistrar *pRegistrar, LPCOLESTR wszDll,
759 LPCOLESTR wszId, BOOL do_register,
760 const struct _ATL_REGMAP_ENTRY* pMapEntries)
762 WCHAR buf[MAX_PATH];
763 HRESULT hres;
764 const struct _ATL_REGMAP_ENTRY *pMapEntry;
766 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
767 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
768 static const WCHAR wszCLSID_ATLRegistrar[] =
769 {'C','L','S','I','D','_','A','T','L','R','e','g','i','s','t','r','a','r',0};
771 if (!pRegistrar)
772 Registrar_create(NULL, &IID_IRegistrar, (void**)&pRegistrar);
774 IRegistrar_AddReplacement(pRegistrar, wszModule, wszDll);
776 for (pMapEntry = pMapEntries; pMapEntry && pMapEntry->szKey; pMapEntry++)
777 IRegistrar_AddReplacement(pRegistrar, pMapEntry->szKey, pMapEntry->szData);
779 StringFromGUID2(&CLSID_ATLRegistrar, buf, sizeof(buf)/sizeof(buf[0]));
780 IRegistrar_AddReplacement(pRegistrar, wszCLSID_ATLRegistrar, buf);
782 if(do_register)
783 hres = IRegistrar_ResourceRegisterSz(pRegistrar, wszDll, wszId, wszRegistry);
784 else
785 hres = IRegistrar_ResourceUnregisterSz(pRegistrar, wszDll, wszId, wszRegistry);
787 IRegistrar_Release(pRegistrar);
788 return hres;
791 static HRESULT do_register_server(BOOL do_register)
793 static const WCHAR wszDll[] = {'a','t','l','.','d','l','l',0};
794 return do_register_dll_server(NULL, wszDll, MAKEINTRESOURCEW(101), do_register, NULL);
797 /***********************************************************************
798 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
801 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
802 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
804 HINSTANCE lhInst = pM->m_hInst;
805 /* everything inside this function below this point
806 * should go into atl71.AtlUpdateRegistryFromResourceD
808 WCHAR module_name[MAX_PATH];
810 if(!GetModuleFileNameW(lhInst, module_name, MAX_PATH)) {
811 FIXME("hinst %p: did not get module name\n",
812 lhInst);
813 return E_FAIL;
816 TRACE("%p (%s), %s, %d, %p, %p\n", hInst, debugstr_w(module_name),
817 debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
819 return do_register_dll_server(pReg, module_name, lpszRes, bRegister, pMapEntries);
822 /***********************************************************************
823 * DllRegisterServer (ATL.@)
825 HRESULT WINAPI DllRegisterServer(void)
827 TRACE("\n");
828 return do_register_server(TRUE);
831 /***********************************************************************
832 * DllRegisterServer (ATL.@)
834 HRESULT WINAPI DllUnregisterServer(void)
836 TRACE("\n");
837 return do_register_server(FALSE);
840 /***********************************************************************
841 * DllCanUnloadNow (ATL.@)
843 HRESULT WINAPI DllCanUnloadNow(void)
845 TRACE("dll_count = %u\n", dll_count);
846 return dll_count ? S_FALSE : S_OK;