[ATL30] Fix m_szAutoName definition and construction in Wine's ATL30 dll.
[reactos.git] / dll / win32 / atl / atl30.c
1 /*
2 * Implementation of Active Template Library (atl.dll)
3 *
4 * Copyright 2004 Aric Stewart for CodeWeavers
5 * Copyright 2005 Jacek Caban
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "objidl.h"
27 #include "rpcproxy.h"
28 #include "wine/atlbase.h"
29 #include "wine/atlwin.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(atl);
34
35 extern HINSTANCE atl_instance;
36
37 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
38
39 HRESULT WINAPI AtlModuleInit(_ATL_MODULEW* pM, _ATL_OBJMAP_ENTRYW* p, HINSTANCE h)
40 {
41 INT i;
42 UINT size;
43
44 TRACE("(%p %p %p)\n", pM, p, h);
45
46 size = pM->cbSize;
47 switch (size)
48 {
49 case ATLVer1Size:
50 case sizeof(_ATL_MODULEW):
51 #ifdef _WIN64
52 case sizeof(_ATL_MODULEW) + sizeof(void *):
53 #endif
54 break;
55 default:
56 WARN("Unknown structure version (size %i)\n",size);
57 return E_INVALIDARG;
58 }
59
60 memset(pM,0,pM->cbSize);
61 pM->cbSize = size;
62 pM->m_hInst = h;
63 pM->m_hInstResource = h;
64 pM->m_hInstTypeLib = h;
65 pM->m_pObjMap = p;
66 pM->m_hHeap = GetProcessHeap();
67
68 InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
69 InitializeCriticalSection(&pM->m_csWindowCreate);
70 InitializeCriticalSection(&pM->m_csObjMap);
71
72 /* call mains */
73 i = 0;
74 if (pM->m_pObjMap != NULL && size > ATLVer1Size)
75 {
76 while (pM->m_pObjMap[i].pclsid != NULL)
77 {
78 TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
79 if (p[i].pfnObjectMain)
80 p[i].pfnObjectMain(TRUE);
81 i++;
82 }
83 }
84
85 return S_OK;
86 }
87
88 static _ATL_OBJMAP_ENTRYW_V1 *get_objmap_entry( _ATL_MODULEW *mod, unsigned int index )
89 {
90 _ATL_OBJMAP_ENTRYW_V1 *ret;
91
92 if (mod->cbSize == ATLVer1Size)
93 ret = (_ATL_OBJMAP_ENTRYW_V1 *)mod->m_pObjMap + index;
94 else
95 ret = (_ATL_OBJMAP_ENTRYW_V1 *)(mod->m_pObjMap + index);
96
97 if (!ret->pclsid) ret = NULL;
98 return ret;
99 }
100
101 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
102 BSTR *pbstrPath, ITypeLib **ppTypeLib)
103 {
104 TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
105
106 if (!pM)
107 return E_INVALIDARG;
108
109 return AtlLoadTypeLib(pM->m_hInstTypeLib, lpszIndex, pbstrPath, ppTypeLib);
110 }
111
112 HRESULT WINAPI AtlModuleTerm(_ATL_MODULE *pM)
113 {
114 _ATL_TERMFUNC_ELEM *iter, *tmp;
115
116 TRACE("(%p)\n", pM);
117
118 if (pM->cbSize > ATLVer1Size)
119 {
120 iter = pM->m_pTermFuncs;
121
122 while(iter) {
123 iter->pFunc(iter->dw);
124 tmp = iter;
125 iter = iter->pNext;
126 HeapFree(GetProcessHeap(), 0, tmp);
127 }
128 }
129
130 return S_OK;
131 }
132
133 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
134 DWORD dwFlags)
135 {
136 _ATL_OBJMAP_ENTRYW_V1 *obj;
137 int i=0;
138
139 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
140
141 if (pM == NULL)
142 return E_INVALIDARG;
143
144 while ((obj = get_objmap_entry( pM, i++ )))
145 {
146 IUnknown* pUnknown;
147 HRESULT rc;
148
149 TRACE("Registering object %i\n",i);
150 if (obj->pfnGetClassObject)
151 {
152 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
153 (LPVOID*)&pUnknown);
154 if (SUCCEEDED (rc) )
155 {
156 rc = CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
157 dwFlags, &obj->dwRegister);
158
159 if (FAILED (rc) )
160 WARN("Failed to register object %i: 0x%08x\n", i, rc);
161
162 if (pUnknown)
163 IUnknown_Release(pUnknown);
164 }
165 }
166 }
167
168 return S_OK;
169 }
170
171 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
172 {
173 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
174 return S_OK;
175 }
176
177 /***********************************************************************
178 * AtlModuleRegisterServer [ATL.@]
179 *
180 */
181 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
182 {
183 const _ATL_OBJMAP_ENTRYW_V1 *obj;
184 int i;
185 HRESULT hRes;
186
187 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
188
189 if (pM == NULL)
190 return E_INVALIDARG;
191
192 for (i = 0; (obj = get_objmap_entry( pM, i )) != NULL; i++) /* register CLSIDs */
193 {
194 if (!clsid || IsEqualCLSID(obj->pclsid, clsid))
195 {
196 TRACE("Registering clsid %s\n", debugstr_guid(obj->pclsid));
197 hRes = obj->pfnUpdateRegistry(TRUE); /* register */
198 if (FAILED(hRes))
199 return hRes;
200
201 if(pM->cbSize > ATLVer1Size) {
202 const struct _ATL_CATMAP_ENTRY *catmap;
203
204 catmap = ((const _ATL_OBJMAP_ENTRYW*)obj)->pfnGetCategoryMap();
205 if(catmap) {
206 hRes = AtlRegisterClassCategoriesHelper(obj->pclsid, catmap, TRUE);
207 if(FAILED(hRes))
208 return hRes;
209 }
210 }
211 }
212 }
213
214 if (bRegTypeLib)
215 {
216 hRes = AtlRegisterTypeLib(pM->m_hInstTypeLib, NULL);
217 if (FAILED(hRes))
218 return hRes;
219 }
220
221 return S_OK;
222 }
223
224 /***********************************************************************
225 * AtlModuleGetClassObject [ATL.@]
226 */
227 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
228 REFIID riid, LPVOID *ppv)
229 {
230 _ATL_OBJMAP_ENTRYW_V1 *obj;
231 int i;
232 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
233
234 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
235
236 if (pm == NULL)
237 return E_INVALIDARG;
238
239 for (i = 0; (obj = get_objmap_entry( pm, i )) != NULL; i++)
240 {
241 if (IsEqualCLSID(obj->pclsid, rclsid))
242 {
243 TRACE("found object %i\n", i);
244 if (obj->pfnGetClassObject)
245 {
246 if (!obj->pCF)
247 hres = obj->pfnGetClassObject(obj->pfnCreateInstance,
248 &IID_IUnknown,
249 (void **)&obj->pCF);
250 if (obj->pCF)
251 hres = IUnknown_QueryInterface(obj->pCF, riid, ppv);
252 break;
253 }
254 }
255 }
256
257 WARN("no class object found for %s\n", debugstr_guid(rclsid));
258
259 return hres;
260 }
261
262 /***********************************************************************
263 * AtlModuleGetClassObject [ATL.@]
264 */
265 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
266 {
267 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
268
269 if (!pm)
270 return E_INVALIDARG;
271
272 return AtlRegisterTypeLib(pm->m_hInstTypeLib, lpszIndex);
273 }
274
275 /***********************************************************************
276 * AtlModuleRevokeClassObjects [ATL.@]
277 */
278 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
279 {
280 FIXME("%p\n", pm);
281 return E_FAIL;
282 }
283
284 /***********************************************************************
285 * AtlModuleUnregisterServer [ATL.@]
286 */
287 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
288 {
289 FIXME("%p %s\n", pm, debugstr_guid(clsid));
290 return E_FAIL;
291 }
292
293 /***********************************************************************
294 * AtlModuleRegisterWndClassInfoA [ATL.@]
295 *
296 * See AtlModuleRegisterWndClassInfoW.
297 */
298 ATOM WINAPI AtlModuleRegisterWndClassInfoA(_ATL_MODULEA *pm, _ATL_WNDCLASSINFOA *wci, WNDPROC *pProc)
299 {
300 ATOM atom;
301
302 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
303
304 atom = wci->m_atom;
305 if (!atom)
306 {
307 WNDCLASSEXA wc;
308
309 TRACE("wci->m_wc.lpszClassName = %s\n", wci->m_wc.lpszClassName);
310
311 if (wci->m_lpszOrigName)
312 FIXME( "subclassing %s not implemented\n", debugstr_a(wci->m_lpszOrigName));
313
314 if (!wci->m_wc.lpszClassName)
315 {
316 #ifdef __REACTOS__
317 sprintf(wci->m_szAutoName, "ATL:%p", wci);
318 #else
319 sprintf(wci->m_szAutoName, "ATL%08lx", (UINT_PTR)wci);
320 #endif
321 TRACE("auto-generated class name %s\n", wci->m_szAutoName);
322 wci->m_wc.lpszClassName = wci->m_szAutoName;
323 }
324
325 atom = GetClassInfoExA(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
326 if (!atom)
327 {
328 wci->m_wc.hInstance = pm->m_hInst;
329 wci->m_wc.hCursor = LoadCursorA( wci->m_bSystemCursor ? NULL : pm->m_hInst,
330 wci->m_lpszCursorID );
331 atom = RegisterClassExA(&wci->m_wc);
332 }
333 wci->pWndProc = wci->m_wc.lpfnWndProc;
334 wci->m_atom = atom;
335 }
336
337 if (wci->m_lpszOrigName) *pProc = wci->pWndProc;
338
339 TRACE("returning 0x%04x\n", atom);
340 return atom;
341 }
342
343 /***********************************************************************
344 * AtlModuleRegisterWndClassInfoW [ATL.@]
345 *
346 * PARAMS
347 * pm [IO] Information about the module registering the window.
348 * wci [IO] Information about the window being registered.
349 * pProc [O] Window procedure of the registered class.
350 *
351 * RETURNS
352 * Atom representing the registered class.
353 *
354 * NOTES
355 * Can be called multiple times without error, unlike RegisterClassEx().
356 *
357 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
358 * registered, where the 'x's represent a unique value.
359 *
360 */
361 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
362 {
363 ATOM atom;
364
365 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
366
367 atom = wci->m_atom;
368 if (!atom)
369 {
370 WNDCLASSEXW wc;
371
372 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
373
374 if (wci->m_lpszOrigName)
375 FIXME( "subclassing %s not implemented\n", debugstr_w(wci->m_lpszOrigName));
376
377 if (!wci->m_wc.lpszClassName)
378 {
379 #ifdef __REACTOS__
380 static const WCHAR szFormat[] = {'A','T','L',':','%','p',0};
381 #else
382 static const WCHAR szFormat[] = {'A','T','L','%','0','8','l','x',0};
383 #endif
384 swprintf(wci->m_szAutoName, szFormat, (UINT_PTR)wci);
385 TRACE("auto-generated class name %s\n", debugstr_w(wci->m_szAutoName));
386 wci->m_wc.lpszClassName = wci->m_szAutoName;
387 }
388
389 atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
390 if (!atom)
391 {
392 wci->m_wc.hInstance = pm->m_hInst;
393 wci->m_wc.hCursor = LoadCursorW( wci->m_bSystemCursor ? NULL : pm->m_hInst,
394 wci->m_lpszCursorID );
395 atom = RegisterClassExW(&wci->m_wc);
396 }
397 wci->pWndProc = wci->m_wc.lpfnWndProc;
398 wci->m_atom = atom;
399 }
400
401 if (wci->m_lpszOrigName) *pProc = wci->pWndProc;
402
403 TRACE("returning 0x%04x\n", atom);
404 return atom;
405 }
406
407 /***********************************************************************
408 * AtlModuleAddCreateWndData [ATL.@]
409 */
410 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, void* pvObject)
411 {
412 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
413
414 pData->m_pThis = pvObject;
415 pData->m_dwThreadID = GetCurrentThreadId();
416
417 EnterCriticalSection(&pM->m_csWindowCreate);
418 pData->m_pNext = pM->m_pCreateWndList;
419 pM->m_pCreateWndList = pData;
420 LeaveCriticalSection(&pM->m_csWindowCreate);
421 }
422
423 /***********************************************************************
424 * AtlModuleExtractCreateWndData [ATL.@]
425 *
426 * NOTE: Tests show that this function extracts one of _AtlCreateWndData
427 * records from the current thread from a list
428 *
429 */
430 void* WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
431 {
432 _AtlCreateWndData **ppData;
433 void *ret = NULL;
434
435 TRACE("(%p)\n", pM);
436
437 EnterCriticalSection(&pM->m_csWindowCreate);
438
439 for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
440 {
441 if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
442 {
443 _AtlCreateWndData *pData = *ppData;
444 *ppData = pData->m_pNext;
445 ret = pData->m_pThis;
446 break;
447 }
448 }
449
450 LeaveCriticalSection(&pM->m_csWindowCreate);
451 return ret;
452 }
453
454 /***********************************************************************
455 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
456 *
457 */
458 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
459 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
460 {
461 TRACE("(%p %s %d %p %p)\n", pM, debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
462
463 return AtlUpdateRegistryFromResourceD(pM->m_hInst, lpszRes, bRegister, pMapEntries, pReg);
464 }
465
466 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
467 {
468 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
469
470 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
471 *ppvObject = iface;
472 IClassFactory_AddRef( iface );
473 return S_OK;
474 }
475
476 return E_NOINTERFACE;
477 }
478
479 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
480 {
481 return 2;
482 }
483
484 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
485 {
486 return 1;
487 }
488
489 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
490 REFIID riid, void **ppv)
491 {
492 IRegistrar *registrar;
493 HRESULT hres;
494
495 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
496
497 if(pUnkOuter) {
498 *ppv = NULL;
499 return CLASS_E_NOAGGREGATION;
500 }
501
502 hres = AtlCreateRegistrar(&registrar);
503 if(FAILED(hres))
504 return hres;
505
506 hres = IRegistrar_QueryInterface(registrar, riid, ppv);
507 IRegistrar_Release(registrar);
508 return hres;
509 }
510
511 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
512 {
513 TRACE("(%p)->(%x)\n", iface, lock);
514 return S_OK;
515 }
516
517 static const IClassFactoryVtbl IRegistrarCFVtbl = {
518 RegistrarCF_QueryInterface,
519 RegistrarCF_AddRef,
520 RegistrarCF_Release,
521 RegistrarCF_CreateInstance,
522 RegistrarCF_LockServer
523 };
524
525 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
526
527 #ifdef __REACTOS__
528 static HRESULT do_register_dll_server(IRegistrar *pRegistrar, LPCOLESTR wszDll,
529 LPCOLESTR wszId, BOOL do_register,
530 const struct _ATL_REGMAP_ENTRY* pMapEntries)
531 {
532 IRegistrar *registrar;
533 HRESULT hres;
534 const struct _ATL_REGMAP_ENTRY *pMapEntry;
535
536 static const WCHAR wszModule[] = {'M','O','D','U','L','E',0};
537 static const WCHAR wszRegistry[] = {'R','E','G','I','S','T','R','Y',0};
538
539 if(pRegistrar) {
540 registrar = pRegistrar;
541 }else {
542 hres = AtlCreateRegistrar(&registrar);
543 if(FAILED(hres))
544 return hres;
545 }
546
547 IRegistrar_AddReplacement(registrar, wszModule, wszDll);
548
549 for (pMapEntry = pMapEntries; pMapEntry && pMapEntry->szKey; pMapEntry++)
550 IRegistrar_AddReplacement(registrar, pMapEntry->szKey, pMapEntry->szData);
551
552 if(do_register)
553 hres = IRegistrar_ResourceRegisterSz(registrar, wszDll, wszId, wszRegistry);
554 else
555 hres = IRegistrar_ResourceUnregisterSz(registrar, wszDll, wszId, wszRegistry);
556
557 if(registrar != pRegistrar)
558 IRegistrar_Release(registrar);
559 return hres;
560 }
561
562 static HRESULT do_register_server(BOOL do_register)
563 {
564 static const WCHAR CLSID_RegistrarW[] =
565 {'C','L','S','I','D','_','R','e','g','i','s','t','r','a','r',0};
566 static const WCHAR atl_dllW[] = {'a','t','l','.','d','l','l',0};
567
568 WCHAR clsid_str[40];
569 const struct _ATL_REGMAP_ENTRY reg_map[] = {{CLSID_RegistrarW, clsid_str}, {NULL,NULL}};
570
571 StringFromGUID2(&CLSID_Registrar, clsid_str, sizeof(clsid_str)/sizeof(WCHAR));
572 return do_register_dll_server(NULL, atl_dllW, MAKEINTRESOURCEW(101), do_register, reg_map);
573 }
574 #endif
575
576 /**************************************************************
577 * DllGetClassObject (ATL.2)
578 */
579 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
580 {
581 TRACE("(%s %s %p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
582
583 if(IsEqualGUID(&CLSID_Registrar, clsid))
584 return IClassFactory_QueryInterface( &RegistrarCF, riid, ppvObject );
585
586 FIXME("Not supported class %s\n", debugstr_guid(clsid));
587 return CLASS_E_CLASSNOTAVAILABLE;
588 }
589
590 /***********************************************************************
591 * DllRegisterServer (ATL.@)
592 */
593 HRESULT WINAPI DllRegisterServer(void)
594 {
595 #ifdef __REACTOS__
596 /* Note: we can't use __wine_register_server here because it uses CLSID_Registrar which isn't registred yet */
597 return do_register_server(TRUE);
598 #else
599 return __wine_register_resources( atl_instance );
600 #endif
601 }
602
603 /***********************************************************************
604 * DllUnRegisterServer (ATL.@)
605 */
606 HRESULT WINAPI DllUnregisterServer(void)
607 {
608 #ifdef __REACTOS__
609 return do_register_server(FALSE);
610 #else
611 return __wine_unregister_resources( atl_instance );
612 #endif
613 }
614
615 /***********************************************************************
616 * DllCanUnloadNow (ATL.@)
617 */
618 HRESULT WINAPI DllCanUnloadNow(void)
619 {
620 return S_FALSE;
621 }