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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(atl);
42 /**************************************************************
43 * ATLRegistrar implementation
50 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
52 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
54 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
56 {{'H','K','E','Y','_','U','S','E','R','S',0},
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},
62 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
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 {
77 struct rep_list_str *next;
81 const IRegistrarVtbl *lpVtbl;
92 static void strbuf_init(strbuf *buf)
94 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
99 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
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));
109 buf->str[buf->len] = '\0';
112 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
114 LPCOLESTR iter, iter2 = *str;
119 while(isspaceW(*iter2))
127 if(*iter == '{' || *iter == '}' || *iter == '=') {
128 strbuf_write(iter++, buf, 1);
129 }else if(*iter == '\'') {
131 iter = strchrW(iter, '\'');
133 WARN("Unexpected end of script\n");
135 return DISP_E_EXCEPTION;
137 strbuf_write(iter2, buf, iter-iter2);
140 while(*iter && !isspaceW(*iter))
142 strbuf_write(iter2, buf, iter-iter2);
145 while(isspaceW(*iter))
151 static HRESULT do_preprocess(Registrar *This, LPCOLESTR data, strbuf *buf)
153 LPCOLESTR iter, iter2 = data;
155 static const WCHAR wstr[] = {'%',0};
157 iter = strchrW(data, '%');
159 strbuf_write(iter2, buf, iter-iter2);
163 return DISP_E_EXCEPTION;
164 iter = strchrW(iter2, '%');
166 return DISP_E_EXCEPTION;
169 strbuf_write(wstr, buf, 1);
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))
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);
185 iter = strchrW(iter, '%');
188 strbuf_write(iter2, buf, -1);
189 TRACE("%s\n", debugstr_w(buf->str));
194 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
196 LPCOLESTR iter = *pstr;
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};
216 hres = get_word(&iter, buf);
221 while(buf->str[1] || buf->str[0] != '}') {
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))
229 else if(!lstrcmpiW(buf->str, wstrDelete))
230 key_type = DO_DELETE;
232 if(key_type != NORMAL) {
233 hres = get_word(&iter, buf);
237 TRACE("name = %s\n", debugstr_w(buf->str));
240 if(key_type == IS_VAL) {
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 = SHDeleteKeyW(parent_key, buf->str);
247 if(key_type == FORCE_REMOVE)
248 SHDeleteKeyW(parent_key, buf->str);
249 lres = RegCreateKeyW(parent_key, buf->str, &hkey);
250 if(lres != ERROR_SUCCESS) {
251 WARN("Could not create(open) key: %08lx\n", lres);
252 hres = HRESULT_FROM_WIN32(lres);
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: %08lx\n", debugstr_w(name.str), lres);
263 if(key_type != DO_DELETE && *iter == '=') {
265 hres = get_word(&iter, buf);
269 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
270 hres = DISP_E_EXCEPTION;
274 switch(buf->str[0]) {
276 hres = get_word(&iter, buf);
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: %08lx\n", lres);
283 hres = HRESULT_FROM_WIN32(lres);
289 if(*iter == '0' && iter[1] == 'x') {
291 dw = strtolW(iter, (WCHAR**)&iter, 16);
293 dw = strtolW(iter, (WCHAR**)&iter, 10);
295 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_DWORD,
296 (PBYTE)&dw, sizeof(dw));
297 if(lres != ERROR_SUCCESS) {
298 WARN("Could set value of key: %08lx\n", lres);
299 hres = HRESULT_FROM_WIN32(lres);
305 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
306 hres = DISP_E_EXCEPTION;
313 hres = get_word(&iter, buf);
317 }else if(key_type == IS_VAL) {
318 WARN("value not set!\n");
319 hres = DISP_E_EXCEPTION;
323 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{') {
324 hres = get_word(&iter, buf);
327 hres = do_process_key(&iter, hkey, buf, do_register);
332 TRACE("%x %x\n", do_register, key_type);
333 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
334 TRACE("Deleting %s\n", debugstr_w(name.str));
335 RegDeleteKeyW(parent_key, name.str);
338 if(hkey && key_type != IS_VAL)
343 hres = get_word(&iter, buf);
348 HeapFree(GetProcessHeap(), 0, name.str);
349 if(hkey && key_type != IS_VAL)
355 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
357 LPCOLESTR iter = data;
363 hres = get_word(&iter, &buf);
369 WARN("ward.len == 0, failed\n");
370 hres = DISP_E_EXCEPTION;
373 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
374 if(!lstrcmpiW(buf.str, root_keys[i].name))
377 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
378 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
379 hres = DISP_E_EXCEPTION;
382 hres = get_word(&iter, &buf);
385 if(buf.str[1] || buf.str[0] != '{') {
386 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
387 hres = DISP_E_EXCEPTION;
390 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
392 WARN("Processing key failed: %08lx\n", hres);
395 hres = get_word(&iter, &buf);
399 HeapFree(GetProcessHeap(), 0, buf.str);
403 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
408 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
411 hres = do_preprocess(This, data, &buf);
413 WARN("preprocessing failed!\n");
414 HeapFree(GetProcessHeap(), 0, buf.str);
418 hres = do_process_root_key(buf.str, do_register);
419 if(FAILED(hres) && do_register)
420 do_process_root_key(buf.str, FALSE);
422 HeapFree(GetProcessHeap(), 0, buf.str);
426 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
427 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
436 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
438 src = FindResourceW(hins, szID, szType);
440 regstra = (LPSTR)LoadResource(hins, src);
441 reslen = SizeofResource(hins, src);
443 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
444 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
445 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, -1);
446 regstrw[len-1] = '\0';
448 hres = string_register(This, regstrw, do_register);
450 HeapFree(GetProcessHeap(), 0, regstrw);
452 WARN("could not load resource\n");
453 hres = HRESULT_FROM_WIN32(GetLastError());
455 HeapFree(GetProcessHeap(), 0, regstra);
457 WARN("Could not find source\n");
458 hres = HRESULT_FROM_WIN32(GetLastError());
462 WARN("Could not load resource file\n");
463 hres = HRESULT_FROM_WIN32(GetLastError());
469 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
478 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
480 filelen = GetFileSize(file, NULL);
481 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
482 lres = ReadFile(file, regstra, filelen, NULL, NULL);
483 if(lres == ERROR_SUCCESS) {
484 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
485 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
486 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, -1);
487 regstrw[len-1] = '\0';
489 hres = string_register(This, regstrw, do_register);
491 HeapFree(GetProcessHeap(), 0, regstrw);
493 WARN("Failed to read faile\n");
494 hres = HRESULT_FROM_WIN32(lres);
496 HeapFree(GetProcessHeap(), 0, regstra);
499 WARN("Could not open file\n");
500 hres = HRESULT_FROM_WIN32(GetLastError());
506 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
508 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
510 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
511 IRegistrar_AddRef(iface);
515 return E_NOINTERFACE;
518 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
520 Registrar *This = (Registrar*)iface;
521 ULONG ref = InterlockedIncrement(&This->ref);
522 TRACE("(%p) ->%ld\n", This, ref);
526 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
528 Registrar *This = (Registrar*)iface;
529 ULONG ref = InterlockedDecrement(&This->ref);
531 TRACE("(%p) ->%ld\n", This, ref);
533 IRegistrar_ClearReplacements(iface);
534 HeapFree(GetProcessHeap(), 0, This);
535 InterlockedDecrement(&dll_count);
540 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
542 Registrar *This = (Registrar*)iface;
546 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
548 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
550 new_rep->key_len = lstrlenW(Key);
551 new_rep->key = HeapAlloc(GetProcessHeap(), 0, new_rep->key_len*sizeof(OLECHAR)+1);
552 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
554 len = lstrlenW(item)+1;
555 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
556 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
558 new_rep->next = This->rep;
564 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
566 Registrar *This = (Registrar*)iface;
567 rep_list *iter, *iter2;
569 TRACE("(%p)\n", This);
577 HeapFree(GetProcessHeap(), 0, iter->key);
578 HeapFree(GetProcessHeap(), 0, iter->item);
579 HeapFree(GetProcessHeap(), 0, iter);
587 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
588 LPCOLESTR szID, LPCOLESTR szType)
590 Registrar *This = (Registrar*)iface;
591 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
592 return resource_register(This, resFileName, szID, szType, TRUE);
595 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
596 LPCOLESTR szID, LPCOLESTR szType)
598 Registrar *This = (Registrar*)iface;
599 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
600 return resource_register(This, resFileName, szID, szType, FALSE);
603 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
605 Registrar *This = (Registrar*)iface;
606 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
607 return file_register(This, fileName, TRUE);
610 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
612 Registrar *This = (Registrar*)iface;
613 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
614 return file_register(This, fileName, FALSE);
617 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
619 Registrar *This = (Registrar*)iface;
620 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
621 return string_register(This, data, TRUE);
624 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
626 Registrar *This = (Registrar*)iface;
627 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
628 return string_register(This, data, FALSE);
631 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
632 UINT nID, LPCOLESTR szType)
634 Registrar *This = (Registrar*)iface;
635 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
636 return resource_register(This, resFileName, (LPOLESTR)nID, szType, TRUE);
639 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
640 UINT nID, LPCOLESTR szType)
642 Registrar *This = (Registrar*)iface;
643 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
644 return resource_register(This, resFileName, (LPOLESTR)nID, szType, FALSE);
647 static const IRegistrarVtbl RegistrarVtbl = {
648 Registrar_QueryInterface,
651 Registrar_AddReplacement,
652 Registrar_ClearReplacements,
653 Registrar_ResourceRegisterSz,
654 Registrar_ResourceUnregisterSz,
655 Registrar_FileRegister,
656 Registrar_FileUnregister,
657 Registrar_StringRegister,
658 Registrar_StringUnregister,
659 Registrar_ResourceRegister,
660 Registrar_ResourceUnregister,
663 static HRESULT Registrar_create(LPUNKNOWN pUnkOuter, REFIID riid, void **ppvObject)
667 if(!IsEqualGUID(&IID_IUnknown, riid) && !IsEqualGUID(&IID_IRegistrar, riid))
668 return E_NOINTERFACE;
670 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar));
671 ret->lpVtbl = &RegistrarVtbl;
676 InterlockedIncrement(&dll_count);
681 /**************************************************************
682 * ClassFactory implementation
685 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
687 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
689 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
694 return E_NOINTERFACE;
697 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
699 InterlockedIncrement(&dll_count);
703 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
705 InterlockedDecrement(&dll_count);
709 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
710 REFIID riid, void **ppvObject)
712 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
713 return Registrar_create(pUnkOuter, riid, ppvObject);
716 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
718 TRACE("(%p)->(%x)\n", iface, lock);
721 InterlockedIncrement(&dll_count);
723 InterlockedDecrement(&dll_count);
728 static const IClassFactoryVtbl IRegistrarCFVtbl = {
729 RegistrarCF_QueryInterface,
732 RegistrarCF_CreateInstance,
733 RegistrarCF_LockServer
736 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
738 /**************************************************************
739 * DllGetClassObject implementation
741 HRESULT WINAPI ATL_DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
743 TRACE("(%s %s %p)", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
745 if(IsEqualGUID(&CLSID_ATLRegistrar, clsid)) {
746 *ppvObject = &RegistrarCF;
750 FIXME("Not supported class %s\n", debugstr_guid(clsid));
751 return CLASS_E_CLASSNOTAVAILABLE;
754 extern HINSTANCE hInst;
756 static HRESULT do_register_server(BOOL do_register)
760 IRegistrar *pRegistrar;
762 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
763 static const WCHAR wszDll[] = {'a','t','l','.','d','l','l',0};
764 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
765 static const WCHAR wszCLSID_ATLRegistrar[] =
766 {'C','L','S','I','D','_','A','T','L','R','e','g','i','s','t','r','a','r',0};
768 Registrar_create(NULL, &IID_IRegistrar, (void**)&pRegistrar);
769 IRegistrar_AddReplacement(pRegistrar, wszModule, wszDll);
771 StringFromGUID2(&CLSID_ATLRegistrar, buf, sizeof(buf)/sizeof(buf[0]));
772 IRegistrar_AddReplacement(pRegistrar, wszCLSID_ATLRegistrar, buf);
775 hres = IRegistrar_ResourceRegister(pRegistrar, wszDll, 1, wszRegistry);
777 hres = IRegistrar_ResourceUnregister(pRegistrar, wszDll, 1, wszRegistry);
779 IRegistrar_Release(pRegistrar);
783 /***********************************************************************
784 * DllRegisterServer (ATL.@)
786 HRESULT WINAPI ATL_DllRegisterServer(void)
789 return do_register_server(TRUE);
792 /***********************************************************************
793 * DllRegisterServer (ATL.@)
795 HRESULT WINAPI ATL_DllUnregisterServer(void)
798 return do_register_server(FALSE);
801 /***********************************************************************
802 * DllCanUnloadNow (ATL.@)
804 HRESULT WINAPI ATL_DllCanUnloadNow(void)
806 TRACE("dll_count = %lu\n", dll_count);
807 return dll_count ? S_FALSE : S_OK;