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
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(atl
);
40 static LONG dll_count
;
42 /**************************************************************
43 * ATLRegistrar implementation
50 {{'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
52 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
54 {{'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
56 {{'H','K','E','Y','_','U','S','E','R','S',0},
58 {{'H','K','E','Y','_','P','E','R','F','O','R','M','A','N','C','E','_','D','A','T','A',0},
59 HKEY_PERFORMANCE_DATA
},
60 {{'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0},
62 {{'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
64 {{'H','K','C','R',0}, HKEY_CLASSES_ROOT
},
65 {{'H','K','C','U',0}, HKEY_CURRENT_USER
},
66 {{'H','K','L','M',0}, HKEY_LOCAL_MACHINE
},
67 {{'H','K','U',0}, HKEY_USERS
},
68 {{'H','K','P','D',0}, HKEY_PERFORMANCE_DATA
},
69 {{'H','K','D','D',0}, HKEY_DYN_DATA
},
70 {{'H','K','C','C',0}, HKEY_CURRENT_CONFIG
}
73 typedef struct rep_list_str
{
77 struct rep_list_str
*next
;
81 IRegistrar IRegistrar_iface
;
92 static inline Registrar
*impl_from_IRegistrar(IRegistrar
*iface
)
94 return CONTAINING_RECORD(iface
, Registrar
, IRegistrar_iface
);
97 static void strbuf_init(strbuf
*buf
)
99 buf
->str
= HeapAlloc(GetProcessHeap(), 0, 128*sizeof(WCHAR
));
104 static void strbuf_write(LPCOLESTR str
, strbuf
*buf
, int len
)
108 if(buf
->len
+len
+1 >= buf
->alloc
) {
109 buf
->alloc
= (buf
->len
+len
)<<1;
110 buf
->str
= HeapReAlloc(GetProcessHeap(), 0, buf
->str
, buf
->alloc
*sizeof(WCHAR
));
112 memcpy(buf
->str
+buf
->len
, str
, len
*sizeof(OLECHAR
));
114 buf
->str
[buf
->len
] = '\0';
117 static HRESULT
get_word(LPCOLESTR
*str
, strbuf
*buf
)
119 LPCOLESTR iter
, iter2
= *str
;
124 while(isspaceW(*iter2
))
132 if(*iter
== '}' || *iter
== '=') {
133 strbuf_write(iter
++, buf
, 1);
134 }else if(*iter
== '\'') {
136 iter
= strchrW(iter
, '\'');
138 WARN("Unexpected end of script\n");
140 return DISP_E_EXCEPTION
;
142 strbuf_write(iter2
, buf
, iter
-iter2
);
145 while(*iter
&& !isspaceW(*iter
))
147 strbuf_write(iter2
, buf
, iter
-iter2
);
150 while(isspaceW(*iter
))
156 static HRESULT
do_preprocess(const Registrar
*This
, LPCOLESTR data
, strbuf
*buf
)
158 LPCOLESTR iter
, iter2
= data
;
160 static const WCHAR wstr
[] = {'%',0};
162 iter
= strchrW(data
, '%');
164 strbuf_write(iter2
, buf
, iter
-iter2
);
168 return DISP_E_EXCEPTION
;
169 iter
= strchrW(iter2
, '%');
171 return DISP_E_EXCEPTION
;
174 strbuf_write(wstr
, buf
, 1);
176 for(rep_iter
= This
->rep
; rep_iter
; rep_iter
= rep_iter
->next
) {
177 if(rep_iter
->key_len
== iter
-iter2
178 && !memicmpW(iter2
, rep_iter
->key
, rep_iter
->key_len
))
182 WARN("Could not find replacement: %s\n", debugstr_wn(iter2
, iter
-iter2
));
183 return DISP_E_EXCEPTION
;
186 strbuf_write(rep_iter
->item
, buf
, -1);
190 iter
= strchrW(iter
, '%');
193 strbuf_write(iter2
, buf
, -1);
194 TRACE("%s\n", debugstr_w(buf
->str
));
199 static HRESULT
do_process_key(LPCOLESTR
*pstr
, HKEY parent_key
, strbuf
*buf
, BOOL do_register
)
201 LPCOLESTR iter
= *pstr
;
215 static const WCHAR wstrNoRemove
[] = {'N','o','R','e','m','o','v','e',0};
216 static const WCHAR wstrForceRemove
[] = {'F','o','r','c','e','R','e','m','o','v','e',0};
217 static const WCHAR wstrDelete
[] = {'D','e','l','e','t','e',0};
218 static const WCHAR wstrval
[] = {'v','a','l',0};
221 hres
= get_word(&iter
, buf
);
226 while(buf
->str
[1] || buf
->str
[0] != '}') {
228 if(!lstrcmpiW(buf
->str
, wstrNoRemove
))
229 key_type
= NO_REMOVE
;
230 else if(!lstrcmpiW(buf
->str
, wstrForceRemove
))
231 key_type
= FORCE_REMOVE
;
232 else if(!lstrcmpiW(buf
->str
, wstrval
))
234 else if(!lstrcmpiW(buf
->str
, wstrDelete
))
235 key_type
= DO_DELETE
;
237 if(key_type
!= NORMAL
) {
238 hres
= get_word(&iter
, buf
);
242 TRACE("name = %s\n", debugstr_w(buf
->str
));
245 if(key_type
== IS_VAL
) {
247 strbuf_write(buf
->str
, &name
, -1);
248 }else if(key_type
== DO_DELETE
) {
249 TRACE("Deleting %s\n", debugstr_w(buf
->str
));
250 RegDeleteTreeW(parent_key
, buf
->str
);
252 if(key_type
== FORCE_REMOVE
)
253 RegDeleteTreeW(parent_key
, buf
->str
);
254 lres
= RegCreateKeyW(parent_key
, buf
->str
, &hkey
);
255 if(lres
!= ERROR_SUCCESS
) {
256 WARN("Could not create(open) key: %08x\n", lres
);
257 hres
= HRESULT_FROM_WIN32(lres
);
261 }else if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
) {
262 strbuf_write(buf
->str
, &name
, -1);
263 lres
= RegOpenKeyW(parent_key
, buf
->str
, &hkey
);
264 if(lres
!= ERROR_SUCCESS
)
265 WARN("Could not open key %s: %08x\n", debugstr_w(name
.str
), lres
);
268 if(key_type
!= DO_DELETE
&& *iter
== '=') {
270 hres
= get_word(&iter
, buf
);
274 WARN("Wrong registry type: %s\n", debugstr_w(buf
->str
));
275 hres
= DISP_E_EXCEPTION
;
279 switch(buf
->str
[0]) {
281 hres
= get_word(&iter
, buf
);
284 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_SZ
, (PBYTE
)buf
->str
,
285 (lstrlenW(buf
->str
)+1)*sizeof(WCHAR
));
286 if(lres
!= ERROR_SUCCESS
) {
287 WARN("Could set value of key: %08x\n", lres
);
288 hres
= HRESULT_FROM_WIN32(lres
);
294 hres
= get_word(&iter
, buf
);
297 dw
= atoiW(buf
->str
);
298 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_DWORD
,
299 (PBYTE
)&dw
, sizeof(dw
));
300 if(lres
!= ERROR_SUCCESS
) {
301 WARN("Could set value of key: %08x\n", lres
);
302 hres
= HRESULT_FROM_WIN32(lres
);
311 hres
= get_word(&iter
, buf
);
314 count
= (lstrlenW(buf
->str
) + 1) / 2;
315 bytes
= HeapAlloc(GetProcessHeap(), 0, count
);
317 hres
= E_OUTOFMEMORY
;
320 for(i
= 0; i
< count
&& buf
->str
[2*i
]; i
++) {
322 if(!isxdigitW(buf
->str
[2*i
]) || !isxdigitW(buf
->str
[2*i
+ 1])) {
326 digits
[0] = buf
->str
[2*i
];
327 digits
[1] = buf
->str
[2*i
+ 1];
329 bytes
[i
] = (BYTE
) strtoulW(digits
, NULL
, 16);
331 if(SUCCEEDED(hres
)) {
332 lres
= RegSetValueExW(hkey
, name
.len
? name
.str
: NULL
, 0, REG_BINARY
,
334 if(lres
!= ERROR_SUCCESS
) {
335 WARN("Could not set value of key: 0x%08x\n", lres
);
336 hres
= HRESULT_FROM_WIN32(lres
);
339 HeapFree(GetProcessHeap(), 0, bytes
);
343 WARN("Wrong resource type: %s\n", debugstr_w(buf
->str
));
344 hres
= DISP_E_EXCEPTION
;
351 hres
= get_word(&iter
, buf
);
355 }else if(key_type
== IS_VAL
) {
356 WARN("value not set!\n");
357 hres
= DISP_E_EXCEPTION
;
361 if(key_type
!= IS_VAL
&& key_type
!= DO_DELETE
&& *iter
== '{' && isspaceW(iter
[1])) {
362 hres
= get_word(&iter
, buf
);
365 hres
= do_process_key(&iter
, hkey
, buf
, do_register
);
370 TRACE("%x %x\n", do_register
, key_type
);
371 if(!do_register
&& (key_type
== NORMAL
|| key_type
== FORCE_REMOVE
)) {
372 TRACE("Deleting %s\n", debugstr_w(name
.str
));
373 RegDeleteKeyW(parent_key
, name
.str
);
376 if(hkey
&& key_type
!= IS_VAL
)
381 hres
= get_word(&iter
, buf
);
386 HeapFree(GetProcessHeap(), 0, name
.str
);
387 if(hkey
&& key_type
!= IS_VAL
)
393 static HRESULT
do_process_root_key(LPCOLESTR data
, BOOL do_register
)
395 LPCOLESTR iter
= data
;
401 hres
= get_word(&iter
, &buf
);
407 WARN("ward.len == 0, failed\n");
408 hres
= DISP_E_EXCEPTION
;
411 for(i
=0; i
<sizeof(root_keys
)/sizeof(root_keys
[0]); i
++) {
412 if(!lstrcmpiW(buf
.str
, root_keys
[i
].name
))
415 if(i
== sizeof(root_keys
)/sizeof(root_keys
[0])) {
416 WARN("Wrong root key name: %s\n", debugstr_w(buf
.str
));
417 hres
= DISP_E_EXCEPTION
;
420 hres
= get_word(&iter
, &buf
);
423 if(buf
.str
[1] || buf
.str
[0] != '{') {
424 WARN("Failed, expected '{', got %s\n", debugstr_w(buf
.str
));
425 hres
= DISP_E_EXCEPTION
;
428 hres
= do_process_key(&iter
, root_keys
[i
].key
, &buf
, do_register
);
430 WARN("Processing key failed: %08x\n", hres
);
433 hres
= get_word(&iter
, &buf
);
437 HeapFree(GetProcessHeap(), 0, buf
.str
);
441 static HRESULT
string_register(Registrar
*This
, LPCOLESTR data
, BOOL do_register
)
446 TRACE("(%p %s %x)\n", This
, debugstr_w(data
), do_register
);
449 hres
= do_preprocess(This
, data
, &buf
);
451 WARN("preprocessing failed!\n");
452 HeapFree(GetProcessHeap(), 0, buf
.str
);
456 hres
= do_process_root_key(buf
.str
, do_register
);
457 if(FAILED(hres
) && do_register
)
458 do_process_root_key(buf
.str
, FALSE
);
460 HeapFree(GetProcessHeap(), 0, buf
.str
);
464 static HRESULT
resource_register(Registrar
*This
, LPCOLESTR resFileName
,
465 LPCOLESTR szID
, LPCOLESTR szType
, BOOL do_register
)
474 hins
= LoadLibraryExW(resFileName
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
476 src
= FindResourceW(hins
, szID
, szType
);
478 regstra
= LoadResource(hins
, src
);
479 reslen
= SizeofResource(hins
, src
);
481 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, NULL
, 0)+1;
482 regstrw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
483 MultiByteToWideChar(CP_ACP
, 0, regstra
, reslen
, regstrw
, len
);
484 regstrw
[len
-1] = '\0';
486 hres
= string_register(This
, regstrw
, do_register
);
488 HeapFree(GetProcessHeap(), 0, regstrw
);
490 WARN("could not load resource\n");
491 hres
= HRESULT_FROM_WIN32(GetLastError());
494 WARN("Could not find source\n");
495 hres
= HRESULT_FROM_WIN32(GetLastError());
499 WARN("Could not load resource file\n");
500 hres
= HRESULT_FROM_WIN32(GetLastError());
506 static HRESULT
file_register(Registrar
*This
, LPCOLESTR fileName
, BOOL do_register
)
515 file
= CreateFileW(fileName
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
516 if(file
!= INVALID_HANDLE_VALUE
) {
517 filelen
= GetFileSize(file
, NULL
);
518 regstra
= HeapAlloc(GetProcessHeap(), 0, filelen
);
519 lres
= ReadFile(file
, regstra
, filelen
, NULL
, NULL
);
520 if(lres
== ERROR_SUCCESS
) {
521 len
= MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, NULL
, 0)+1;
522 regstrw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
523 MultiByteToWideChar(CP_ACP
, 0, regstra
, filelen
, regstrw
, len
);
524 regstrw
[len
-1] = '\0';
526 hres
= string_register(This
, regstrw
, do_register
);
528 HeapFree(GetProcessHeap(), 0, regstrw
);
530 WARN("Failed to read faile\n");
531 hres
= HRESULT_FROM_WIN32(lres
);
533 HeapFree(GetProcessHeap(), 0, regstra
);
536 WARN("Could not open file\n");
537 hres
= HRESULT_FROM_WIN32(GetLastError());
543 static HRESULT WINAPI
Registrar_QueryInterface(IRegistrar
*iface
, REFIID riid
, void **ppvObject
)
545 TRACE("(%p)->(%s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
547 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IRegistrar
, riid
)) {
548 IRegistrar_AddRef(iface
);
552 return E_NOINTERFACE
;
555 static ULONG WINAPI
Registrar_AddRef(IRegistrar
*iface
)
557 Registrar
*This
= impl_from_IRegistrar(iface
);
558 ULONG ref
= InterlockedIncrement(&This
->ref
);
559 TRACE("(%p) ->%d\n", This
, ref
);
563 static ULONG WINAPI
Registrar_Release(IRegistrar
*iface
)
565 Registrar
*This
= impl_from_IRegistrar(iface
);
566 ULONG ref
= InterlockedDecrement(&This
->ref
);
568 TRACE("(%p) ->%d\n", This
, ref
);
570 IRegistrar_ClearReplacements(iface
);
571 HeapFree(GetProcessHeap(), 0, This
);
572 InterlockedDecrement(&dll_count
);
577 static HRESULT WINAPI
Registrar_AddReplacement(IRegistrar
*iface
, LPCOLESTR Key
, LPCOLESTR item
)
579 Registrar
*This
= impl_from_IRegistrar(iface
);
583 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(Key
), debugstr_w(item
));
585 new_rep
= HeapAlloc(GetProcessHeap(), 0, sizeof(rep_list
));
587 new_rep
->key_len
= lstrlenW(Key
);
588 new_rep
->key
= HeapAlloc(GetProcessHeap(), 0, (new_rep
->key_len
+ 1) * sizeof(OLECHAR
));
589 memcpy(new_rep
->key
, Key
, (new_rep
->key_len
+1)*sizeof(OLECHAR
));
591 len
= lstrlenW(item
)+1;
592 new_rep
->item
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(OLECHAR
));
593 memcpy(new_rep
->item
, item
, len
*sizeof(OLECHAR
));
595 new_rep
->next
= This
->rep
;
601 static HRESULT WINAPI
Registrar_ClearReplacements(IRegistrar
*iface
)
603 Registrar
*This
= impl_from_IRegistrar(iface
);
604 rep_list
*iter
, *iter2
;
606 TRACE("(%p)\n", This
);
614 HeapFree(GetProcessHeap(), 0, iter
->key
);
615 HeapFree(GetProcessHeap(), 0, iter
->item
);
616 HeapFree(GetProcessHeap(), 0, iter
);
624 static HRESULT WINAPI
Registrar_ResourceRegisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
625 LPCOLESTR szID
, LPCOLESTR szType
)
627 Registrar
*This
= impl_from_IRegistrar(iface
);
628 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
629 return resource_register(This
, resFileName
, szID
, szType
, TRUE
);
632 static HRESULT WINAPI
Registrar_ResourceUnregisterSz(IRegistrar
* iface
, LPCOLESTR resFileName
,
633 LPCOLESTR szID
, LPCOLESTR szType
)
635 Registrar
*This
= impl_from_IRegistrar(iface
);
636 TRACE("(%p)->(%s %s %s)\n", This
, debugstr_w(resFileName
), debugstr_w(szID
), debugstr_w(szType
));
637 return resource_register(This
, resFileName
, szID
, szType
, FALSE
);
640 static HRESULT WINAPI
Registrar_FileRegister(IRegistrar
* iface
, LPCOLESTR fileName
)
642 Registrar
*This
= impl_from_IRegistrar(iface
);
643 TRACE("(%p)->(%s)\n", This
, debugstr_w(fileName
));
644 return file_register(This
, fileName
, TRUE
);
647 static HRESULT WINAPI
Registrar_FileUnregister(IRegistrar
* iface
, LPCOLESTR fileName
)
649 Registrar
*This
= impl_from_IRegistrar(iface
);
650 FIXME("(%p)->(%s)\n", This
, debugstr_w(fileName
));
651 return file_register(This
, fileName
, FALSE
);
654 static HRESULT WINAPI
Registrar_StringRegister(IRegistrar
* iface
, LPCOLESTR data
)
656 Registrar
*This
= impl_from_IRegistrar(iface
);
657 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
658 return string_register(This
, data
, TRUE
);
661 static HRESULT WINAPI
Registrar_StringUnregister(IRegistrar
* iface
, LPCOLESTR data
)
663 Registrar
*This
= impl_from_IRegistrar(iface
);
664 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
665 return string_register(This
, data
, FALSE
);
668 static HRESULT WINAPI
Registrar_ResourceRegister(IRegistrar
* iface
, LPCOLESTR resFileName
,
669 UINT nID
, LPCOLESTR szType
)
671 Registrar
*This
= impl_from_IRegistrar(iface
);
672 TRACE("(%p)->(%s %d %s)\n", iface
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
673 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, TRUE
);
676 static HRESULT WINAPI
Registrar_ResourceUnregister(IRegistrar
* iface
, LPCOLESTR resFileName
,
677 UINT nID
, LPCOLESTR szType
)
679 Registrar
*This
= impl_from_IRegistrar(iface
);
680 TRACE("(%p)->(%s %d %s)\n", This
, debugstr_w(resFileName
), nID
, debugstr_w(szType
));
681 return resource_register(This
, resFileName
, MAKEINTRESOURCEW(nID
), szType
, FALSE
);
684 static const IRegistrarVtbl RegistrarVtbl
= {
685 Registrar_QueryInterface
,
688 Registrar_AddReplacement
,
689 Registrar_ClearReplacements
,
690 Registrar_ResourceRegisterSz
,
691 Registrar_ResourceUnregisterSz
,
692 Registrar_FileRegister
,
693 Registrar_FileUnregister
,
694 Registrar_StringRegister
,
695 Registrar_StringUnregister
,
696 Registrar_ResourceRegister
,
697 Registrar_ResourceUnregister
,
700 static HRESULT
Registrar_create(const IUnknown
*pUnkOuter
, REFIID riid
, void **ppvObject
)
704 if(!IsEqualGUID(&IID_IUnknown
, riid
) && !IsEqualGUID(&IID_IRegistrar
, riid
))
705 return E_NOINTERFACE
;
707 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(Registrar
));
708 ret
->IRegistrar_iface
.lpVtbl
= &RegistrarVtbl
;
713 InterlockedIncrement(&dll_count
);
718 /**************************************************************
719 * ClassFactory implementation
722 static HRESULT WINAPI
RegistrarCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppvObject
)
724 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
726 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IClassFactory
, riid
)) {
728 IClassFactory_AddRef( iface
);
732 return E_NOINTERFACE
;
735 static ULONG WINAPI
RegistrarCF_AddRef(IClassFactory
*iface
)
737 InterlockedIncrement(&dll_count
);
741 static ULONG WINAPI
RegistrarCF_Release(IClassFactory
*iface
)
743 InterlockedDecrement(&dll_count
);
747 static HRESULT WINAPI
RegistrarCF_CreateInstance(IClassFactory
*iface
, LPUNKNOWN pUnkOuter
,
748 REFIID riid
, void **ppvObject
)
750 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
751 return Registrar_create(pUnkOuter
, riid
, ppvObject
);
754 static HRESULT WINAPI
RegistrarCF_LockServer(IClassFactory
*iface
, BOOL lock
)
756 TRACE("(%p)->(%x)\n", iface
, lock
);
759 InterlockedIncrement(&dll_count
);
761 InterlockedDecrement(&dll_count
);
766 static const IClassFactoryVtbl IRegistrarCFVtbl
= {
767 RegistrarCF_QueryInterface
,
770 RegistrarCF_CreateInstance
,
771 RegistrarCF_LockServer
774 static IClassFactory RegistrarCF
= { &IRegistrarCFVtbl
};
776 /**************************************************************
777 * DllGetClassObject (ATL.2)
779 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID riid
, LPVOID
*ppvObject
)
781 TRACE("(%s %s %p)\n", debugstr_guid(clsid
), debugstr_guid(riid
), ppvObject
);
783 if(IsEqualGUID(&CLSID_Registrar
, clsid
))
784 return IClassFactory_QueryInterface( &RegistrarCF
, riid
, ppvObject
);
786 FIXME("Not supported class %s\n", debugstr_guid(clsid
));
787 return CLASS_E_CLASSNOTAVAILABLE
;
790 extern HINSTANCE hInst
;
792 /* this is a copy of the winecrt0 registration code that creates the registrar directly, */
793 /* since we can't do it through ole32 until it has been registered */
797 IRegistrar
*registrar
;
802 static IRegistrar
*create_registrar( HMODULE inst
, struct reg_info
*info
)
804 info
->result
= Registrar_create( NULL
, &IID_IRegistrar
, (void**)&info
->registrar
);
805 if (SUCCEEDED( info
->result
))
807 static const WCHAR moduleW
[] = {'M','O','D','U','L','E',0};
810 GetModuleFileNameW( hInst
, str
, MAX_PATH
);
811 IRegistrar_AddReplacement( info
->registrar
, moduleW
, str
);
813 return info
->registrar
;
816 static BOOL CALLBACK
register_resource( HMODULE module
, LPCWSTR type
, LPWSTR name
, LONG_PTR arg
)
818 struct reg_info
*info
= (struct reg_info
*)arg
;
820 HRSRC rsrc
= FindResourceW( module
, name
, type
);
821 char *str
= LoadResource( module
, rsrc
);
822 DWORD lenW
, lenA
= SizeofResource( module
, rsrc
);
824 if (!str
) return FALSE
;
825 if (!info
->registrar
&& !create_registrar( module
, info
)) return FALSE
;
826 lenW
= MultiByteToWideChar( CP_UTF8
, 0, str
, lenA
, NULL
, 0 ) + 1;
827 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, lenW
* sizeof(WCHAR
) )))
829 info
->result
= E_OUTOFMEMORY
;
832 MultiByteToWideChar( CP_UTF8
, 0, str
, lenA
, buffer
, lenW
);
833 buffer
[lenW
- 1] = 0;
835 if (info
->do_register
)
836 info
->result
= IRegistrar_StringRegister( info
->registrar
, buffer
);
838 info
->result
= IRegistrar_StringUnregister( info
->registrar
, buffer
);
840 HeapFree( GetProcessHeap(), 0, buffer
);
841 return SUCCEEDED(info
->result
);
844 static HRESULT
do_register_dll_server(IRegistrar
*pRegistrar
, LPCOLESTR wszDll
,
845 LPCOLESTR wszId
, BOOL do_register
,
846 const struct _ATL_REGMAP_ENTRY
* pMapEntries
)
848 IRegistrar
*registrar
;
850 const struct _ATL_REGMAP_ENTRY
*pMapEntry
;
852 static const WCHAR wszModule
[] = {'M','O','D','U','L','E',0};
853 static const WCHAR wszRegistry
[] = {'R','E','G','I','S','T','R','Y',0};
856 registrar
= pRegistrar
;
858 Registrar_create(NULL
, &IID_IRegistrar
, (void**)®istrar
);
860 IRegistrar_AddReplacement(registrar
, wszModule
, wszDll
);
862 for (pMapEntry
= pMapEntries
; pMapEntry
&& pMapEntry
->szKey
; pMapEntry
++)
863 IRegistrar_AddReplacement(registrar
, pMapEntry
->szKey
, pMapEntry
->szData
);
866 hres
= IRegistrar_ResourceRegisterSz(registrar
, wszDll
, wszId
, wszRegistry
);
868 hres
= IRegistrar_ResourceUnregisterSz(registrar
, wszDll
, wszId
, wszRegistry
);
870 if(registrar
!= pRegistrar
)
871 IRegistrar_Release(registrar
);
875 /***********************************************************************
876 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
879 HRESULT WINAPI
AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW
* pM
, LPCOLESTR lpszRes
,
880 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
* pMapEntries
, IRegistrar
* pReg
)
882 HINSTANCE lhInst
= pM
->m_hInst
;
883 /* everything inside this function below this point
884 * should go into atl71.AtlUpdateRegistryFromResourceD
886 WCHAR module_name
[MAX_PATH
];
888 if(!GetModuleFileNameW(lhInst
, module_name
, MAX_PATH
)) {
889 FIXME("hinst %p: did not get module name\n",
894 TRACE("%p (%s), %s, %d, %p, %p\n", hInst
, debugstr_w(module_name
),
895 debugstr_w(lpszRes
), bRegister
, pMapEntries
, pReg
);
897 return do_register_dll_server(pReg
, module_name
, lpszRes
, bRegister
, pMapEntries
);
900 static const WCHAR regtypeW
[] = {'W','I','N','E','_','R','E','G','I','S','T','R','Y',0};
902 /***********************************************************************
903 * DllRegisterServer (ATL.@)
905 HRESULT WINAPI
DllRegisterServer(void)
907 struct reg_info info
;
909 info
.registrar
= NULL
;
910 info
.do_register
= TRUE
;
912 EnumResourceNamesW( hInst
, regtypeW
, register_resource
, (LONG_PTR
)&info
);
913 if (info
.registrar
) IRegistrar_Release( info
.registrar
);
917 /***********************************************************************
918 * DllUnRegisterServer (ATL.@)
920 HRESULT WINAPI
DllUnregisterServer(void)
922 struct reg_info info
;
924 info
.registrar
= NULL
;
925 info
.do_register
= FALSE
;
927 EnumResourceNamesW( hInst
, regtypeW
, register_resource
, (LONG_PTR
)&info
);
928 if (info
.registrar
) IRegistrar_Release( info
.registrar
);
932 /***********************************************************************
933 * DllCanUnloadNow (ATL.@)
935 HRESULT WINAPI
DllCanUnloadNow(void)
937 TRACE("dll_count = %u\n", dll_count
);
938 return dll_count
? S_FALSE
: S_OK
;