shell32: Avoid leaks and add parameter checks in SHCreateShellFolderView[Ex].
[wine.git] / dlls / atl100 / registrar.c
blob9cad32a7d142dcf8e655e643e5d13d33aa3f1a00
1 /*
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
19 #define COBJMACROS
21 #include "atlbase.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(atl);
28 /**************************************************************
29 * ATLRegistrar implementation
32 static const struct {
33 WCHAR name[22];
34 HKEY key;
35 } root_keys[] = {
36 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
37 HKEY_CLASSES_ROOT},
38 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
39 HKEY_CURRENT_USER},
40 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
41 HKEY_LOCAL_MACHINE},
42 {{'H','K','E','Y','_','U','S','E','R','S',0},
43 HKEY_USERS},
44 {{'H','K','E','Y','_','P','E','R','F','O','R','M','A','N','C','E','_','D','A','T','A',0},
45 HKEY_PERFORMANCE_DATA},
46 {{'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0},
47 HKEY_DYN_DATA},
48 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
49 HKEY_CURRENT_CONFIG},
50 {{'H','K','C','R',0}, HKEY_CLASSES_ROOT},
51 {{'H','K','C','U',0}, HKEY_CURRENT_USER},
52 {{'H','K','L','M',0}, HKEY_LOCAL_MACHINE},
53 {{'H','K','U',0}, HKEY_USERS},
54 {{'H','K','P','D',0}, HKEY_PERFORMANCE_DATA},
55 {{'H','K','D','D',0}, HKEY_DYN_DATA},
56 {{'H','K','C','C',0}, HKEY_CURRENT_CONFIG}
59 typedef struct rep_list_str {
60 LPOLESTR key;
61 LPOLESTR item;
62 int key_len;
63 struct rep_list_str *next;
64 } rep_list;
66 typedef struct {
67 IRegistrar IRegistrar_iface;
68 LONG ref;
69 rep_list *rep;
70 } Registrar;
72 typedef struct {
73 LPOLESTR str;
74 DWORD alloc;
75 DWORD len;
76 } strbuf;
78 static inline Registrar *impl_from_IRegistrar(IRegistrar *iface)
80 return CONTAINING_RECORD(iface, Registrar, IRegistrar_iface);
83 static void strbuf_init(strbuf *buf)
85 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
86 buf->alloc = 128;
87 buf->len = 0;
90 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
92 if(len == -1)
93 len = lstrlenW(str);
94 if(buf->len+len+1 >= buf->alloc) {
95 buf->alloc = (buf->len+len)<<1;
96 buf->str = HeapReAlloc(GetProcessHeap(), 0, buf->str, buf->alloc*sizeof(WCHAR));
98 memcpy(buf->str+buf->len, str, len*sizeof(OLECHAR));
99 buf->len += len;
100 buf->str[buf->len] = '\0';
103 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
105 LPCOLESTR iter, iter2 = *str;
107 buf->len = 0;
108 buf->str[0] = '\0';
110 while(isspaceW(*iter2))
111 iter2++;
112 iter = iter2;
113 if(!*iter) {
114 *str = iter;
115 return S_OK;
118 if(*iter == '}' || *iter == '=') {
119 strbuf_write(iter++, buf, 1);
120 }else if(*iter == '\'') {
121 iter2 = ++iter;
122 iter = strchrW(iter, '\'');
123 if(!iter) {
124 WARN("Unexpected end of script\n");
125 *str = iter;
126 return DISP_E_EXCEPTION;
128 strbuf_write(iter2, buf, iter-iter2);
129 iter++;
130 }else {
131 while(*iter && !isspaceW(*iter))
132 iter++;
133 strbuf_write(iter2, buf, iter-iter2);
136 while(isspaceW(*iter))
137 iter++;
138 *str = iter;
139 return S_OK;
142 static HRESULT do_preprocess(const Registrar *This, LPCOLESTR data, strbuf *buf)
144 LPCOLESTR iter, iter2 = data;
145 rep_list *rep_iter;
146 static const WCHAR wstr[] = {'%',0};
148 iter = strchrW(data, '%');
149 while(iter) {
150 strbuf_write(iter2, buf, iter-iter2);
152 iter2 = ++iter;
153 if(!*iter2)
154 return DISP_E_EXCEPTION;
155 iter = strchrW(iter2, '%');
156 if(!iter)
157 return DISP_E_EXCEPTION;
159 if(iter == iter2) {
160 strbuf_write(wstr, buf, 1);
161 }else {
162 for(rep_iter = This->rep; rep_iter; rep_iter = rep_iter->next) {
163 if(rep_iter->key_len == iter-iter2
164 && !memicmpW(iter2, rep_iter->key, rep_iter->key_len))
165 break;
167 if(!rep_iter) {
168 WARN("Could not find replacement: %s\n", debugstr_wn(iter2, iter-iter2));
169 return DISP_E_EXCEPTION;
172 strbuf_write(rep_iter->item, buf, -1);
175 iter2 = ++iter;
176 iter = strchrW(iter, '%');
179 strbuf_write(iter2, buf, -1);
180 TRACE("%s\n", debugstr_w(buf->str));
182 return S_OK;
185 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
187 LPCOLESTR iter = *pstr;
188 HRESULT hres;
189 LONG lres;
190 HKEY hkey = 0;
191 strbuf name;
193 enum {
194 NORMAL,
195 NO_REMOVE,
196 IS_VAL,
197 FORCE_REMOVE,
198 DO_DELETE
199 } key_type = NORMAL;
201 static const WCHAR wstrNoRemove[] = {'N','o','R','e','m','o','v','e',0};
202 static const WCHAR wstrForceRemove[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
203 static const WCHAR wstrDelete[] = {'D','e','l','e','t','e',0};
204 static const WCHAR wstrval[] = {'v','a','l',0};
206 iter = *pstr;
207 hres = get_word(&iter, buf);
208 if(FAILED(hres))
209 return hres;
210 strbuf_init(&name);
212 while(buf->str[1] || buf->str[0] != '}') {
213 key_type = NORMAL;
214 if(!lstrcmpiW(buf->str, wstrNoRemove))
215 key_type = NO_REMOVE;
216 else if(!lstrcmpiW(buf->str, wstrForceRemove))
217 key_type = FORCE_REMOVE;
218 else if(!lstrcmpiW(buf->str, wstrval))
219 key_type = IS_VAL;
220 else if(!lstrcmpiW(buf->str, wstrDelete))
221 key_type = DO_DELETE;
223 if(key_type != NORMAL) {
224 hres = get_word(&iter, buf);
225 if(FAILED(hres))
226 break;
228 TRACE("name = %s\n", debugstr_w(buf->str));
230 if(do_register) {
231 if(key_type == IS_VAL) {
232 hkey = parent_key;
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);
237 }else {
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: %08x\n", lres);
243 hres = HRESULT_FROM_WIN32(lres);
244 break;
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: %08x\n", debugstr_w(name.str), lres);
254 if(key_type != DO_DELETE && *iter == '=') {
255 iter++;
256 hres = get_word(&iter, buf);
257 if(FAILED(hres))
258 break;
259 if(buf->len != 1) {
260 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
261 hres = DISP_E_EXCEPTION;
262 break;
264 if(do_register) {
265 switch(buf->str[0]) {
266 case 's':
267 hres = get_word(&iter, buf);
268 if(FAILED(hres))
269 break;
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: %08x\n", lres);
274 hres = HRESULT_FROM_WIN32(lres);
275 break;
277 break;
278 case 'd': {
279 DWORD dw;
280 hres = get_word(&iter, buf);
281 if(FAILED(hres))
282 break;
283 dw = atoiW(buf->str);
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: %08x\n", lres);
288 hres = HRESULT_FROM_WIN32(lres);
289 break;
291 break;
293 case 'b': {
294 BYTE *bytes;
295 DWORD count;
296 DWORD i;
297 hres = get_word(&iter, buf);
298 if(FAILED(hres))
299 break;
300 count = (lstrlenW(buf->str) + 1) / 2;
301 bytes = HeapAlloc(GetProcessHeap(), 0, count);
302 if(bytes == NULL) {
303 hres = E_OUTOFMEMORY;
304 break;
306 for(i = 0; i < count && buf->str[2*i]; i++) {
307 WCHAR digits[3];
308 if(!isxdigitW(buf->str[2*i]) || !isxdigitW(buf->str[2*i + 1])) {
309 hres = E_FAIL;
310 break;
312 digits[0] = buf->str[2*i];
313 digits[1] = buf->str[2*i + 1];
314 digits[2] = 0;
315 bytes[i] = (BYTE) strtoulW(digits, NULL, 16);
317 if(SUCCEEDED(hres)) {
318 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_BINARY,
319 bytes, count);
320 if(lres != ERROR_SUCCESS) {
321 WARN("Could not set value of key: 0x%08x\n", lres);
322 hres = HRESULT_FROM_WIN32(lres);
325 HeapFree(GetProcessHeap(), 0, bytes);
326 break;
328 default:
329 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
330 hres = DISP_E_EXCEPTION;
332 if(FAILED(hres))
333 break;
334 }else {
335 if(*iter == '-')
336 iter++;
337 hres = get_word(&iter, buf);
338 if(FAILED(hres))
339 break;
341 }else if(key_type == IS_VAL) {
342 WARN("value not set!\n");
343 hres = DISP_E_EXCEPTION;
344 break;
347 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
348 hres = get_word(&iter, buf);
349 if(FAILED(hres))
350 break;
351 hres = do_process_key(&iter, hkey, buf, do_register);
352 if(FAILED(hres))
353 break;
356 TRACE("%x %x\n", do_register, key_type);
357 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
358 TRACE("Deleting %s\n", debugstr_w(name.str));
359 RegDeleteKeyW(parent_key, name.str);
362 if(hkey && key_type != IS_VAL)
363 RegCloseKey(hkey);
364 hkey = 0;
365 name.len = 0;
367 hres = get_word(&iter, buf);
368 if(FAILED(hres))
369 break;
372 HeapFree(GetProcessHeap(), 0, name.str);
373 if(hkey && key_type != IS_VAL)
374 RegCloseKey(hkey);
375 *pstr = iter;
376 return hres;
379 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
381 LPCOLESTR iter = data;
382 strbuf buf;
383 HRESULT hres = S_OK;
384 unsigned int i;
386 strbuf_init(&buf);
387 hres = get_word(&iter, &buf);
388 if(FAILED(hres))
389 return hres;
391 while(*iter) {
392 if(!buf.len) {
393 WARN("ward.len == 0, failed\n");
394 hres = DISP_E_EXCEPTION;
395 break;
397 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
398 if(!lstrcmpiW(buf.str, root_keys[i].name))
399 break;
401 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
402 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
403 hres = DISP_E_EXCEPTION;
404 break;
406 hres = get_word(&iter, &buf);
407 if(FAILED(hres))
408 break;
409 if(buf.str[1] || buf.str[0] != '{') {
410 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
411 hres = DISP_E_EXCEPTION;
412 break;
414 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
415 if(FAILED(hres)) {
416 WARN("Processing key failed: %08x\n", hres);
417 break;
419 hres = get_word(&iter, &buf);
420 if(FAILED(hres))
421 break;
423 HeapFree(GetProcessHeap(), 0, buf.str);
424 return hres;
427 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
429 strbuf buf;
430 HRESULT hres;
432 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
434 strbuf_init(&buf);
435 hres = do_preprocess(This, data, &buf);
436 if(FAILED(hres)) {
437 WARN("preprocessing failed!\n");
438 HeapFree(GetProcessHeap(), 0, buf.str);
439 return hres;
442 hres = do_process_root_key(buf.str, do_register);
443 if(FAILED(hres) && do_register)
444 do_process_root_key(buf.str, FALSE);
446 HeapFree(GetProcessHeap(), 0, buf.str);
447 return hres;
450 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
451 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
453 HINSTANCE hins;
454 HRSRC src;
455 LPSTR regstra;
456 LPWSTR regstrw;
457 DWORD len, reslen;
458 HRESULT hres;
460 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
461 if(hins) {
462 src = FindResourceW(hins, szID, szType);
463 if(src) {
464 regstra = LoadResource(hins, src);
465 reslen = SizeofResource(hins, src);
466 if(regstra) {
467 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
468 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
469 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, len);
470 regstrw[len-1] = '\0';
472 hres = string_register(This, regstrw, do_register);
474 HeapFree(GetProcessHeap(), 0, regstrw);
475 }else {
476 WARN("could not load resource\n");
477 hres = HRESULT_FROM_WIN32(GetLastError());
479 }else {
480 WARN("Could not find source\n");
481 hres = HRESULT_FROM_WIN32(GetLastError());
483 FreeLibrary(hins);
484 }else {
485 WARN("Could not load resource file\n");
486 hres = HRESULT_FROM_WIN32(GetLastError());
489 return hres;
492 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
494 HANDLE file;
495 DWORD filelen, len;
496 LPWSTR regstrw;
497 LPSTR regstra;
498 LRESULT lres;
499 HRESULT hres;
501 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
502 if(file != INVALID_HANDLE_VALUE) {
503 filelen = GetFileSize(file, NULL);
504 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
505 lres = ReadFile(file, regstra, filelen, NULL, NULL);
506 if(lres == ERROR_SUCCESS) {
507 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
508 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
509 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, len);
510 regstrw[len-1] = '\0';
512 hres = string_register(This, regstrw, do_register);
514 HeapFree(GetProcessHeap(), 0, regstrw);
515 }else {
516 WARN("Failed to read faile\n");
517 hres = HRESULT_FROM_WIN32(lres);
519 HeapFree(GetProcessHeap(), 0, regstra);
520 CloseHandle(file);
521 }else {
522 WARN("Could not open file\n");
523 hres = HRESULT_FROM_WIN32(GetLastError());
526 return hres;
529 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
531 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
533 if(IsEqualGUID(&IID_IUnknown, riid)
534 || IsEqualGUID(&IID_IRegistrar, riid)
535 || IsEqualGUID(&IID_IRegistrarBase, riid)) {
536 IRegistrar_AddRef(iface);
537 *ppvObject = iface;
538 return S_OK;
540 return E_NOINTERFACE;
543 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
545 Registrar *This = impl_from_IRegistrar(iface);
546 ULONG ref = InterlockedIncrement(&This->ref);
547 TRACE("(%p) ->%d\n", This, ref);
548 return ref;
551 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
553 Registrar *This = impl_from_IRegistrar(iface);
554 ULONG ref = InterlockedDecrement(&This->ref);
556 TRACE("(%p) ->%d\n", This, ref);
557 if(!ref) {
558 IRegistrar_ClearReplacements(iface);
559 HeapFree(GetProcessHeap(), 0, This);
561 return ref;
564 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
566 Registrar *This = impl_from_IRegistrar(iface);
567 int len;
568 rep_list *new_rep;
570 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
572 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
574 new_rep->key_len = lstrlenW(Key);
575 new_rep->key = HeapAlloc(GetProcessHeap(), 0, (new_rep->key_len + 1) * sizeof(OLECHAR));
576 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
578 len = lstrlenW(item)+1;
579 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
580 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
582 new_rep->next = This->rep;
583 This->rep = new_rep;
585 return S_OK;
588 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
590 Registrar *This = impl_from_IRegistrar(iface);
591 rep_list *iter, *iter2;
593 TRACE("(%p)\n", This);
595 if(!This->rep)
596 return S_OK;
598 iter = This->rep;
599 while(iter) {
600 iter2 = iter->next;
601 HeapFree(GetProcessHeap(), 0, iter->key);
602 HeapFree(GetProcessHeap(), 0, iter->item);
603 HeapFree(GetProcessHeap(), 0, iter);
604 iter = iter2;
607 This->rep = NULL;
608 return S_OK;
611 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
612 LPCOLESTR szID, LPCOLESTR szType)
614 Registrar *This = impl_from_IRegistrar(iface);
615 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
616 return resource_register(This, resFileName, szID, szType, TRUE);
619 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
620 LPCOLESTR szID, LPCOLESTR szType)
622 Registrar *This = impl_from_IRegistrar(iface);
623 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
624 return resource_register(This, resFileName, szID, szType, FALSE);
627 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
629 Registrar *This = impl_from_IRegistrar(iface);
630 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
631 return file_register(This, fileName, TRUE);
634 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
636 Registrar *This = impl_from_IRegistrar(iface);
637 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
638 return file_register(This, fileName, FALSE);
641 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
643 Registrar *This = impl_from_IRegistrar(iface);
644 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
645 return string_register(This, data, TRUE);
648 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
650 Registrar *This = impl_from_IRegistrar(iface);
651 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
652 return string_register(This, data, FALSE);
655 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
656 UINT nID, LPCOLESTR szType)
658 Registrar *This = impl_from_IRegistrar(iface);
659 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
660 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, TRUE);
663 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
664 UINT nID, LPCOLESTR szType)
666 Registrar *This = impl_from_IRegistrar(iface);
667 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
668 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, FALSE);
671 static const IRegistrarVtbl RegistrarVtbl = {
672 Registrar_QueryInterface,
673 Registrar_AddRef,
674 Registrar_Release,
675 Registrar_AddReplacement,
676 Registrar_ClearReplacements,
677 Registrar_ResourceRegisterSz,
678 Registrar_ResourceUnregisterSz,
679 Registrar_FileRegister,
680 Registrar_FileUnregister,
681 Registrar_StringRegister,
682 Registrar_StringUnregister,
683 Registrar_ResourceRegister,
684 Registrar_ResourceUnregister,
687 /***********************************************************************
688 * AtlCreateRegistrar [atl100.@]
690 HRESULT WINAPI AtlCreateRegistrar(IRegistrar **ret)
692 Registrar *registrar;
694 registrar = HeapAlloc(GetProcessHeap(), 0, sizeof(*registrar));
695 if(!registrar)
696 return E_OUTOFMEMORY;
698 registrar->IRegistrar_iface.lpVtbl = &RegistrarVtbl;
699 registrar->ref = 1;
700 registrar->rep = NULL;
702 *ret = &registrar->IRegistrar_iface;
703 return S_OK;
706 /***********************************************************************
707 * AtlUpdateRegistryFromResourceD [atl100.@]
709 HRESULT WINAPI AtlUpdateRegistryFromResourceD(HINSTANCE inst, LPCOLESTR res,
710 BOOL bRegister, struct _ATL_REGMAP_ENTRY *pMapEntries, IRegistrar *pReg)
712 const struct _ATL_REGMAP_ENTRY *iter;
713 WCHAR module_name[MAX_PATH];
714 IRegistrar *registrar;
715 HRESULT hres;
717 static const WCHAR moduleW[] = {'M','O','D','U','L','E',0};
718 static const WCHAR registryW[] = {'R','E','G','I','S','T','R','Y',0};
720 if(!GetModuleFileNameW(inst, module_name, MAX_PATH)) {
721 FIXME("hinst %p: did not get module name\n", inst);
722 return E_FAIL;
725 TRACE("%p (%s), %s, %d, %p, %p\n", inst, debugstr_w(module_name),
726 debugstr_w(res), bRegister, pMapEntries, pReg);
728 if(pReg) {
729 registrar = pReg;
730 }else {
731 hres = AtlCreateRegistrar(&registrar);
732 if(FAILED(hres))
733 return hres;
736 IRegistrar_AddReplacement(registrar, moduleW, module_name);
738 for (iter = pMapEntries; iter && iter->szKey; iter++)
739 IRegistrar_AddReplacement(registrar, iter->szKey, iter->szData);
741 if(bRegister)
742 hres = IRegistrar_ResourceRegisterSz(registrar, module_name, res, registryW);
743 else
744 hres = IRegistrar_ResourceUnregisterSz(registrar, module_name, res, registryW);
746 if(registrar != pReg)
747 IRegistrar_Release(registrar);
748 return hres;