hhctrl.ocx: Add a missing Release call.
[wine/multimedia.git] / dlls / atl / registrar.c
blob4fcf28d4e65af99ab191a3a94415ec8ea867caf5
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
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "oaidl.h"
30 #include "rpcproxy.h"
31 #include "atliface.h"
32 #include "atlbase.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(atl);
39 static LONG dll_count;
41 /**************************************************************
42 * ATLRegistrar implementation
45 static const struct {
46 WCHAR name[22];
47 HKEY key;
48 } root_keys[] = {
49 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
50 HKEY_CLASSES_ROOT},
51 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
52 HKEY_CURRENT_USER},
53 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
54 HKEY_LOCAL_MACHINE},
55 {{'H','K','E','Y','_','U','S','E','R','S',0},
56 HKEY_USERS},
57 {{'H','K','E','Y','_','P','E','R','F','O','R','M','A','N','C','E','_','D','A','T','A',0},
58 HKEY_PERFORMANCE_DATA},
59 {{'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0},
60 HKEY_DYN_DATA},
61 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
62 HKEY_CURRENT_CONFIG},
63 {{'H','K','C','R',0}, HKEY_CLASSES_ROOT},
64 {{'H','K','C','U',0}, HKEY_CURRENT_USER},
65 {{'H','K','L','M',0}, HKEY_LOCAL_MACHINE},
66 {{'H','K','U',0}, HKEY_USERS},
67 {{'H','K','P','D',0}, HKEY_PERFORMANCE_DATA},
68 {{'H','K','D','D',0}, HKEY_DYN_DATA},
69 {{'H','K','C','C',0}, HKEY_CURRENT_CONFIG}
72 typedef struct rep_list_str {
73 LPOLESTR key;
74 LPOLESTR item;
75 int key_len;
76 struct rep_list_str *next;
77 } rep_list;
79 typedef struct {
80 IRegistrar IRegistrar_iface;
81 LONG ref;
82 rep_list *rep;
83 } Registrar;
85 typedef struct {
86 LPOLESTR str;
87 DWORD alloc;
88 DWORD len;
89 } strbuf;
91 static inline Registrar *impl_from_IRegistrar(IRegistrar *iface)
93 return CONTAINING_RECORD(iface, Registrar, IRegistrar_iface);
96 static void strbuf_init(strbuf *buf)
98 buf->str = HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR));
99 buf->alloc = 128;
100 buf->len = 0;
103 static void strbuf_write(LPCOLESTR str, strbuf *buf, int len)
105 if(len == -1)
106 len = lstrlenW(str);
107 if(buf->len+len+1 >= buf->alloc) {
108 buf->alloc = (buf->len+len)<<1;
109 buf->str = HeapReAlloc(GetProcessHeap(), 0, buf->str, buf->alloc*sizeof(WCHAR));
111 memcpy(buf->str+buf->len, str, len*sizeof(OLECHAR));
112 buf->len += len;
113 buf->str[buf->len] = '\0';
116 static HRESULT get_word(LPCOLESTR *str, strbuf *buf)
118 LPCOLESTR iter, iter2 = *str;
120 buf->len = 0;
121 buf->str[0] = '\0';
123 while(isspaceW(*iter2))
124 iter2++;
125 iter = iter2;
126 if(!*iter) {
127 *str = iter;
128 return S_OK;
131 if(*iter == '}' || *iter == '=') {
132 strbuf_write(iter++, buf, 1);
133 }else if(*iter == '\'') {
134 iter2 = ++iter;
135 iter = strchrW(iter, '\'');
136 if(!iter) {
137 WARN("Unexpected end of script\n");
138 *str = iter;
139 return DISP_E_EXCEPTION;
141 strbuf_write(iter2, buf, iter-iter2);
142 iter++;
143 }else {
144 while(*iter && !isspaceW(*iter))
145 iter++;
146 strbuf_write(iter2, buf, iter-iter2);
149 while(isspaceW(*iter))
150 iter++;
151 *str = iter;
152 return S_OK;
155 static HRESULT do_preprocess(const Registrar *This, LPCOLESTR data, strbuf *buf)
157 LPCOLESTR iter, iter2 = data;
158 rep_list *rep_iter;
159 static const WCHAR wstr[] = {'%',0};
161 iter = strchrW(data, '%');
162 while(iter) {
163 strbuf_write(iter2, buf, iter-iter2);
165 iter2 = ++iter;
166 if(!*iter2)
167 return DISP_E_EXCEPTION;
168 iter = strchrW(iter2, '%');
169 if(!iter)
170 return DISP_E_EXCEPTION;
172 if(iter == iter2) {
173 strbuf_write(wstr, buf, 1);
174 }else {
175 for(rep_iter = This->rep; rep_iter; rep_iter = rep_iter->next) {
176 if(rep_iter->key_len == iter-iter2
177 && !memicmpW(iter2, rep_iter->key, rep_iter->key_len))
178 break;
180 if(!rep_iter) {
181 WARN("Could not find replacement: %s\n", debugstr_wn(iter2, iter-iter2));
182 return DISP_E_EXCEPTION;
185 strbuf_write(rep_iter->item, buf, -1);
188 iter2 = ++iter;
189 iter = strchrW(iter, '%');
192 strbuf_write(iter2, buf, -1);
193 TRACE("%s\n", debugstr_w(buf->str));
195 return S_OK;
198 static HRESULT do_process_key(LPCOLESTR *pstr, HKEY parent_key, strbuf *buf, BOOL do_register)
200 LPCOLESTR iter = *pstr;
201 HRESULT hres;
202 LONG lres;
203 HKEY hkey = 0;
204 strbuf name;
206 enum {
207 NORMAL,
208 NO_REMOVE,
209 IS_VAL,
210 FORCE_REMOVE,
211 DO_DELETE
212 } key_type = NORMAL;
214 static const WCHAR wstrNoRemove[] = {'N','o','R','e','m','o','v','e',0};
215 static const WCHAR wstrForceRemove[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
216 static const WCHAR wstrDelete[] = {'D','e','l','e','t','e',0};
217 static const WCHAR wstrval[] = {'v','a','l',0};
219 iter = *pstr;
220 hres = get_word(&iter, buf);
221 if(FAILED(hres))
222 return hres;
223 strbuf_init(&name);
225 while(buf->str[1] || buf->str[0] != '}') {
226 key_type = NORMAL;
227 if(!lstrcmpiW(buf->str, wstrNoRemove))
228 key_type = NO_REMOVE;
229 else if(!lstrcmpiW(buf->str, wstrForceRemove))
230 key_type = FORCE_REMOVE;
231 else if(!lstrcmpiW(buf->str, wstrval))
232 key_type = IS_VAL;
233 else if(!lstrcmpiW(buf->str, wstrDelete))
234 key_type = DO_DELETE;
236 if(key_type != NORMAL) {
237 hres = get_word(&iter, buf);
238 if(FAILED(hres))
239 break;
241 TRACE("name = %s\n", debugstr_w(buf->str));
243 if(do_register) {
244 if(key_type == IS_VAL) {
245 hkey = parent_key;
246 strbuf_write(buf->str, &name, -1);
247 }else if(key_type == DO_DELETE) {
248 TRACE("Deleting %s\n", debugstr_w(buf->str));
249 RegDeleteTreeW(parent_key, buf->str);
250 }else {
251 if(key_type == FORCE_REMOVE)
252 RegDeleteTreeW(parent_key, buf->str);
253 lres = RegCreateKeyW(parent_key, buf->str, &hkey);
254 if(lres != ERROR_SUCCESS) {
255 WARN("Could not create(open) key: %08x\n", lres);
256 hres = HRESULT_FROM_WIN32(lres);
257 break;
260 }else if(key_type != IS_VAL && key_type != DO_DELETE) {
261 strbuf_write(buf->str, &name, -1);
262 lres = RegOpenKeyW(parent_key, buf->str, &hkey);
263 if(lres != ERROR_SUCCESS)
264 WARN("Could not open key %s: %08x\n", debugstr_w(name.str), lres);
267 if(key_type != DO_DELETE && *iter == '=') {
268 iter++;
269 hres = get_word(&iter, buf);
270 if(FAILED(hres))
271 break;
272 if(buf->len != 1) {
273 WARN("Wrong registry type: %s\n", debugstr_w(buf->str));
274 hres = DISP_E_EXCEPTION;
275 break;
277 if(do_register) {
278 switch(buf->str[0]) {
279 case 's':
280 hres = get_word(&iter, buf);
281 if(FAILED(hres))
282 break;
283 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_SZ, (PBYTE)buf->str,
284 (lstrlenW(buf->str)+1)*sizeof(WCHAR));
285 if(lres != ERROR_SUCCESS) {
286 WARN("Could set value of key: %08x\n", lres);
287 hres = HRESULT_FROM_WIN32(lres);
288 break;
290 break;
291 case 'd': {
292 DWORD dw;
293 hres = get_word(&iter, buf);
294 if(FAILED(hres))
295 break;
296 dw = atoiW(buf->str);
297 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_DWORD,
298 (PBYTE)&dw, sizeof(dw));
299 if(lres != ERROR_SUCCESS) {
300 WARN("Could set value of key: %08x\n", lres);
301 hres = HRESULT_FROM_WIN32(lres);
302 break;
304 break;
306 case 'b': {
307 BYTE *bytes;
308 DWORD count;
309 DWORD i;
310 hres = get_word(&iter, buf);
311 if(FAILED(hres))
312 break;
313 count = (lstrlenW(buf->str) + 1) / 2;
314 bytes = HeapAlloc(GetProcessHeap(), 0, count);
315 if(bytes == NULL) {
316 hres = E_OUTOFMEMORY;
317 break;
319 for(i = 0; i < count && buf->str[2*i]; i++) {
320 WCHAR digits[3];
321 if(!isxdigitW(buf->str[2*i]) || !isxdigitW(buf->str[2*i + 1])) {
322 hres = E_FAIL;
323 break;
325 digits[0] = buf->str[2*i];
326 digits[1] = buf->str[2*i + 1];
327 digits[2] = 0;
328 bytes[i] = (BYTE) strtoulW(digits, NULL, 16);
330 if(SUCCEEDED(hres)) {
331 lres = RegSetValueExW(hkey, name.len ? name.str : NULL, 0, REG_BINARY,
332 bytes, count);
333 if(lres != ERROR_SUCCESS) {
334 WARN("Could not set value of key: 0x%08x\n", lres);
335 hres = HRESULT_FROM_WIN32(lres);
338 HeapFree(GetProcessHeap(), 0, bytes);
339 break;
341 default:
342 WARN("Wrong resource type: %s\n", debugstr_w(buf->str));
343 hres = DISP_E_EXCEPTION;
345 if(FAILED(hres))
346 break;
347 }else {
348 if(*iter == '-')
349 iter++;
350 hres = get_word(&iter, buf);
351 if(FAILED(hres))
352 break;
354 }else if(key_type == IS_VAL) {
355 WARN("value not set!\n");
356 hres = DISP_E_EXCEPTION;
357 break;
360 if(key_type != IS_VAL && key_type != DO_DELETE && *iter == '{' && isspaceW(iter[1])) {
361 hres = get_word(&iter, buf);
362 if(FAILED(hres))
363 break;
364 hres = do_process_key(&iter, hkey, buf, do_register);
365 if(FAILED(hres))
366 break;
369 TRACE("%x %x\n", do_register, key_type);
370 if(!do_register && (key_type == NORMAL || key_type == FORCE_REMOVE)) {
371 TRACE("Deleting %s\n", debugstr_w(name.str));
372 RegDeleteKeyW(parent_key, name.str);
375 if(hkey && key_type != IS_VAL)
376 RegCloseKey(hkey);
377 hkey = 0;
378 name.len = 0;
380 hres = get_word(&iter, buf);
381 if(FAILED(hres))
382 break;
385 HeapFree(GetProcessHeap(), 0, name.str);
386 if(hkey && key_type != IS_VAL)
387 RegCloseKey(hkey);
388 *pstr = iter;
389 return hres;
392 static HRESULT do_process_root_key(LPCOLESTR data, BOOL do_register)
394 LPCOLESTR iter = data;
395 strbuf buf;
396 HRESULT hres = S_OK;
397 unsigned int i;
399 strbuf_init(&buf);
400 hres = get_word(&iter, &buf);
401 if(FAILED(hres))
402 return hres;
404 while(*iter) {
405 if(!buf.len) {
406 WARN("ward.len == 0, failed\n");
407 hres = DISP_E_EXCEPTION;
408 break;
410 for(i=0; i<sizeof(root_keys)/sizeof(root_keys[0]); i++) {
411 if(!lstrcmpiW(buf.str, root_keys[i].name))
412 break;
414 if(i == sizeof(root_keys)/sizeof(root_keys[0])) {
415 WARN("Wrong root key name: %s\n", debugstr_w(buf.str));
416 hres = DISP_E_EXCEPTION;
417 break;
419 hres = get_word(&iter, &buf);
420 if(FAILED(hres))
421 break;
422 if(buf.str[1] || buf.str[0] != '{') {
423 WARN("Failed, expected '{', got %s\n", debugstr_w(buf.str));
424 hres = DISP_E_EXCEPTION;
425 break;
427 hres = do_process_key(&iter, root_keys[i].key, &buf, do_register);
428 if(FAILED(hres)) {
429 WARN("Processing key failed: %08x\n", hres);
430 break;
432 hres = get_word(&iter, &buf);
433 if(FAILED(hres))
434 break;
436 HeapFree(GetProcessHeap(), 0, buf.str);
437 return hres;
440 static HRESULT string_register(Registrar *This, LPCOLESTR data, BOOL do_register)
442 strbuf buf;
443 HRESULT hres;
445 TRACE("(%p %s %x)\n", This, debugstr_w(data), do_register);
447 strbuf_init(&buf);
448 hres = do_preprocess(This, data, &buf);
449 if(FAILED(hres)) {
450 WARN("preprocessing failed!\n");
451 HeapFree(GetProcessHeap(), 0, buf.str);
452 return hres;
455 hres = do_process_root_key(buf.str, do_register);
456 if(FAILED(hres) && do_register)
457 do_process_root_key(buf.str, FALSE);
459 HeapFree(GetProcessHeap(), 0, buf.str);
460 return hres;
463 static HRESULT resource_register(Registrar *This, LPCOLESTR resFileName,
464 LPCOLESTR szID, LPCOLESTR szType, BOOL do_register)
466 HINSTANCE hins;
467 HRSRC src;
468 LPSTR regstra;
469 LPWSTR regstrw;
470 DWORD len, reslen;
471 HRESULT hres;
473 hins = LoadLibraryExW(resFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
474 if(hins) {
475 src = FindResourceW(hins, szID, szType);
476 if(src) {
477 regstra = LoadResource(hins, src);
478 reslen = SizeofResource(hins, src);
479 if(regstra) {
480 len = MultiByteToWideChar(CP_ACP, 0, regstra, reslen, NULL, 0)+1;
481 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
482 MultiByteToWideChar(CP_ACP, 0, regstra, reslen, regstrw, len);
483 regstrw[len-1] = '\0';
485 hres = string_register(This, regstrw, do_register);
487 HeapFree(GetProcessHeap(), 0, regstrw);
488 }else {
489 WARN("could not load resource\n");
490 hres = HRESULT_FROM_WIN32(GetLastError());
492 }else {
493 WARN("Could not find source\n");
494 hres = HRESULT_FROM_WIN32(GetLastError());
496 FreeLibrary(hins);
497 }else {
498 WARN("Could not load resource file\n");
499 hres = HRESULT_FROM_WIN32(GetLastError());
502 return hres;
505 static HRESULT file_register(Registrar *This, LPCOLESTR fileName, BOOL do_register)
507 HANDLE file;
508 DWORD filelen, len;
509 LPWSTR regstrw;
510 LPSTR regstra;
511 LRESULT lres;
512 HRESULT hres;
514 file = CreateFileW(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
515 if(file != INVALID_HANDLE_VALUE) {
516 filelen = GetFileSize(file, NULL);
517 regstra = HeapAlloc(GetProcessHeap(), 0, filelen);
518 lres = ReadFile(file, regstra, filelen, NULL, NULL);
519 if(lres == ERROR_SUCCESS) {
520 len = MultiByteToWideChar(CP_ACP, 0, regstra, filelen, NULL, 0)+1;
521 regstrw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
522 MultiByteToWideChar(CP_ACP, 0, regstra, filelen, regstrw, len);
523 regstrw[len-1] = '\0';
525 hres = string_register(This, regstrw, do_register);
527 HeapFree(GetProcessHeap(), 0, regstrw);
528 }else {
529 WARN("Failed to read faile\n");
530 hres = HRESULT_FROM_WIN32(lres);
532 HeapFree(GetProcessHeap(), 0, regstra);
533 CloseHandle(file);
534 }else {
535 WARN("Could not open file\n");
536 hres = HRESULT_FROM_WIN32(GetLastError());
539 return hres;
542 static HRESULT WINAPI Registrar_QueryInterface(IRegistrar *iface, REFIID riid, void **ppvObject)
544 TRACE("(%p)->(%s %p\n", iface, debugstr_guid(riid), ppvObject);
546 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRegistrar, riid)) {
547 IRegistrar_AddRef(iface);
548 *ppvObject = iface;
549 return S_OK;
551 return E_NOINTERFACE;
554 static ULONG WINAPI Registrar_AddRef(IRegistrar *iface)
556 Registrar *This = impl_from_IRegistrar(iface);
557 ULONG ref = InterlockedIncrement(&This->ref);
558 TRACE("(%p) ->%d\n", This, ref);
559 return ref;
562 static ULONG WINAPI Registrar_Release(IRegistrar *iface)
564 Registrar *This = impl_from_IRegistrar(iface);
565 ULONG ref = InterlockedDecrement(&This->ref);
567 TRACE("(%p) ->%d\n", This, ref);
568 if(!ref) {
569 IRegistrar_ClearReplacements(iface);
570 HeapFree(GetProcessHeap(), 0, This);
571 InterlockedDecrement(&dll_count);
573 return ref;
576 static HRESULT WINAPI Registrar_AddReplacement(IRegistrar *iface, LPCOLESTR Key, LPCOLESTR item)
578 Registrar *This = impl_from_IRegistrar(iface);
579 int len;
580 rep_list *new_rep;
582 TRACE("(%p)->(%s %s)\n", This, debugstr_w(Key), debugstr_w(item));
584 new_rep = HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list));
586 new_rep->key_len = lstrlenW(Key);
587 new_rep->key = HeapAlloc(GetProcessHeap(), 0, (new_rep->key_len + 1) * sizeof(OLECHAR));
588 memcpy(new_rep->key, Key, (new_rep->key_len+1)*sizeof(OLECHAR));
590 len = lstrlenW(item)+1;
591 new_rep->item = HeapAlloc(GetProcessHeap(), 0, len*sizeof(OLECHAR));
592 memcpy(new_rep->item, item, len*sizeof(OLECHAR));
594 new_rep->next = This->rep;
595 This->rep = new_rep;
597 return S_OK;
600 static HRESULT WINAPI Registrar_ClearReplacements(IRegistrar *iface)
602 Registrar *This = impl_from_IRegistrar(iface);
603 rep_list *iter, *iter2;
605 TRACE("(%p)\n", This);
607 if(!This->rep)
608 return S_OK;
610 iter = This->rep;
611 while(iter) {
612 iter2 = iter->next;
613 HeapFree(GetProcessHeap(), 0, iter->key);
614 HeapFree(GetProcessHeap(), 0, iter->item);
615 HeapFree(GetProcessHeap(), 0, iter);
616 iter = iter2;
619 This->rep = NULL;
620 return S_OK;
623 static HRESULT WINAPI Registrar_ResourceRegisterSz(IRegistrar* iface, LPCOLESTR resFileName,
624 LPCOLESTR szID, LPCOLESTR szType)
626 Registrar *This = impl_from_IRegistrar(iface);
627 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
628 return resource_register(This, resFileName, szID, szType, TRUE);
631 static HRESULT WINAPI Registrar_ResourceUnregisterSz(IRegistrar* iface, LPCOLESTR resFileName,
632 LPCOLESTR szID, LPCOLESTR szType)
634 Registrar *This = impl_from_IRegistrar(iface);
635 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(resFileName), debugstr_w(szID), debugstr_w(szType));
636 return resource_register(This, resFileName, szID, szType, FALSE);
639 static HRESULT WINAPI Registrar_FileRegister(IRegistrar* iface, LPCOLESTR fileName)
641 Registrar *This = impl_from_IRegistrar(iface);
642 TRACE("(%p)->(%s)\n", This, debugstr_w(fileName));
643 return file_register(This, fileName, TRUE);
646 static HRESULT WINAPI Registrar_FileUnregister(IRegistrar* iface, LPCOLESTR fileName)
648 Registrar *This = impl_from_IRegistrar(iface);
649 FIXME("(%p)->(%s)\n", This, debugstr_w(fileName));
650 return file_register(This, fileName, FALSE);
653 static HRESULT WINAPI Registrar_StringRegister(IRegistrar* iface, LPCOLESTR data)
655 Registrar *This = impl_from_IRegistrar(iface);
656 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
657 return string_register(This, data, TRUE);
660 static HRESULT WINAPI Registrar_StringUnregister(IRegistrar* iface, LPCOLESTR data)
662 Registrar *This = impl_from_IRegistrar(iface);
663 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
664 return string_register(This, data, FALSE);
667 static HRESULT WINAPI Registrar_ResourceRegister(IRegistrar* iface, LPCOLESTR resFileName,
668 UINT nID, LPCOLESTR szType)
670 Registrar *This = impl_from_IRegistrar(iface);
671 TRACE("(%p)->(%s %d %s)\n", iface, debugstr_w(resFileName), nID, debugstr_w(szType));
672 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, TRUE);
675 static HRESULT WINAPI Registrar_ResourceUnregister(IRegistrar* iface, LPCOLESTR resFileName,
676 UINT nID, LPCOLESTR szType)
678 Registrar *This = impl_from_IRegistrar(iface);
679 TRACE("(%p)->(%s %d %s)\n", This, debugstr_w(resFileName), nID, debugstr_w(szType));
680 return resource_register(This, resFileName, MAKEINTRESOURCEW(nID), szType, FALSE);
683 static const IRegistrarVtbl RegistrarVtbl = {
684 Registrar_QueryInterface,
685 Registrar_AddRef,
686 Registrar_Release,
687 Registrar_AddReplacement,
688 Registrar_ClearReplacements,
689 Registrar_ResourceRegisterSz,
690 Registrar_ResourceUnregisterSz,
691 Registrar_FileRegister,
692 Registrar_FileUnregister,
693 Registrar_StringRegister,
694 Registrar_StringUnregister,
695 Registrar_ResourceRegister,
696 Registrar_ResourceUnregister,
699 static HRESULT Registrar_create(const IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
701 Registrar *ret;
703 if(!IsEqualGUID(&IID_IUnknown, riid) && !IsEqualGUID(&IID_IRegistrar, riid))
704 return E_NOINTERFACE;
706 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar));
707 ret->IRegistrar_iface.lpVtbl = &RegistrarVtbl;
708 ret->ref = 1;
709 ret->rep = NULL;
710 *ppvObject = ret;
712 InterlockedIncrement(&dll_count);
714 return S_OK;
717 /**************************************************************
718 * ClassFactory implementation
721 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
723 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
725 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
726 *ppvObject = iface;
727 IClassFactory_AddRef( iface );
728 return S_OK;
731 return E_NOINTERFACE;
734 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
736 InterlockedIncrement(&dll_count);
737 return 2;
740 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
742 InterlockedDecrement(&dll_count);
743 return 1;
746 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
747 REFIID riid, void **ppvObject)
749 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
750 return Registrar_create(pUnkOuter, riid, ppvObject);
753 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
755 TRACE("(%p)->(%x)\n", iface, lock);
757 if(lock)
758 InterlockedIncrement(&dll_count);
759 else
760 InterlockedDecrement(&dll_count);
762 return S_OK;
765 static const IClassFactoryVtbl IRegistrarCFVtbl = {
766 RegistrarCF_QueryInterface,
767 RegistrarCF_AddRef,
768 RegistrarCF_Release,
769 RegistrarCF_CreateInstance,
770 RegistrarCF_LockServer
773 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
775 /**************************************************************
776 * DllGetClassObject (ATL.2)
778 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
780 TRACE("(%s %s %p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
782 if(IsEqualGUID(&CLSID_Registrar, clsid))
783 return IClassFactory_QueryInterface( &RegistrarCF, riid, ppvObject );
785 FIXME("Not supported class %s\n", debugstr_guid(clsid));
786 return CLASS_E_CLASSNOTAVAILABLE;
789 extern HINSTANCE hInst;
791 static HRESULT do_register_dll_server(IRegistrar *pRegistrar, LPCOLESTR wszDll,
792 LPCOLESTR wszId, BOOL do_register,
793 const struct _ATL_REGMAP_ENTRY* pMapEntries)
795 IRegistrar *registrar;
796 HRESULT hres;
797 const struct _ATL_REGMAP_ENTRY *pMapEntry;
799 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
800 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
802 if (pRegistrar)
803 registrar = pRegistrar;
804 else
805 Registrar_create(NULL, &IID_IRegistrar, (void**)&registrar);
807 IRegistrar_AddReplacement(registrar, wszModule, wszDll);
809 for (pMapEntry = pMapEntries; pMapEntry && pMapEntry->szKey; pMapEntry++)
810 IRegistrar_AddReplacement(registrar, pMapEntry->szKey, pMapEntry->szData);
812 if(do_register)
813 hres = IRegistrar_ResourceRegisterSz(registrar, wszDll, wszId, wszRegistry);
814 else
815 hres = IRegistrar_ResourceUnregisterSz(registrar, wszDll, wszId, wszRegistry);
817 if(registrar != pRegistrar)
818 IRegistrar_Release(registrar);
819 return hres;
822 /***********************************************************************
823 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
826 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
827 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
829 HINSTANCE lhInst = pM->m_hInst;
830 /* everything inside this function below this point
831 * should go into atl71.AtlUpdateRegistryFromResourceD
833 WCHAR module_name[MAX_PATH];
835 if(!GetModuleFileNameW(lhInst, module_name, MAX_PATH)) {
836 FIXME("hinst %p: did not get module name\n",
837 lhInst);
838 return E_FAIL;
841 TRACE("%p (%s), %s, %d, %p, %p\n", hInst, debugstr_w(module_name),
842 debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
844 return do_register_dll_server(pReg, module_name, lpszRes, bRegister, pMapEntries);
847 /***********************************************************************
848 * DllRegisterServer (ATL.@)
850 HRESULT WINAPI DllRegisterServer(void)
852 return __wine_register_resources( hInst );
855 /***********************************************************************
856 * DllUnRegisterServer (ATL.@)
858 HRESULT WINAPI DllUnregisterServer(void)
860 return __wine_unregister_resources( hInst );
863 /***********************************************************************
864 * DllCanUnloadNow (ATL.@)
866 HRESULT WINAPI DllCanUnloadNow(void)
868 TRACE("dll_count = %u\n", dll_count);
869 return dll_count ? S_FALSE : S_OK;