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 {L
"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT
},
36 {L
"HKEY_CURRENT_USER", HKEY_CURRENT_USER
},
37 {L
"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE
},
38 {L
"HKEY_USERS", HKEY_USERS
},
39 {L
"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA
},
40 {L
"HKEY_DYN_DATA", HKEY_DYN_DATA
},
41 {L
"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG
},
42 {L
"HKCR", HKEY_CLASSES_ROOT
},
43 {L
"HKCU", HKEY_CURRENT_USER
},
44 {L
"HKLM", HKEY_LOCAL_MACHINE
},
46 {L
"HKPD", HKEY_PERFORMANCE_DATA
},
47 {L
"HKDD", HKEY_DYN_DATA
},
48 {L
"HKCC", HKEY_CURRENT_CONFIG
}
51 typedef struct rep_list_str
{
55 struct rep_list_str
*next
;
59 IRegistrar IRegistrar_iface
;
70 static inline Registrar
*impl_from_IRegistrar(IRegistrar
*iface
)
72 return CONTAINING_RECORD(iface
, Registrar
, IRegistrar_iface
);
75 static void strbuf_init(strbuf
*buf
)
77 buf
->str
= HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR
));
82 static void strbuf_write(LPCOLESTR str
, strbuf
*buf
, int len
)
86 if(buf
->len
+len
+1 >= buf
->alloc
) {
87 buf
->alloc
= (buf
->len
+len
)<<1;
88 buf
->str
= HeapReAlloc(GetProcessHeap(), 0, buf
->str
, buf
->alloc
*sizeof(WCHAR
));
90 memcpy(buf
->str
+buf
->len
, str
, len
*sizeof(OLECHAR
));
92 buf
->str
[buf
->len
] = '\0';
95 static int xdigit_to_int(WCHAR c
)
97 if('0' <= c
&& c
<= '9') return c
- '0';
98 if('a' <= c
&& c
<= 'f') return c
- 'a' + 10;
99 if('A' <= c
&& c
<= 'F') return c
- 'A' + 10;
103 static HRESULT
get_word(LPCOLESTR
*str
, strbuf
*buf
)
105 LPCOLESTR iter
, iter2
= *str
;
110 while(iswspace(*iter2
))
118 if(*iter
== '}' || *iter
== '=') {
119 strbuf_write(iter
++, buf
, 1);
120 }else if(*iter
== '\'') {
122 iter
= wcschr(iter
, '\'');
124 WARN("Unexpected end of script\n");
126 return DISP_E_EXCEPTION
;
128 strbuf_write(iter2
, buf
, iter
-iter2
);
131 while(*iter
&& !iswspace(*iter
))
133 strbuf_write(iter2
, buf
, iter
-iter2
);
136 while(iswspace(*iter
))
142 static HRESULT
do_preprocess(const Registrar
*This
, LPCOLESTR data
, strbuf
*buf
)
144 LPCOLESTR iter
, iter2
= data
;
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(L
"%", 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
)
201 hres
= get_word(&iter
, buf
);
206 while(buf
->str
[1] || buf
->str
[0] != '}') {
208 if(!lstrcmpiW(buf
->str
, L
"NoRemove"))
209 key_type
= NO_REMOVE
;
210 else if(!lstrcmpiW(buf
->str
, L
"ForceRemove"))
211 key_type
= FORCE_REMOVE
;
212 else if(!lstrcmpiW(buf
->str
, L
"val"))
214 else if(!lstrcmpiW(buf
->str
, L
"Delete"))
215 key_type
= DO_DELETE
;
217 if(key_type
!= NORMAL
) {
218 hres
= get_word(&iter
, buf
);
222 TRACE("name = %s\n", debugstr_w(buf
->str
));
225 if(key_type
== IS_VAL
) {
227 strbuf_write(buf
->str
, &name
, -1);
228 }else if(key_type
== DO_DELETE
) {
229 TRACE("Deleting %s\n", debugstr_w(buf
->str
));
230 RegDeleteTreeW(parent_key
, buf
->str
);
232 if(key_type
== FORCE_REMOVE
)
233 RegDeleteTreeW(parent_key
, buf
->str
);
234 lres
= RegCreateKeyW(parent_key
, buf
->str
, &hkey
);
235 if(lres
!= ERROR_SUCCESS
) {
236 WARN("Could not create(open) key: %08x\n", lres
);
237 hres
= HRESULT_FROM_WIN32(lres
);
241 }else if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
) {
242 strbuf_write(buf
->str
, &name
, -1);
243 lres
= RegOpenKeyW(parent_key
, buf
->str
, &hkey
);
244 if(lres
!= ERROR_SUCCESS
)
245 WARN("Could not open key %s: %08x\n", debugstr_w(name
.str
), lres
);
248 if(key_type
!= DO_DELETE
&& *iter
== '=') {
250 hres
= get_word(&iter
, buf
);
254 WARN("Wrong registry type: %s\n", debugstr_w(buf
->str
));
255 hres
= DISP_E_EXCEPTION
;
259 switch(buf
->str
[0]) {
261 hres
= get_word(&iter
, buf
);
264 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_SZ
, (PBYTE
)buf
->str
,
265 (lstrlenW(buf
->str
)+1)*sizeof(WCHAR
));
266 if(lres
!= ERROR_SUCCESS
) {
267 WARN("Could set value of key: %08x\n", lres
);
268 hres
= HRESULT_FROM_WIN32(lres
);
274 hres
= get_word(&iter
, buf
);
277 dw
= wcstol(buf
->str
, NULL
, 10);
278 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_DWORD
,
279 (PBYTE
)&dw
, sizeof(dw
));
280 if(lres
!= ERROR_SUCCESS
) {
281 WARN("Could set value of key: %08x\n", lres
);
282 hres
= HRESULT_FROM_WIN32(lres
);
291 hres
= get_word(&iter
, buf
);
294 count
= (lstrlenW(buf
->str
) + 1) / 2;
295 bytes
= HeapAlloc(GetProcessHeap(), 0, count
);
297 hres
= E_OUTOFMEMORY
;
300 for(i
= 0; i
< count
&& buf
->str
[2*i
]; i
++) {
302 if((d1
= xdigit_to_int(buf
->str
[2*i
])) == -1 || (d2
= xdigit_to_int(buf
->str
[2*i
+ 1])) == -1) {
306 bytes
[i
] = (d1
<< 4) | d2
;
308 if(SUCCEEDED(hres
)) {
309 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_BINARY
,
311 if(lres
!= ERROR_SUCCESS
) {
312 WARN("Could not set value of key: 0x%08x\n", lres
);
313 hres
= HRESULT_FROM_WIN32(lres
);
316 HeapFree(GetProcessHeap(), 0, bytes
);
320 WARN("Wrong resource type: %s\n", debugstr_w(buf
->str
));
321 hres
= DISP_E_EXCEPTION
;
328 hres
= get_word(&iter
, buf
);
332 }else if(key_type
== IS_VAL
) {
333 WARN("value not set!\n");
334 hres
= DISP_E_EXCEPTION
;
338 if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
&& *iter
== '{' && iswspace(iter
[1])) {
339 hres
= get_word(&iter
, buf
);
342 hres
= do_process_key(&iter
, hkey
, buf
, do_register
);
347 TRACE("%x %x\n", do_register
, key_type
);
348 if(!do_register
&& (key_type
== NORMAL
|| key_type
== FORCE_REMOVE
)) {
349 TRACE("Deleting %s\n", debugstr_w(name
.str
));
350 RegDeleteKeyW(parent_key
, name
.str
);
353 if(hkey
&& key_type
!= IS_VAL
)
358 hres
= get_word(&iter
, buf
);
363 HeapFree(GetProcessHeap(), 0, name
.str
);
364 if(hkey
&& key_type
!= IS_VAL
)
370 static HRESULT
do_process_root_key(LPCOLESTR data
, BOOL do_register
)
372 LPCOLESTR iter
= data
;
378 hres
= get_word(&iter
, &buf
);
384 WARN("ward.len == 0, failed\n");
385 hres
= DISP_E_EXCEPTION
;
388 for(i
=0; i
<ARRAY_SIZE(root_keys
); i
++) {
389 if(!lstrcmpiW(buf
.str
, root_keys
[i
].name
))
392 if(i
== ARRAY_SIZE(root_keys
)) {
393 WARN("Wrong root key name: %s\n", debugstr_w(buf
.str
));
394 hres
= DISP_E_EXCEPTION
;
397 hres
= get_word(&iter
, &buf
);
400 if(buf
.str
[1] || buf
.str
[0] != '{') {
401 WARN("Failed, expected '{', got %s\n", debugstr_w(buf
.str
));
402 hres
= DISP_E_EXCEPTION
;
405 hres
= do_process_key(&iter
, root_keys
[i
].key
, &buf
, do_register
);
407 WARN("Processing key failed: %08x\n", hres
);
410 hres
= get_word(&iter
, &buf
);
414 HeapFree(GetProcessHeap(), 0, buf
.str
);
418 static HRESULT
string_register(Registrar
*This
, LPCOLESTR data
, BOOL do_register
)
423 TRACE("(%p %s %x)\n", This
, debugstr_w(data
), do_register
);
426 hres
= do_preprocess(This
, data
, &buf
);
428 WARN("preprocessing failed!\n");
429 HeapFree(GetProcessHeap(), 0, buf
.str
);
433 hres
= do_process_root_key(buf
.str
, do_register
);
434 if(FAILED(hres
) && do_register
)
435 do_process_root_key(buf
.str
, FALSE
);
437 HeapFree(GetProcessHeap(), 0, buf
.str
);
441 static HRESULT
resource_register(Registrar
*This
, LPCOLESTR resFileName
,
442 LPCOLESTR szID
, LPCOLESTR szType
, BOOL do_register
)
451 hins
= LoadLibraryExW(resFileName
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
453 src
= FindResourceW(hins
, szID
, szType
);
455 regstra
= LoadResource(hins
, src
);
456 reslen
= SizeofResource(hins
, src
);
458 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, NULL
, 0)+1;
459 regstrw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
460 MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, regstrw
, len
);
461 regstrw
[len
-1] = '\0';
463 hres
= string_register(This
, regstrw
, do_register
);
465 HeapFree(GetProcessHeap(), 0, regstrw
);
467 WARN("could not load resource\n");
468 hres
= HRESULT_FROM_WIN32(GetLastError());
471 WARN("Could not find source\n");
472 hres
= HRESULT_FROM_WIN32(GetLastError());
476 WARN("Could not load resource file\n");
477 hres
= HRESULT_FROM_WIN32(GetLastError());
483 static HRESULT
file_register(Registrar
*This
, LPCOLESTR fileName
, BOOL do_register
)
491 file
= CreateFileW(fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
492 if(file
!= INVALID_HANDLE_VALUE
) {
493 filelen
= GetFileSize(file
, NULL
);
494 regstra
= HeapAlloc(GetProcessHeap(), 0, filelen
);
495 if(ReadFile(file
, regstra
, filelen
, NULL
, NULL
)) {
496 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, NULL
, 0)+1;
497 regstrw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
498 MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, regstrw
, len
);
499 regstrw
[len
-1] = '\0';
501 hres
= string_register(This
, regstrw
, do_register
);
503 HeapFree(GetProcessHeap(), 0, regstrw
);
505 WARN("Failed to read file %s\n", debugstr_w(fileName
));
506 hres
= HRESULT_FROM_WIN32(GetLastError());
508 HeapFree(GetProcessHeap(), 0, regstra
);
511 WARN("Could not open file %s\n", debugstr_w(fileName
));
512 hres
= HRESULT_FROM_WIN32(GetLastError());
518 static HRESULT WINAPI
Registrar_QueryInterface(IRegistrar
*iface
, REFIID riid
, void **ppvObject
)
520 TRACE("(%p)->(%s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
522 if(IsEqualGUID(&IID_IUnknown
, riid
)
523 || IsEqualGUID(&IID_IRegistrar
, riid
)
524 || IsEqualGUID(&IID_IRegistrarBase
, riid
)) {
525 IRegistrar_AddRef(iface
);
529 return E_NOINTERFACE
;
532 static ULONG WINAPI
Registrar_AddRef(IRegistrar
*iface
)
534 Registrar
*This
= impl_from_IRegistrar(iface
);
535 ULONG ref
= InterlockedIncrement(&This
->ref
);
536 TRACE("(%p) ->%d\n", This
, ref
);
540 static ULONG WINAPI
Registrar_Release(IRegistrar
*iface
)
542 Registrar
*This
= impl_from_IRegistrar(iface
);
543 ULONG ref
= InterlockedDecrement(&This
->ref
);
545 TRACE("(%p) ->%d\n", This
, ref
);
547 IRegistrar_ClearReplacements(iface
);
548 HeapFree(GetProcessHeap(), 0, This
);
553 static HRESULT WINAPI
Registrar_AddReplacement(IRegistrar
*iface
, LPCOLESTR Key
, LPCOLESTR item
)
555 Registrar
*This
= impl_from_IRegistrar(iface
);
559 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(Key
), debugstr_w(item
));
561 new_rep
= HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list
));
563 new_rep
->key_len
= lstrlenW(Key
);
564 new_rep
->key
= HeapAlloc(GetProcessHeap(), 0, (new_rep
->key_len
+ 1) * sizeof(OLECHAR
));
565 memcpy(new_rep
->key
, Key
, (new_rep
->key_len
+1)*sizeof(OLECHAR
));
567 len
= lstrlenW(item
)+1;
568 new_rep
->item
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(OLECHAR
));
569 memcpy(new_rep
->item
, item
, len
*sizeof(OLECHAR
));
571 new_rep
->next
= This
->rep
;
577 static HRESULT WINAPI
Registrar_ClearReplacements(IRegistrar
*iface
)
579 Registrar
*This
= impl_from_IRegistrar(iface
);
580 rep_list
*iter
, *iter2
;
582 TRACE("(%p)\n", This
);
590 HeapFree(GetProcessHeap(), 0, iter
->key
);
591 HeapFree(GetProcessHeap(), 0, iter
->item
);
592 HeapFree(GetProcessHeap(), 0, iter
);
600 static HRESULT WINAPI
Registrar_ResourceRegisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
601 LPCOLESTR szID
, LPCOLESTR szType
)
603 Registrar
*This
= impl_from_IRegistrar(iface
);
604 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
605 return resource_register(This
, resFileName
, szID
, szType
, TRUE
);
608 static HRESULT WINAPI
Registrar_ResourceUnregisterSz(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
, FALSE
);
616 static HRESULT WINAPI
Registrar_FileRegister(IRegistrar
* iface
, LPCOLESTR fileName
)
618 Registrar
*This
= impl_from_IRegistrar(iface
);
619 TRACE("(%p)->(%s)\n", This
, debugstr_w(fileName
));
620 return file_register(This
, fileName
, TRUE
);
623 static HRESULT WINAPI
Registrar_FileUnregister(IRegistrar
* iface
, LPCOLESTR fileName
)
625 Registrar
*This
= impl_from_IRegistrar(iface
);
626 FIXME("(%p)->(%s)\n", This
, debugstr_w(fileName
));
627 return file_register(This
, fileName
, FALSE
);
630 static HRESULT WINAPI
Registrar_StringRegister(IRegistrar
* iface
, LPCOLESTR data
)
632 Registrar
*This
= impl_from_IRegistrar(iface
);
633 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
634 return string_register(This
, data
, TRUE
);
637 static HRESULT WINAPI
Registrar_StringUnregister(IRegistrar
* iface
, LPCOLESTR data
)
639 Registrar
*This
= impl_from_IRegistrar(iface
);
640 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
641 return string_register(This
, data
, FALSE
);
644 static HRESULT WINAPI
Registrar_ResourceRegister(IRegistrar
* iface
, LPCOLESTR resFileName
,
645 UINT nID
, LPCOLESTR szType
)
647 Registrar
*This
= impl_from_IRegistrar(iface
);
648 TRACE("(%p)->(%s %d %s)\n", iface
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
649 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, TRUE
);
652 static HRESULT WINAPI
Registrar_ResourceUnregister(IRegistrar
* iface
, LPCOLESTR resFileName
,
653 UINT nID
, LPCOLESTR szType
)
655 Registrar
*This
= impl_from_IRegistrar(iface
);
656 TRACE("(%p)->(%s %d %s)\n", This
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
657 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, FALSE
);
660 static const IRegistrarVtbl RegistrarVtbl
= {
661 Registrar_QueryInterface
,
664 Registrar_AddReplacement
,
665 Registrar_ClearReplacements
,
666 Registrar_ResourceRegisterSz
,
667 Registrar_ResourceUnregisterSz
,
668 Registrar_FileRegister
,
669 Registrar_FileUnregister
,
670 Registrar_StringRegister
,
671 Registrar_StringUnregister
,
672 Registrar_ResourceRegister
,
673 Registrar_ResourceUnregister
,
676 /***********************************************************************
677 * AtlCreateRegistrar [atl100.@]
679 HRESULT WINAPI
AtlCreateRegistrar(IRegistrar
**ret
)
681 Registrar
*registrar
;
683 registrar
= HeapAlloc(GetProcessHeap(), 0, sizeof(*registrar
));
685 return E_OUTOFMEMORY
;
687 registrar
->IRegistrar_iface
.lpVtbl
= &RegistrarVtbl
;
689 registrar
->rep
= NULL
;
691 *ret
= ®istrar
->IRegistrar_iface
;
695 /***********************************************************************
696 * AtlUpdateRegistryFromResourceD [atl100.@]
698 HRESULT WINAPI
AtlUpdateRegistryFromResourceD(HINSTANCE inst
, LPCOLESTR res
,
699 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
*pMapEntries
, IRegistrar
*pReg
)
701 const struct _ATL_REGMAP_ENTRY
*iter
;
702 WCHAR module_name
[MAX_PATH
];
703 IRegistrar
*registrar
;
706 if(!GetModuleFileNameW(inst
, module_name
, MAX_PATH
)) {
707 FIXME("hinst %p: did not get module name\n", inst
);
711 TRACE("%p (%s), %s, %d, %p, %p\n", inst
, debugstr_w(module_name
),
712 debugstr_w(res
), bRegister
, pMapEntries
, pReg
);
717 hres
= AtlCreateRegistrar(®istrar
);
722 IRegistrar_AddReplacement(registrar
, L
"MODULE", module_name
);
724 for (iter
= pMapEntries
; iter
&& iter
->szKey
; iter
++)
725 IRegistrar_AddReplacement(registrar
, iter
->szKey
, iter
->szData
);
728 hres
= IRegistrar_ResourceRegisterSz(registrar
, module_name
, res
, L
"REGISTRY");
730 hres
= IRegistrar_ResourceUnregisterSz(registrar
, module_name
, res
, L
"REGISTRY");
732 if(registrar
!= pReg
)
733 IRegistrar_Release(registrar
);