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
= malloc(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
= realloc(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
== '\'') {
124 iter
= wcschr(iter
, '\'');
126 WARN("Unexpected end of script\n");
128 return DISP_E_EXCEPTION
;
130 if (iter
[1] != '\'') break;
132 strbuf_write(iter2
, buf
, iter
-iter2
);
134 strbuf_write(iter2
, buf
, iter
-iter2
);
137 while(*iter
&& !iswspace(*iter
))
139 strbuf_write(iter2
, buf
, iter
-iter2
);
142 while(iswspace(*iter
))
148 static HRESULT
do_preprocess(const Registrar
*This
, LPCOLESTR data
, strbuf
*buf
)
150 LPCOLESTR iter
, iter2
= data
;
153 iter
= wcschr(data
, '%');
155 strbuf_write(iter2
, buf
, iter
-iter2
);
159 return DISP_E_EXCEPTION
;
160 iter
= wcschr(iter2
, '%');
162 return DISP_E_EXCEPTION
;
165 strbuf_write(L
"%", buf
, 1);
167 for(rep_iter
= This
->rep
; rep_iter
; rep_iter
= rep_iter
->next
) {
168 if(rep_iter
->key_len
== iter
-iter2
169 && !wcsnicmp(iter2
, rep_iter
->key
, rep_iter
->key_len
))
173 WARN("Could not find replacement: %s\n", debugstr_wn(iter2
, iter
-iter2
));
174 return DISP_E_EXCEPTION
;
177 strbuf_write(rep_iter
->item
, buf
, -1);
181 iter
= wcschr(iter
, '%');
184 strbuf_write(iter2
, buf
, -1);
185 TRACE("%s\n", debugstr_w(buf
->str
));
190 static HRESULT
do_process_key(LPCOLESTR
*pstr
, HKEY parent_key
, strbuf
*buf
, BOOL do_register
)
207 hres
= get_word(&iter
, buf
);
212 while(buf
->str
[1] || buf
->str
[0] != '}') {
214 if(!lstrcmpiW(buf
->str
, L
"NoRemove"))
215 key_type
= NO_REMOVE
;
216 else if(!lstrcmpiW(buf
->str
, L
"ForceRemove"))
217 key_type
= FORCE_REMOVE
;
218 else if(!lstrcmpiW(buf
->str
, L
"val"))
220 else if(!lstrcmpiW(buf
->str
, L
"Delete"))
221 key_type
= DO_DELETE
;
223 if(key_type
!= NORMAL
) {
224 hres
= get_word(&iter
, buf
);
228 TRACE("name = %s\n", debugstr_w(buf
->str
));
231 if(key_type
== IS_VAL
) {
233 strbuf_write(buf
->str
, &name
, -1);
234 }else if(key_type
== DO_DELETE
) {
235 TRACE("Deleting %s\n", debugstr_w(buf
->str
));
236 RegDeleteTreeW(parent_key
, buf
->str
);
238 if(key_type
== FORCE_REMOVE
)
239 RegDeleteTreeW(parent_key
, buf
->str
);
240 lres
= RegCreateKeyW(parent_key
, buf
->str
, &hkey
);
241 if(lres
!= ERROR_SUCCESS
) {
242 WARN("Could not create(open) key: %08lx\n", lres
);
243 hres
= HRESULT_FROM_WIN32(lres
);
247 }else if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
) {
248 strbuf_write(buf
->str
, &name
, -1);
249 lres
= RegOpenKeyW(parent_key
, buf
->str
, &hkey
);
250 if(lres
!= ERROR_SUCCESS
)
251 WARN("Could not open key %s: %08lx\n", debugstr_w(name
.str
), lres
);
254 if(key_type
!= DO_DELETE
&& *iter
== '=') {
256 hres
= get_word(&iter
, buf
);
260 WARN("Wrong registry type: %s\n", debugstr_w(buf
->str
));
261 hres
= DISP_E_EXCEPTION
;
265 switch(buf
->str
[0]) {
267 hres
= get_word(&iter
, buf
);
270 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_SZ
, (PBYTE
)buf
->str
,
271 (lstrlenW(buf
->str
)+1)*sizeof(WCHAR
));
272 if(lres
!= ERROR_SUCCESS
) {
273 WARN("Could set value of key: %08lx\n", lres
);
274 hres
= HRESULT_FROM_WIN32(lres
);
280 hres
= get_word(&iter
, buf
);
283 dw
= wcstol(buf
->str
, NULL
, 10);
284 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_DWORD
,
285 (PBYTE
)&dw
, sizeof(dw
));
286 if(lres
!= ERROR_SUCCESS
) {
287 WARN("Could set value of key: %08lx\n", lres
);
288 hres
= HRESULT_FROM_WIN32(lres
);
297 hres
= get_word(&iter
, buf
);
300 count
= (lstrlenW(buf
->str
) + 1) / 2;
301 bytes
= malloc(count
);
303 hres
= E_OUTOFMEMORY
;
306 for(i
= 0; i
< count
&& buf
->str
[2*i
]; i
++) {
308 if((d1
= xdigit_to_int(buf
->str
[2*i
])) == -1 || (d2
= xdigit_to_int(buf
->str
[2*i
+ 1])) == -1) {
312 bytes
[i
] = (d1
<< 4) | d2
;
314 if(SUCCEEDED(hres
)) {
315 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_BINARY
,
317 if(lres
!= ERROR_SUCCESS
) {
318 WARN("Could not set value of key: 0x%08lx\n", lres
);
319 hres
= HRESULT_FROM_WIN32(lres
);
326 WARN("Wrong resource type: %s\n", debugstr_w(buf
->str
));
327 hres
= DISP_E_EXCEPTION
;
334 hres
= get_word(&iter
, buf
);
338 }else if(key_type
== IS_VAL
) {
339 WARN("value not set!\n");
340 hres
= DISP_E_EXCEPTION
;
344 if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
&& *iter
== '{' && iswspace(iter
[1])) {
345 hres
= get_word(&iter
, buf
);
348 hres
= do_process_key(&iter
, hkey
, buf
, do_register
);
353 TRACE("%x %x\n", do_register
, key_type
);
354 if(!do_register
&& (key_type
== NORMAL
|| key_type
== FORCE_REMOVE
)) {
355 TRACE("Deleting %s\n", debugstr_w(name
.str
));
356 RegDeleteKeyW(parent_key
, name
.str
);
359 if(hkey
&& key_type
!= IS_VAL
)
364 hres
= get_word(&iter
, buf
);
370 if(hkey
&& key_type
!= IS_VAL
)
376 static HRESULT
do_process_root_key(LPCOLESTR data
, BOOL do_register
)
378 LPCOLESTR iter
= data
;
384 hres
= get_word(&iter
, &buf
);
390 WARN("ward.len == 0, failed\n");
391 hres
= DISP_E_EXCEPTION
;
394 for(i
=0; i
<ARRAY_SIZE(root_keys
); i
++) {
395 if(!lstrcmpiW(buf
.str
, root_keys
[i
].name
))
398 if(i
== ARRAY_SIZE(root_keys
)) {
399 WARN("Wrong root key name: %s\n", debugstr_w(buf
.str
));
400 hres
= DISP_E_EXCEPTION
;
403 hres
= get_word(&iter
, &buf
);
406 if(buf
.str
[1] || buf
.str
[0] != '{') {
407 WARN("Failed, expected '{', got %s\n", debugstr_w(buf
.str
));
408 hres
= DISP_E_EXCEPTION
;
411 hres
= do_process_key(&iter
, root_keys
[i
].key
, &buf
, do_register
);
413 WARN("Processing key failed: %08lx\n", hres
);
416 hres
= get_word(&iter
, &buf
);
424 static HRESULT
string_register(Registrar
*This
, LPCOLESTR data
, BOOL do_register
)
429 TRACE("(%p %s %x)\n", This
, debugstr_w(data
), do_register
);
432 hres
= do_preprocess(This
, data
, &buf
);
434 WARN("preprocessing failed!\n");
439 hres
= do_process_root_key(buf
.str
, do_register
);
440 if(FAILED(hres
) && do_register
)
441 do_process_root_key(buf
.str
, FALSE
);
447 static HRESULT
resource_register(Registrar
*This
, LPCOLESTR resFileName
,
448 LPCOLESTR szID
, LPCOLESTR szType
, BOOL do_register
)
457 hins
= LoadLibraryExW(resFileName
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
459 src
= FindResourceW(hins
, szID
, szType
);
461 regstra
= LoadResource(hins
, src
);
462 reslen
= SizeofResource(hins
, src
);
464 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, NULL
, 0)+1;
465 regstrw
= calloc(len
, sizeof(WCHAR
));
466 MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, regstrw
, len
);
467 regstrw
[len
-1] = '\0';
469 hres
= string_register(This
, regstrw
, do_register
);
473 WARN("could not load resource\n");
474 hres
= HRESULT_FROM_WIN32(GetLastError());
477 WARN("Could not find source\n");
478 hres
= HRESULT_FROM_WIN32(GetLastError());
482 WARN("Could not load resource file\n");
483 hres
= HRESULT_FROM_WIN32(GetLastError());
489 static HRESULT
file_register(Registrar
*This
, LPCOLESTR fileName
, BOOL do_register
)
497 file
= CreateFileW(fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
498 if(file
!= INVALID_HANDLE_VALUE
) {
499 filelen
= GetFileSize(file
, NULL
);
500 regstra
= malloc(filelen
);
501 if(ReadFile(file
, regstra
, filelen
, NULL
, NULL
)) {
502 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, NULL
, 0)+1;
503 regstrw
= calloc(len
, sizeof(WCHAR
));
504 MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, regstrw
, len
);
505 regstrw
[len
-1] = '\0';
507 hres
= string_register(This
, regstrw
, do_register
);
511 WARN("Failed to read file %s\n", debugstr_w(fileName
));
512 hres
= HRESULT_FROM_WIN32(GetLastError());
517 WARN("Could not open file %s\n", debugstr_w(fileName
));
518 hres
= HRESULT_FROM_WIN32(GetLastError());
524 static HRESULT WINAPI
Registrar_QueryInterface(IRegistrar
*iface
, REFIID riid
, void **ppvObject
)
526 TRACE("(%p)->(%s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
528 if(IsEqualGUID(&IID_IUnknown
, riid
)
529 || IsEqualGUID(&IID_IRegistrar
, riid
)
530 || IsEqualGUID(&IID_IRegistrarBase
, riid
)) {
531 IRegistrar_AddRef(iface
);
535 return E_NOINTERFACE
;
538 static ULONG WINAPI
Registrar_AddRef(IRegistrar
*iface
)
540 Registrar
*This
= impl_from_IRegistrar(iface
);
541 ULONG ref
= InterlockedIncrement(&This
->ref
);
542 TRACE("(%p) ->%ld\n", This
, ref
);
546 static ULONG WINAPI
Registrar_Release(IRegistrar
*iface
)
548 Registrar
*This
= impl_from_IRegistrar(iface
);
549 ULONG ref
= InterlockedDecrement(&This
->ref
);
551 TRACE("(%p) ->%ld\n", This
, ref
);
553 IRegistrar_ClearReplacements(iface
);
559 static HRESULT WINAPI
Registrar_AddReplacement(IRegistrar
*iface
, LPCOLESTR Key
, LPCOLESTR item
)
561 Registrar
*This
= impl_from_IRegistrar(iface
);
565 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(Key
), debugstr_w(item
));
567 new_rep
= malloc(sizeof(*new_rep
));
569 new_rep
->key_len
= lstrlenW(Key
);
570 new_rep
->key
= malloc((new_rep
->key_len
+ 1) * sizeof(OLECHAR
));
571 memcpy(new_rep
->key
, Key
, (new_rep
->key_len
+1)*sizeof(OLECHAR
));
573 len
= lstrlenW(item
)+1;
574 new_rep
->item
= malloc(len
*sizeof(OLECHAR
));
575 memcpy(new_rep
->item
, item
, len
*sizeof(OLECHAR
));
577 new_rep
->next
= This
->rep
;
583 static HRESULT WINAPI
Registrar_ClearReplacements(IRegistrar
*iface
)
585 Registrar
*This
= impl_from_IRegistrar(iface
);
586 rep_list
*iter
, *iter2
;
588 TRACE("(%p)\n", This
);
606 static HRESULT WINAPI
Registrar_ResourceRegisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
607 LPCOLESTR szID
, LPCOLESTR szType
)
609 Registrar
*This
= impl_from_IRegistrar(iface
);
610 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
611 return resource_register(This
, resFileName
, szID
, szType
, TRUE
);
614 static HRESULT WINAPI
Registrar_ResourceUnregisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
615 LPCOLESTR szID
, LPCOLESTR szType
)
617 Registrar
*This
= impl_from_IRegistrar(iface
);
618 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
619 return resource_register(This
, resFileName
, szID
, szType
, FALSE
);
622 static HRESULT WINAPI
Registrar_FileRegister(IRegistrar
* iface
, LPCOLESTR fileName
)
624 Registrar
*This
= impl_from_IRegistrar(iface
);
625 TRACE("(%p)->(%s)\n", This
, debugstr_w(fileName
));
626 return file_register(This
, fileName
, TRUE
);
629 static HRESULT WINAPI
Registrar_FileUnregister(IRegistrar
* iface
, LPCOLESTR fileName
)
631 Registrar
*This
= impl_from_IRegistrar(iface
);
632 FIXME("(%p)->(%s)\n", This
, debugstr_w(fileName
));
633 return file_register(This
, fileName
, FALSE
);
636 static HRESULT WINAPI
Registrar_StringRegister(IRegistrar
* iface
, LPCOLESTR data
)
638 Registrar
*This
= impl_from_IRegistrar(iface
);
639 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
640 return string_register(This
, data
, TRUE
);
643 static HRESULT WINAPI
Registrar_StringUnregister(IRegistrar
* iface
, LPCOLESTR data
)
645 Registrar
*This
= impl_from_IRegistrar(iface
);
646 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
647 return string_register(This
, data
, FALSE
);
650 static HRESULT WINAPI
Registrar_ResourceRegister(IRegistrar
* iface
, LPCOLESTR resFileName
,
651 UINT nID
, LPCOLESTR szType
)
653 Registrar
*This
= impl_from_IRegistrar(iface
);
654 TRACE("(%p)->(%s %d %s)\n", iface
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
655 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, TRUE
);
658 static HRESULT WINAPI
Registrar_ResourceUnregister(IRegistrar
* iface
, LPCOLESTR resFileName
,
659 UINT nID
, LPCOLESTR szType
)
661 Registrar
*This
= impl_from_IRegistrar(iface
);
662 TRACE("(%p)->(%s %d %s)\n", This
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
663 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, FALSE
);
666 static const IRegistrarVtbl RegistrarVtbl
= {
667 Registrar_QueryInterface
,
670 Registrar_AddReplacement
,
671 Registrar_ClearReplacements
,
672 Registrar_ResourceRegisterSz
,
673 Registrar_ResourceUnregisterSz
,
674 Registrar_FileRegister
,
675 Registrar_FileUnregister
,
676 Registrar_StringRegister
,
677 Registrar_StringUnregister
,
678 Registrar_ResourceRegister
,
679 Registrar_ResourceUnregister
,
682 /***********************************************************************
683 * AtlCreateRegistrar [atl100.@]
685 HRESULT WINAPI
AtlCreateRegistrar(IRegistrar
**ret
)
687 Registrar
*registrar
;
689 registrar
= calloc(1, sizeof(*registrar
));
691 return E_OUTOFMEMORY
;
693 registrar
->IRegistrar_iface
.lpVtbl
= &RegistrarVtbl
;
696 *ret
= ®istrar
->IRegistrar_iface
;
700 /***********************************************************************
701 * AtlUpdateRegistryFromResourceD [atl100.@]
703 HRESULT WINAPI
AtlUpdateRegistryFromResourceD(HINSTANCE inst
, LPCOLESTR res
,
704 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
*pMapEntries
, IRegistrar
*pReg
)
706 const struct _ATL_REGMAP_ENTRY
*iter
;
707 WCHAR module_name
[MAX_PATH
];
708 IRegistrar
*registrar
;
711 if(!GetModuleFileNameW(inst
, module_name
, MAX_PATH
)) {
712 FIXME("hinst %p: did not get module name\n", inst
);
716 TRACE("%p (%s), %s, %d, %p, %p\n", inst
, debugstr_w(module_name
),
717 debugstr_w(res
), bRegister
, pMapEntries
, pReg
);
722 hres
= AtlCreateRegistrar(®istrar
);
727 IRegistrar_AddReplacement(registrar
, L
"MODULE", module_name
);
729 for (iter
= pMapEntries
; iter
&& iter
->szKey
; iter
++)
730 IRegistrar_AddReplacement(registrar
, iter
->szKey
, iter
->szData
);
733 hres
= IRegistrar_ResourceRegisterSz(registrar
, module_name
, res
, L
"REGISTRY");
735 hres
= IRegistrar_ResourceUnregisterSz(registrar
, module_name
, res
, L
"REGISTRY");
737 if(registrar
!= pReg
)
738 IRegistrar_Release(registrar
);