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
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
51 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
53 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
55 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
57 {{'H','K','E','Y','_','U','S','E','R','S',0},
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},
63 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
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 {
78 struct rep_list_str *next;
82 const IRegistrarVtbl *lpVtbl;
93 static void strbuf_init(strbuf *buf)
95 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
100 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
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));
110 buf->str[buf->len] = '\0';
113 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
115 LPCOLESTR iter, iter2 = *str;
120 while(isspaceW(*iter2))
128 if(*iter == '}' || *iter == '=') {
129 strbuf_write(iter++, buf, 1);
130 }else if(*iter == '\'') {
132 iter = strchrW(iter, '\'');
134 WARN("Unexpected end of script\n");
136 return DISP_E_EXCEPTION;
138 strbuf_write(iter2, buf, iter-iter2);
141 while(*iter && !isspaceW(*iter))
143 strbuf_write(iter2, buf, iter-iter2);
146 while(isspaceW(*iter))
152 static HRESULT do_preprocess(Registrar *This, LPCOLESTR data, strbuf *buf)
154 LPCOLESTR iter, iter2 = data;
156 static const WCHAR wstr[] = {'%',0};
158 iter = strchrW(data, '%');
160 strbuf_write(iter2, buf, iter-iter2);
164 return DISP_E_EXCEPTION;
165 iter = strchrW(iter2, '%');
167 return DISP_E_EXCEPTION;
170 strbuf_write(wstr, buf, 1);
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))
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);
186 iter = strchrW(iter, '%');
189 strbuf_write(iter2, buf, -1);
190 TRACE("%s\n", debugstr_w(buf->str));
195 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
197 LPCOLESTR iter = *pstr;
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};
217 hres = get_word(&iter, buf);
222 while(buf->str[1] || buf->str[0] != '}') {
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))
230 else if(!lstrcmpiW(buf->str, wstrDelete))
231 key_type = DO_DELETE;
233 if(key_type != NORMAL) {
234 hres = get_word(&iter, buf);
238 TRACE("name = %s\n", debugstr_w(buf->str));
241 if(key_type == IS_VAL) {
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);
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);
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 == '=') {
266 hres = get_word(&iter, buf);
270 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
271 hres = DISP_E_EXCEPTION;
275 switch(buf->str[0]) {
277 hres = get_word(&iter, buf);
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);
291 if(*iter == '0' && iter[1] == 'x') {
293 dw = strtolW(iter, &end, 16);
295 dw = strtolW(iter, &end, 10);
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);
308 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
309 hres = DISP_E_EXCEPTION;
316 hres = get_word(&iter, buf);
320 }else if(key_type == IS_VAL) {
321 WARN("value not set!\n");
322 hres = DISP_E_EXCEPTION;
326 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
327 hres = get_word(&iter, buf);
330 hres = do_process_key(&iter, hkey, buf, do_register);
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)
346 hres = get_word(&iter, buf);
351 HeapFree(GetProcessHeap(), 0, name.str);
352 if(hkey && key_type != IS_VAL)
358 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
360 LPCOLESTR iter = data;
366 hres = get_word(&iter, &buf);
372 WARN("ward.len == 0, failed\n");
373 hres = DISP_E_EXCEPTION;
376 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
377 if(!lstrcmpiW(buf.str, root_keys[i].name))
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;
385 hres = get_word(&iter, &buf);
388 if(buf.str[1] || buf.str[0] != '{') {
389 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
390 hres = DISP_E_EXCEPTION;
393 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
395 WARN("Processing key failed: %08x\n", hres);
398 hres = get_word(&iter, &buf);
402 HeapFree(GetProcessHeap(), 0, buf.str);
406 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
411 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
414 hres = do_preprocess(This, data, &buf);
416 WARN("preprocessing failed!\n");
417 HeapFree(GetProcessHeap(), 0, buf.str);
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);
429 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
430 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
439 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
441 src = FindResourceW(hins, szID, szType);
443 regstra = (LPSTR)LoadResource(hins, src);
444 reslen = SizeofResource(hins, src);
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);
455 WARN("could not load resource\n");
456 hres = HRESULT_FROM_WIN32(GetLastError());
458 HeapFree(GetProcessHeap(), 0, regstra);
460 WARN("Could not find source\n");
461 hres = HRESULT_FROM_WIN32(GetLastError());
465 WARN("Could not load resource file\n");
466 hres = HRESULT_FROM_WIN32(GetLastError());
472 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
481 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
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);
496 WARN("Failed to read faile\n");
497 hres = HRESULT_FROM_WIN32(lres);
499 HeapFree(GetProcessHeap(), 0, regstra);
502 WARN("Could not open file\n");
503 hres = HRESULT_FROM_WIN32(GetLastError());
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);
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);
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);
536 IRegistrar_ClearReplacements(iface);
537 HeapFree(GetProcessHeap(), 0, This);
538 InterlockedDecrement(&dll_count);
543 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
545 Registrar *This = (Registrar*)iface;
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;
567 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
569 Registrar *This = (Registrar*)iface;
570 rep_list *iter, *iter2;
572 TRACE("(%p)\n", This);
580 HeapFree(GetProcessHeap(), 0, iter->key);
581 HeapFree(GetProcessHeap(), 0, iter->item);
582 HeapFree(GetProcessHeap(), 0, iter);
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,
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(LPUNKNOWN pUnkOuter, REFIID riid, void **ppvObject)
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;
679 InterlockedIncrement(&dll_count);
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)) {
697 return E_NOINTERFACE;
700 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
702 InterlockedIncrement(&dll_count);
706 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
708 InterlockedDecrement(&dll_count);
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);
724 InterlockedIncrement(&dll_count);
726 InterlockedDecrement(&dll_count);
731 static const IClassFactoryVtbl IRegistrarCFVtbl = {
732 RegistrarCF_QueryInterface,
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;
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(LPCOLESTR wszDll, LPCOLESTR wszId, BOOL do_register)
763 IRegistrar *pRegistrar;
765 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
766 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
767 static const WCHAR wszCLSID_ATLRegistrar[] =
768 {'C','L','S','I','D','_','A','T','L','R','e','g','i','s','t','r','a','r',0};
770 Registrar_create(NULL, &IID_IRegistrar, (void**)&pRegistrar);
771 IRegistrar_AddReplacement(pRegistrar, wszModule, wszDll);
773 StringFromGUID2(&CLSID_ATLRegistrar, buf, sizeof(buf)/sizeof(buf[0]));
774 IRegistrar_AddReplacement(pRegistrar, wszCLSID_ATLRegistrar, buf);
777 hres = IRegistrar_ResourceRegisterSz(pRegistrar, wszDll, wszId, wszRegistry);
779 hres = IRegistrar_ResourceUnregisterSz(pRegistrar, wszDll, wszId, wszRegistry);
781 IRegistrar_Release(pRegistrar);
785 static HRESULT do_register_server(BOOL do_register)
787 static const WCHAR wszDll[] = {'a','t','l','.','d','l','l',0};
788 return do_register_dll_server(wszDll, MAKEINTRESOURCEW(101), do_register);
791 /***********************************************************************
792 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
795 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
796 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
798 HINSTANCE lhInst = pM->m_hInst;
799 /* everything inside this function below this point
800 * should go into atl71.AtlUpdateRegistryFromResourceD
802 WCHAR module_name[MAX_PATH];
804 if(pMapEntries || pReg) {
805 FIXME("MapEntries and Registrar parameter not supported\n");
809 if(!GetModuleFileNameW(lhInst, module_name, MAX_PATH)) {
810 FIXME("hinst %p: did not get module name\n",
815 TRACE("%p (%s), %s, %d, %p, %p\n", hInst, debugstr_w(module_name),
816 debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
818 return do_register_dll_server(module_name, lpszRes, bRegister);
821 /***********************************************************************
822 * DllRegisterServer (ATL.@)
824 HRESULT WINAPI DllRegisterServer(void)
827 return do_register_server(TRUE);
830 /***********************************************************************
831 * DllRegisterServer (ATL.@)
833 HRESULT WINAPI DllUnregisterServer(void)
836 return do_register_server(FALSE);
839 /***********************************************************************
840 * DllCanUnloadNow (ATL.@)
842 HRESULT WINAPI DllCanUnloadNow(void)
844 TRACE("dll_count = %u\n", dll_count);
845 return dll_count ? S_FALSE : S_OK;