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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(atl
);
27 /**************************************************************
28 * ATLRegistrar implementation
35 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
37 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
39 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
41 {{'H','K','E','Y','_','U','S','E','R','S',0},
43 {{'H','K','E','Y','_','P','E','R','F','O','R','M','A','N','C','E','_','D','A','T','A',0},
44 HKEY_PERFORMANCE_DATA
},
45 {{'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0},
47 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
49 {{'H','K','C','R',0}, HKEY_CLASSES_ROOT
},
50 {{'H','K','C','U',0}, HKEY_CURRENT_USER
},
51 {{'H','K','L','M',0}, HKEY_LOCAL_MACHINE
},
52 {{'H','K','U',0}, HKEY_USERS
},
53 {{'H','K','P','D',0}, HKEY_PERFORMANCE_DATA
},
54 {{'H','K','D','D',0}, HKEY_DYN_DATA
},
55 {{'H','K','C','C',0}, HKEY_CURRENT_CONFIG
}
58 typedef struct rep_list_str
{
62 struct rep_list_str
*next
;
66 IRegistrar IRegistrar_iface
;
77 static inline Registrar
*impl_from_IRegistrar(IRegistrar
*iface
)
79 return CONTAINING_RECORD(iface
, Registrar
, IRegistrar_iface
);
82 static void strbuf_init(strbuf
*buf
)
84 buf
->str
= HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR
));
89 static void strbuf_write(LPCOLESTR str
, strbuf
*buf
, int len
)
93 if(buf
->len
+len
+1 >= buf
->alloc
) {
94 buf
->alloc
= (buf
->len
+len
)<<1;
95 buf
->str
= HeapReAlloc(GetProcessHeap(), 0, buf
->str
, buf
->alloc
*sizeof(WCHAR
));
97 memcpy(buf
->str
+buf
->len
, str
, len
*sizeof(OLECHAR
));
99 buf
->str
[buf
->len
] = '\0';
102 static HRESULT
get_word(LPCOLESTR
*str
, strbuf
*buf
)
104 LPCOLESTR iter
, iter2
= *str
;
109 while(iswspace(*iter2
))
117 if(*iter
== '}' || *iter
== '=') {
118 strbuf_write(iter
++, buf
, 1);
119 }else if(*iter
== '\'') {
121 iter
= wcschr(iter
, '\'');
123 WARN("Unexpected end of script\n");
125 return DISP_E_EXCEPTION
;
127 strbuf_write(iter2
, buf
, iter
-iter2
);
130 while(*iter
&& !iswspace(*iter
))
132 strbuf_write(iter2
, buf
, iter
-iter2
);
135 while(iswspace(*iter
))
141 static HRESULT
do_preprocess(const Registrar
*This
, LPCOLESTR data
, strbuf
*buf
)
143 LPCOLESTR iter
, iter2
= data
;
145 static const WCHAR wstr
[] = {'%',0};
147 iter
= wcschr(data
, '%');
149 strbuf_write(iter2
, buf
, iter
-iter2
);
153 return DISP_E_EXCEPTION
;
154 iter
= wcschr(iter2
, '%');
156 return DISP_E_EXCEPTION
;
159 strbuf_write(wstr
, buf
, 1);
161 for(rep_iter
= This
->rep
; rep_iter
; rep_iter
= rep_iter
->next
) {
162 if(rep_iter
->key_len
== iter
-iter2
163 && !wcsnicmp(iter2
, rep_iter
->key
, rep_iter
->key_len
))
167 WARN("Could not find replacement: %s\n", debugstr_wn(iter2
, iter
-iter2
));
168 return DISP_E_EXCEPTION
;
171 strbuf_write(rep_iter
->item
, buf
, -1);
175 iter
= wcschr(iter
, '%');
178 strbuf_write(iter2
, buf
, -1);
179 TRACE("%s\n", debugstr_w(buf
->str
));
184 static HRESULT
do_process_key(LPCOLESTR
*pstr
, HKEY parent_key
, strbuf
*buf
, BOOL do_register
)
200 static const WCHAR wstrNoRemove
[] = {'N','o','R','e','m','o','v','e',0};
201 static const WCHAR wstrForceRemove
[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
202 static const WCHAR wstrDelete
[] = {'D','e','l','e','t','e',0};
203 static const WCHAR wstrval
[] = {'v','a','l',0};
206 hres
= get_word(&iter
, buf
);
211 while(buf
->str
[1] || buf
->str
[0] != '}') {
213 if(!lstrcmpiW(buf
->str
, wstrNoRemove
))
214 key_type
= NO_REMOVE
;
215 else if(!lstrcmpiW(buf
->str
, wstrForceRemove
))
216 key_type
= FORCE_REMOVE
;
217 else if(!lstrcmpiW(buf
->str
, wstrval
))
219 else if(!lstrcmpiW(buf
->str
, wstrDelete
))
220 key_type
= DO_DELETE
;
222 if(key_type
!= NORMAL
) {
223 hres
= get_word(&iter
, buf
);
227 TRACE("name = %s\n", debugstr_w(buf
->str
));
230 if(key_type
== IS_VAL
) {
232 strbuf_write(buf
->str
, &name
, -1);
233 }else if(key_type
== DO_DELETE
) {
234 TRACE("Deleting %s\n", debugstr_w(buf
->str
));
235 RegDeleteTreeW(parent_key
, buf
->str
);
237 if(key_type
== FORCE_REMOVE
)
238 RegDeleteTreeW(parent_key
, buf
->str
);
239 lres
= RegCreateKeyW(parent_key
, buf
->str
, &hkey
);
240 if(lres
!= ERROR_SUCCESS
) {
241 WARN("Could not create(open) key: %08x\n", lres
);
242 hres
= HRESULT_FROM_WIN32(lres
);
246 }else if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
) {
247 strbuf_write(buf
->str
, &name
, -1);
248 lres
= RegOpenKeyW(parent_key
, buf
->str
, &hkey
);
249 if(lres
!= ERROR_SUCCESS
)
250 WARN("Could not open key %s: %08x\n", debugstr_w(name
.str
), lres
);
253 if(key_type
!= DO_DELETE
&& *iter
== '=') {
255 hres
= get_word(&iter
, buf
);
259 WARN("Wrong registry type: %s\n", debugstr_w(buf
->str
));
260 hres
= DISP_E_EXCEPTION
;
264 switch(buf
->str
[0]) {
266 hres
= get_word(&iter
, buf
);
269 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_SZ
, (PBYTE
)buf
->str
,
270 (lstrlenW(buf
->str
)+1)*sizeof(WCHAR
));
271 if(lres
!= ERROR_SUCCESS
) {
272 WARN("Could set value of key: %08x\n", lres
);
273 hres
= HRESULT_FROM_WIN32(lres
);
279 hres
= get_word(&iter
, buf
);
282 dw
= wcstol(buf
->str
, NULL
, 10);
283 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_DWORD
,
284 (PBYTE
)&dw
, sizeof(dw
));
285 if(lres
!= ERROR_SUCCESS
) {
286 WARN("Could set value of key: %08x\n", lres
);
287 hres
= HRESULT_FROM_WIN32(lres
);
296 hres
= get_word(&iter
, buf
);
299 count
= (lstrlenW(buf
->str
) + 1) / 2;
300 bytes
= HeapAlloc(GetProcessHeap(), 0, count
);
302 hres
= E_OUTOFMEMORY
;
305 for(i
= 0; i
< count
&& buf
->str
[2*i
]; i
++) {
307 if(!iswxdigit(buf
->str
[2*i
]) || !iswxdigit(buf
->str
[2*i
+ 1])) {
311 digits
[0] = buf
->str
[2*i
];
312 digits
[1] = buf
->str
[2*i
+ 1];
314 bytes
[i
] = (BYTE
) wcstoul(digits
, NULL
, 16);
316 if(SUCCEEDED(hres
)) {
317 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_BINARY
,
319 if(lres
!= ERROR_SUCCESS
) {
320 WARN("Could not set value of key: 0x%08x\n", lres
);
321 hres
= HRESULT_FROM_WIN32(lres
);
324 HeapFree(GetProcessHeap(), 0, bytes
);
328 WARN("Wrong resource type: %s\n", debugstr_w(buf
->str
));
329 hres
= DISP_E_EXCEPTION
;
336 hres
= get_word(&iter
, buf
);
340 }else if(key_type
== IS_VAL
) {
341 WARN("value not set!\n");
342 hres
= DISP_E_EXCEPTION
;
346 if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
&& *iter
== '{' && iswspace(iter
[1])) {
347 hres
= get_word(&iter
, buf
);
350 hres
= do_process_key(&iter
, hkey
, buf
, do_register
);
355 TRACE("%x %x\n", do_register
, key_type
);
356 if(!do_register
&& (key_type
== NORMAL
|| key_type
== FORCE_REMOVE
)) {
357 TRACE("Deleting %s\n", debugstr_w(name
.str
));
358 RegDeleteKeyW(parent_key
, name
.str
);
361 if(hkey
&& key_type
!= IS_VAL
)
366 hres
= get_word(&iter
, buf
);
371 HeapFree(GetProcessHeap(), 0, name
.str
);
372 if(hkey
&& key_type
!= IS_VAL
)
378 static HRESULT
do_process_root_key(LPCOLESTR data
, BOOL do_register
)
380 LPCOLESTR iter
= data
;
386 hres
= get_word(&iter
, &buf
);
392 WARN("ward.len == 0, failed\n");
393 hres
= DISP_E_EXCEPTION
;
396 for(i
=0; i
<ARRAY_SIZE(root_keys
); i
++) {
397 if(!lstrcmpiW(buf
.str
, root_keys
[i
].name
))
400 if(i
== ARRAY_SIZE(root_keys
)) {
401 WARN("Wrong root key name: %s\n", debugstr_w(buf
.str
));
402 hres
= DISP_E_EXCEPTION
;
405 hres
= get_word(&iter
, &buf
);
408 if(buf
.str
[1] || buf
.str
[0] != '{') {
409 WARN("Failed, expected '{', got %s\n", debugstr_w(buf
.str
));
410 hres
= DISP_E_EXCEPTION
;
413 hres
= do_process_key(&iter
, root_keys
[i
].key
, &buf
, do_register
);
415 WARN("Processing key failed: %08x\n", hres
);
418 hres
= get_word(&iter
, &buf
);
422 HeapFree(GetProcessHeap(), 0, buf
.str
);
426 static HRESULT
string_register(Registrar
*This
, LPCOLESTR data
, BOOL do_register
)
431 TRACE("(%p %s %x)\n", This
, debugstr_w(data
), do_register
);
434 hres
= do_preprocess(This
, data
, &buf
);
436 WARN("preprocessing failed!\n");
437 HeapFree(GetProcessHeap(), 0, buf
.str
);
441 hres
= do_process_root_key(buf
.str
, do_register
);
442 if(FAILED(hres
) && do_register
)
443 do_process_root_key(buf
.str
, FALSE
);
445 HeapFree(GetProcessHeap(), 0, buf
.str
);
449 static HRESULT
resource_register(Registrar
*This
, LPCOLESTR resFileName
,
450 LPCOLESTR szID
, LPCOLESTR szType
, BOOL do_register
)
459 hins
= LoadLibraryExW(resFileName
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
461 src
= FindResourceW(hins
, szID
, szType
);
463 regstra
= LoadResource(hins
, src
);
464 reslen
= SizeofResource(hins
, src
);
466 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, NULL
, 0)+1;
467 regstrw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
468 MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, regstrw
, len
);
469 regstrw
[len
-1] = '\0';
471 hres
= string_register(This
, regstrw
, do_register
);
473 HeapFree(GetProcessHeap(), 0, regstrw
);
475 WARN("could not load resource\n");
476 hres
= HRESULT_FROM_WIN32(GetLastError());
479 WARN("Could not find source\n");
480 hres
= HRESULT_FROM_WIN32(GetLastError());
484 WARN("Could not load resource file\n");
485 hres
= HRESULT_FROM_WIN32(GetLastError());
491 static HRESULT
file_register(Registrar
*This
, LPCOLESTR fileName
, BOOL do_register
)
499 file
= CreateFileW(fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
500 if(file
!= INVALID_HANDLE_VALUE
) {
501 filelen
= GetFileSize(file
, NULL
);
502 regstra
= HeapAlloc(GetProcessHeap(), 0, filelen
);
503 if(ReadFile(file
, regstra
, filelen
, NULL
, NULL
)) {
504 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, NULL
, 0)+1;
505 regstrw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
506 MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, regstrw
, len
);
507 regstrw
[len
-1] = '\0';
509 hres
= string_register(This
, regstrw
, do_register
);
511 HeapFree(GetProcessHeap(), 0, regstrw
);
513 WARN("Failed to read file %s\n", debugstr_w(fileName
));
514 hres
= HRESULT_FROM_WIN32(GetLastError());
516 HeapFree(GetProcessHeap(), 0, regstra
);
519 WARN("Could not open file %s\n", debugstr_w(fileName
));
520 hres
= HRESULT_FROM_WIN32(GetLastError());
526 static HRESULT WINAPI
Registrar_QueryInterface(IRegistrar
*iface
, REFIID riid
, void **ppvObject
)
528 TRACE("(%p)->(%s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
530 if(IsEqualGUID(&IID_IUnknown
, riid
)
531 || IsEqualGUID(&IID_IRegistrar
, riid
)
532 || IsEqualGUID(&IID_IRegistrarBase
, riid
)) {
533 IRegistrar_AddRef(iface
);
537 return E_NOINTERFACE
;
540 static ULONG WINAPI
Registrar_AddRef(IRegistrar
*iface
)
542 Registrar
*This
= impl_from_IRegistrar(iface
);
543 ULONG ref
= InterlockedIncrement(&This
->ref
);
544 TRACE("(%p) ->%d\n", This
, ref
);
548 static ULONG WINAPI
Registrar_Release(IRegistrar
*iface
)
550 Registrar
*This
= impl_from_IRegistrar(iface
);
551 ULONG ref
= InterlockedDecrement(&This
->ref
);
553 TRACE("(%p) ->%d\n", This
, ref
);
555 IRegistrar_ClearReplacements(iface
);
556 HeapFree(GetProcessHeap(), 0, This
);
561 static HRESULT WINAPI
Registrar_AddReplacement(IRegistrar
*iface
, LPCOLESTR Key
, LPCOLESTR item
)
563 Registrar
*This
= impl_from_IRegistrar(iface
);
567 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(Key
), debugstr_w(item
));
569 new_rep
= HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list
));
571 new_rep
->key_len
= lstrlenW(Key
);
572 new_rep
->key
= HeapAlloc(GetProcessHeap(), 0, (new_rep
->key_len
+ 1) * sizeof(OLECHAR
));
573 memcpy(new_rep
->key
, Key
, (new_rep
->key_len
+1)*sizeof(OLECHAR
));
575 len
= lstrlenW(item
)+1;
576 new_rep
->item
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(OLECHAR
));
577 memcpy(new_rep
->item
, item
, len
*sizeof(OLECHAR
));
579 new_rep
->next
= This
->rep
;
585 static HRESULT WINAPI
Registrar_ClearReplacements(IRegistrar
*iface
)
587 Registrar
*This
= impl_from_IRegistrar(iface
);
588 rep_list
*iter
, *iter2
;
590 TRACE("(%p)\n", This
);
598 HeapFree(GetProcessHeap(), 0, iter
->key
);
599 HeapFree(GetProcessHeap(), 0, iter
->item
);
600 HeapFree(GetProcessHeap(), 0, iter
);
608 static HRESULT WINAPI
Registrar_ResourceRegisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
609 LPCOLESTR szID
, LPCOLESTR szType
)
611 Registrar
*This
= impl_from_IRegistrar(iface
);
612 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
613 return resource_register(This
, resFileName
, szID
, szType
, TRUE
);
616 static HRESULT WINAPI
Registrar_ResourceUnregisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
617 LPCOLESTR szID
, LPCOLESTR szType
)
619 Registrar
*This
= impl_from_IRegistrar(iface
);
620 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
621 return resource_register(This
, resFileName
, szID
, szType
, FALSE
);
624 static HRESULT WINAPI
Registrar_FileRegister(IRegistrar
* iface
, LPCOLESTR fileName
)
626 Registrar
*This
= impl_from_IRegistrar(iface
);
627 TRACE("(%p)->(%s)\n", This
, debugstr_w(fileName
));
628 return file_register(This
, fileName
, TRUE
);
631 static HRESULT WINAPI
Registrar_FileUnregister(IRegistrar
* iface
, LPCOLESTR fileName
)
633 Registrar
*This
= impl_from_IRegistrar(iface
);
634 FIXME("(%p)->(%s)\n", This
, debugstr_w(fileName
));
635 return file_register(This
, fileName
, FALSE
);
638 static HRESULT WINAPI
Registrar_StringRegister(IRegistrar
* iface
, LPCOLESTR data
)
640 Registrar
*This
= impl_from_IRegistrar(iface
);
641 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
642 return string_register(This
, data
, TRUE
);
645 static HRESULT WINAPI
Registrar_StringUnregister(IRegistrar
* iface
, LPCOLESTR data
)
647 Registrar
*This
= impl_from_IRegistrar(iface
);
648 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
649 return string_register(This
, data
, FALSE
);
652 static HRESULT WINAPI
Registrar_ResourceRegister(IRegistrar
* iface
, LPCOLESTR resFileName
,
653 UINT nID
, LPCOLESTR szType
)
655 Registrar
*This
= impl_from_IRegistrar(iface
);
656 TRACE("(%p)->(%s %d %s)\n", iface
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
657 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, TRUE
);
660 static HRESULT WINAPI
Registrar_ResourceUnregister(IRegistrar
* iface
, LPCOLESTR resFileName
,
661 UINT nID
, LPCOLESTR szType
)
663 Registrar
*This
= impl_from_IRegistrar(iface
);
664 TRACE("(%p)->(%s %d %s)\n", This
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
665 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, FALSE
);
668 static const IRegistrarVtbl RegistrarVtbl
= {
669 Registrar_QueryInterface
,
672 Registrar_AddReplacement
,
673 Registrar_ClearReplacements
,
674 Registrar_ResourceRegisterSz
,
675 Registrar_ResourceUnregisterSz
,
676 Registrar_FileRegister
,
677 Registrar_FileUnregister
,
678 Registrar_StringRegister
,
679 Registrar_StringUnregister
,
680 Registrar_ResourceRegister
,
681 Registrar_ResourceUnregister
,
684 /***********************************************************************
685 * AtlCreateRegistrar [atl100.@]
687 HRESULT WINAPI
AtlCreateRegistrar(IRegistrar
**ret
)
689 Registrar
*registrar
;
691 registrar
= HeapAlloc(GetProcessHeap(), 0, sizeof(*registrar
));
693 return E_OUTOFMEMORY
;
695 registrar
->IRegistrar_iface
.lpVtbl
= &RegistrarVtbl
;
697 registrar
->rep
= NULL
;
699 *ret
= ®istrar
->IRegistrar_iface
;
703 /***********************************************************************
704 * AtlUpdateRegistryFromResourceD [atl100.@]
706 HRESULT WINAPI
AtlUpdateRegistryFromResourceD(HINSTANCE inst
, LPCOLESTR res
,
707 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
*pMapEntries
, IRegistrar
*pReg
)
709 const struct _ATL_REGMAP_ENTRY
*iter
;
710 WCHAR module_name
[MAX_PATH
];
711 IRegistrar
*registrar
;
714 static const WCHAR moduleW
[] = {'M','O','D','U','L','E',0};
715 static const WCHAR registryW
[] = {'R','E','G','I','S','T','R','Y',0};
717 if(!GetModuleFileNameW(inst
, module_name
, MAX_PATH
)) {
718 FIXME("hinst %p: did not get module name\n", inst
);
722 TRACE("%p (%s), %s, %d, %p, %p\n", inst
, debugstr_w(module_name
),
723 debugstr_w(res
), bRegister
, pMapEntries
, pReg
);
728 hres
= AtlCreateRegistrar(®istrar
);
733 IRegistrar_AddReplacement(registrar
, moduleW
, module_name
);
735 for (iter
= pMapEntries
; iter
&& iter
->szKey
; iter
++)
736 IRegistrar_AddReplacement(registrar
, iter
->szKey
, iter
->szData
);
739 hres
= IRegistrar_ResourceRegisterSz(registrar
, module_name
, res
, registryW
);
741 hres
= IRegistrar_ResourceUnregisterSz(registrar
, module_name
, res
, registryW
);
743 if(registrar
!= pReg
)
744 IRegistrar_Release(registrar
);