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
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(atl);
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: %08lx\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: %08lx\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: %08lx\n", lres);
284 hres = HRESULT_FROM_WIN32(lres);
290 if(*iter == '0' && iter[1] == 'x') {
292 dw = strtolW(iter, (WCHAR**)&iter, 16);
294 dw = strtolW(iter, (WCHAR**)&iter, 10);
296 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_DWORD,
297 (PBYTE)&dw, sizeof(dw));
298 if(lres != ERROR_SUCCESS) {
299 WARN("Could set value of key: %08lx\n", lres);
300 hres = HRESULT_FROM_WIN32(lres);
306 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
307 hres = DISP_E_EXCEPTION;
314 hres = get_word(&iter, buf);
318 }else if(key_type == IS_VAL) {
319 WARN("value not set!\n");
320 hres = DISP_E_EXCEPTION;
324 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
325 hres = get_word(&iter, buf);
328 hres = do_process_key(&iter, hkey, buf, do_register);
333 TRACE("%x %x\n", do_register, key_type);
334 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
335 TRACE("Deleting %s\n", debugstr_w(name.str));
336 RegDeleteKeyW(parent_key, name.str);
339 if(hkey && key_type != IS_VAL)
344 hres = get_word(&iter, buf);
349 HeapFree(GetProcessHeap(), 0, name.str);
350 if(hkey && key_type != IS_VAL)
356 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
358 LPCOLESTR iter = data;
364 hres = get_word(&iter, &buf);
370 WARN("ward.len == 0, failed\n");
371 hres = DISP_E_EXCEPTION;
374 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
375 if(!lstrcmpiW(buf.str, root_keys[i].name))
378 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
379 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
380 hres = DISP_E_EXCEPTION;
383 hres = get_word(&iter, &buf);
386 if(buf.str[1] || buf.str[0] != '{') {
387 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
388 hres = DISP_E_EXCEPTION;
391 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
393 WARN("Processing key failed: %08lx\n", hres);
396 hres = get_word(&iter, &buf);
400 HeapFree(GetProcessHeap(), 0, buf.str);
404 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
409 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
412 hres = do_preprocess(This, data, &buf);
414 WARN("preprocessing failed!\n");
415 HeapFree(GetProcessHeap(), 0, buf.str);
419 hres = do_process_root_key(buf.str, do_register);
420 if(FAILED(hres) && do_register)
421 do_process_root_key(buf.str, FALSE);
423 HeapFree(GetProcessHeap(), 0, buf.str);
427 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
428 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
437 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
439 src = FindResourceW(hins, szID, szType);
441 regstra = (LPSTR)LoadResource(hins, src);
442 reslen = SizeofResource(hins, src);
444 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
445 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
446 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, -1);
447 regstrw[len-1] = '\0';
449 hres = string_register(This, regstrw, do_register);
451 HeapFree(GetProcessHeap(), 0, regstrw);
453 WARN("could not load resource\n");
454 hres = HRESULT_FROM_WIN32(GetLastError());
456 HeapFree(GetProcessHeap(), 0, regstra);
458 WARN("Could not find source\n");
459 hres = HRESULT_FROM_WIN32(GetLastError());
463 WARN("Could not load resource file\n");
464 hres = HRESULT_FROM_WIN32(GetLastError());
470 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
479 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
481 filelen = GetFileSize(file, NULL);
482 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
483 lres = ReadFile(file, regstra, filelen, NULL, NULL);
484 if(lres == ERROR_SUCCESS) {
485 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
486 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
487 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, -1);
488 regstrw[len-1] = '\0';
490 hres = string_register(This, regstrw, do_register);
492 HeapFree(GetProcessHeap(), 0, regstrw);
494 WARN("Failed to read faile\n");
495 hres = HRESULT_FROM_WIN32(lres);
497 HeapFree(GetProcessHeap(), 0, regstra);
500 WARN("Could not open file\n");
501 hres = HRESULT_FROM_WIN32(GetLastError());
507 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
509 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
511 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
512 IRegistrar_AddRef(iface);
516 return E_NOINTERFACE;
519 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
521 Registrar *This = (Registrar*)iface;
522 ULONG ref = InterlockedIncrement(&This->ref);
523 TRACE("(%p) ->%ld\n", This, ref);
527 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
529 Registrar *This = (Registrar*)iface;
530 ULONG ref = InterlockedDecrement(&This->ref);
532 TRACE("(%p) ->%ld\n", This, ref);
534 IRegistrar_ClearReplacements(iface);
535 HeapFree(GetProcessHeap(), 0, This);
536 InterlockedDecrement(&dll_count);
541 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
543 Registrar *This = (Registrar*)iface;
547 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
549 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
551 new_rep->key_len = lstrlenW(Key);
552 new_rep->key = HeapAlloc(GetProcessHeap(), 0, new_rep->key_len*sizeof(OLECHAR)+1);
553 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
555 len = lstrlenW(item)+1;
556 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
557 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
559 new_rep->next = This->rep;
565 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
567 Registrar *This = (Registrar*)iface;
568 rep_list *iter, *iter2;
570 TRACE("(%p)\n", This);
578 HeapFree(GetProcessHeap(), 0, iter->key);
579 HeapFree(GetProcessHeap(), 0, iter->item);
580 HeapFree(GetProcessHeap(), 0, iter);
588 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
589 LPCOLESTR szID, LPCOLESTR szType)
591 Registrar *This = (Registrar*)iface;
592 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
593 return resource_register(This, resFileName, szID, szType, TRUE);
596 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
597 LPCOLESTR szID, LPCOLESTR szType)
599 Registrar *This = (Registrar*)iface;
600 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
601 return resource_register(This, resFileName, szID, szType, FALSE);
604 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
606 Registrar *This = (Registrar*)iface;
607 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
608 return file_register(This, fileName, TRUE);
611 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
613 Registrar *This = (Registrar*)iface;
614 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
615 return file_register(This, fileName, FALSE);
618 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
620 Registrar *This = (Registrar*)iface;
621 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
622 return string_register(This, data, TRUE);
625 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
627 Registrar *This = (Registrar*)iface;
628 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
629 return string_register(This, data, FALSE);
632 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
633 UINT nID, LPCOLESTR szType)
635 Registrar *This = (Registrar*)iface;
636 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
637 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, TRUE);
640 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
641 UINT nID, LPCOLESTR szType)
643 Registrar *This = (Registrar*)iface;
644 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
645 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, FALSE);
648 static const IRegistrarVtbl RegistrarVtbl = {
649 Registrar_QueryInterface,
652 Registrar_AddReplacement,
653 Registrar_ClearReplacements,
654 Registrar_ResourceRegisterSz,
655 Registrar_ResourceUnregisterSz,
656 Registrar_FileRegister,
657 Registrar_FileUnregister,
658 Registrar_StringRegister,
659 Registrar_StringUnregister,
660 Registrar_ResourceRegister,
661 Registrar_ResourceUnregister,
664 static HRESULT Registrar_create(LPUNKNOWN pUnkOuter, REFIID riid, void **ppvObject)
668 if(!IsEqualGUID(&IID_IUnknown, riid) && !IsEqualGUID(&IID_IRegistrar, riid))
669 return E_NOINTERFACE;
671 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar));
672 ret->lpVtbl = &RegistrarVtbl;
677 InterlockedIncrement(&dll_count);
682 /**************************************************************
683 * ClassFactory implementation
686 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
688 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
690 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
695 return E_NOINTERFACE;
698 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
700 InterlockedIncrement(&dll_count);
704 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
706 InterlockedDecrement(&dll_count);
710 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
711 REFIID riid, void **ppvObject)
713 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
714 return Registrar_create(pUnkOuter, riid, ppvObject);
717 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
719 TRACE("(%p)->(%x)\n", iface, lock);
722 InterlockedIncrement(&dll_count);
724 InterlockedDecrement(&dll_count);
729 static const IClassFactoryVtbl IRegistrarCFVtbl = {
730 RegistrarCF_QueryInterface,
733 RegistrarCF_CreateInstance,
734 RegistrarCF_LockServer
737 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
739 /**************************************************************
740 * DllGetClassObject (ATL.2)
742 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
744 TRACE("(%s %s %p)", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
746 if(IsEqualGUID(&CLSID_ATLRegistrar, clsid)) {
747 *ppvObject = &RegistrarCF;
751 FIXME("Not supported class %s\n", debugstr_guid(clsid));
752 return CLASS_E_CLASSNOTAVAILABLE;
755 extern HINSTANCE hInst;
757 static HRESULT do_register_dll_server(LPCOLESTR wszDll, LPCOLESTR wszId, BOOL do_register)
761 IRegistrar *pRegistrar;
763 static const WCHAR wszModule[] = {'M','O','D','U','L','E',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_ResourceRegisterSz(pRegistrar, wszDll, wszId, wszRegistry);
777 hres = IRegistrar_ResourceUnregisterSz(pRegistrar, wszDll, wszId, wszRegistry);
779 IRegistrar_Release(pRegistrar);
783 static HRESULT do_register_server(BOOL do_register)
785 static const WCHAR wszDll[] = {'a','t','l','.','d','l','l',0};
786 return do_register_dll_server(wszDll, MAKEINTRESOURCEW(101), do_register);
789 /***********************************************************************
790 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
793 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
794 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
796 HINSTANCE lhInst = pM->m_hInst;
797 /* everything inside this function below this point
798 * should go into atl71.AtlUpdateRegistryFromResourceD
800 WCHAR module_name[MAX_PATH];
802 if(pMapEntries || pReg) {
803 FIXME("MapEntries and Registrar parameter not supported\n");
807 if(!GetModuleFileNameW(lhInst, module_name, MAX_PATH)) {
808 FIXME("hinst %p: did not get module name\n",
813 TRACE("%p (%s), %s, %d, %p, %p\n", hInst, debugstr_w(module_name),
814 debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
816 return do_register_dll_server(module_name, lpszRes, bRegister);
819 /***********************************************************************
820 * DllRegisterServer (ATL.@)
822 HRESULT WINAPI DllRegisterServer(void)
825 return do_register_server(TRUE);
828 /***********************************************************************
829 * DllRegisterServer (ATL.@)
831 HRESULT WINAPI DllUnregisterServer(void)
834 return do_register_server(FALSE);
837 /***********************************************************************
838 * DllCanUnloadNow (ATL.@)
840 HRESULT WINAPI DllCanUnloadNow(void)
842 TRACE("dll_count = %lu\n", dll_count);
843 return dll_count ? S_FALSE : S_OK;