mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / atl / atl30.c
blobb015ea4b74728303b69eb8045af44643f86f4bec
1 /*
2 * Implementation of Active Template Library (atl.dll)
4 * Copyright 2004 Aric Stewart for CodeWeavers
5 * Copyright 2005 Jacek Caban
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.
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.
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
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "objidl.h"
27 #include "rpcproxy.h"
28 #include "atlbase.h"
29 #include "atlwin.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(atl);
35 extern HINSTANCE atl_instance;
37 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
39 HRESULT WINAPI AtlModuleInit(_ATL_MODULEW* pM, _ATL_OBJMAP_ENTRYW* p, HINSTANCE h)
41 INT i;
42 UINT size;
44 TRACE("(%p %p %p)\n", pM, p, h);
46 size = pM->cbSize;
47 switch (size)
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;
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();
68 InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
69 InitializeCriticalSection(&pM->m_csWindowCreate);
70 InitializeCriticalSection(&pM->m_csObjMap);
72 /* call mains */
73 i = 0;
74 if (pM->m_pObjMap != NULL && size > ATLVer1Size)
76 while (pM->m_pObjMap[i].pclsid != NULL)
78 TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
79 if (p[i].pfnObjectMain)
80 p[i].pfnObjectMain(TRUE);
81 i++;
85 return S_OK;
88 static _ATL_OBJMAP_ENTRYW_V1 *get_objmap_entry( _ATL_MODULEW *mod, unsigned int index )
90 _ATL_OBJMAP_ENTRYW_V1 *ret;
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);
97 if (!ret->pclsid) ret = NULL;
98 return ret;
101 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
102 BSTR *pbstrPath, ITypeLib **ppTypeLib)
104 TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
106 if (!pM)
107 return E_INVALIDARG;
109 return AtlLoadTypeLib(pM->m_hInstTypeLib, lpszIndex, pbstrPath, ppTypeLib);
112 HRESULT WINAPI AtlModuleTerm(_ATL_MODULE *pM)
114 _ATL_TERMFUNC_ELEM *iter, *tmp;
116 TRACE("(%p)\n", pM);
118 if (pM->cbSize > ATLVer1Size)
120 iter = pM->m_pTermFuncs;
122 while(iter) {
123 iter->pFunc(iter->dw);
124 tmp = iter;
125 iter = iter->pNext;
126 HeapFree(GetProcessHeap(), 0, tmp);
130 return S_OK;
133 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
134 DWORD dwFlags)
136 _ATL_OBJMAP_ENTRYW_V1 *obj;
137 int i=0;
139 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
141 if (pM == NULL)
142 return E_INVALIDARG;
144 while ((obj = get_objmap_entry( pM, i++ )))
146 IUnknown* pUnknown;
147 HRESULT rc;
149 TRACE("Registering object %i\n",i);
150 if (obj->pfnGetClassObject)
152 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
153 (LPVOID*)&pUnknown);
154 if (SUCCEEDED (rc) )
156 rc = CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
157 dwFlags, &obj->dwRegister);
159 if (FAILED (rc) )
160 WARN("Failed to register object %i: 0x%08x\n", i, rc);
162 if (pUnknown)
163 IUnknown_Release(pUnknown);
168 return S_OK;
171 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
173 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
174 return S_OK;
177 /***********************************************************************
178 * AtlModuleRegisterServer [ATL.@]
181 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
183 const _ATL_OBJMAP_ENTRYW_V1 *obj;
184 int i;
185 HRESULT hRes;
187 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
189 if (pM == NULL)
190 return E_INVALIDARG;
192 for (i = 0; (obj = get_objmap_entry( pM, i )) != NULL; i++) /* register CLSIDs */
194 if (!clsid || IsEqualCLSID(obj->pclsid, clsid))
196 TRACE("Registering clsid %s\n", debugstr_guid(obj->pclsid));
197 hRes = obj->pfnUpdateRegistry(TRUE); /* register */
198 if (FAILED(hRes))
199 return hRes;
201 if(pM->cbSize > ATLVer1Size) {
202 const struct _ATL_CATMAP_ENTRY *catmap;
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;
214 if (bRegTypeLib)
216 hRes = AtlRegisterTypeLib(pM->m_hInstTypeLib, NULL);
217 if (FAILED(hRes))
218 return hRes;
221 return S_OK;
224 /***********************************************************************
225 * AtlModuleGetClassObject [ATL.@]
227 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
228 REFIID riid, LPVOID *ppv)
230 _ATL_OBJMAP_ENTRYW_V1 *obj;
231 int i;
232 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
234 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
236 if (pm == NULL)
237 return E_INVALIDARG;
239 for (i = 0; (obj = get_objmap_entry( pm, i )) != NULL; i++)
241 if (IsEqualCLSID(obj->pclsid, rclsid))
243 TRACE("found object %i\n", i);
244 if (obj->pfnGetClassObject)
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;
257 WARN("no class object found for %s\n", debugstr_guid(rclsid));
259 return hres;
262 /***********************************************************************
263 * AtlModuleGetClassObject [ATL.@]
265 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
267 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
269 if (!pm)
270 return E_INVALIDARG;
272 return AtlRegisterTypeLib(pm->m_hInstTypeLib, lpszIndex);
275 /***********************************************************************
276 * AtlModuleRevokeClassObjects [ATL.@]
278 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
280 FIXME("%p\n", pm);
281 return E_FAIL;
284 /***********************************************************************
285 * AtlModuleUnregisterServer [ATL.@]
287 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
289 FIXME("%p %s\n", pm, debugstr_guid(clsid));
290 return E_FAIL;
293 /***********************************************************************
294 * AtlModuleRegisterWndClassInfoA [ATL.@]
296 * See AtlModuleRegisterWndClassInfoW.
298 ATOM WINAPI AtlModuleRegisterWndClassInfoA(_ATL_MODULEA *pm, _ATL_WNDCLASSINFOA *wci, WNDPROC *pProc)
300 ATOM atom;
302 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
304 atom = wci->m_atom;
305 if (!atom)
307 WNDCLASSEXA wc;
309 TRACE("wci->m_wc.lpszClassName = %s\n", wci->m_wc.lpszClassName);
311 if (wci->m_lpszOrigName)
312 FIXME( "subclassing %s not implemented\n", debugstr_a(wci->m_lpszOrigName));
314 if (!wci->m_wc.lpszClassName)
316 sprintf(wci->m_szAutoName, "ATL:%p", wci);
317 TRACE("auto-generated class name %s\n", wci->m_szAutoName);
318 wci->m_wc.lpszClassName = wci->m_szAutoName;
321 atom = GetClassInfoExA(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
322 if (!atom)
324 wci->m_wc.hInstance = pm->m_hInst;
325 wci->m_wc.hCursor = LoadCursorA( wci->m_bSystemCursor ? NULL : pm->m_hInst,
326 wci->m_lpszCursorID );
327 atom = RegisterClassExA(&wci->m_wc);
329 wci->pWndProc = wci->m_wc.lpfnWndProc;
330 wci->m_atom = atom;
333 if (wci->m_lpszOrigName) *pProc = wci->pWndProc;
335 TRACE("returning 0x%04x\n", atom);
336 return atom;
339 /***********************************************************************
340 * AtlModuleRegisterWndClassInfoW [ATL.@]
342 * PARAMS
343 * pm [IO] Information about the module registering the window.
344 * wci [IO] Information about the window being registered.
345 * pProc [O] Window procedure of the registered class.
347 * RETURNS
348 * Atom representing the registered class.
350 * NOTES
351 * Can be called multiple times without error, unlike RegisterClassEx().
353 * If the class name is NULL, then a class with a name of "ATL:xxxxxxxx" is
354 * registered, where 'xxxxxxxx' represents a unique hexadecimal value.
357 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
359 ATOM atom;
361 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
363 atom = wci->m_atom;
364 if (!atom)
366 WNDCLASSEXW wc;
368 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
370 if (wci->m_lpszOrigName)
371 FIXME( "subclassing %s not implemented\n", debugstr_w(wci->m_lpszOrigName));
373 if (!wci->m_wc.lpszClassName)
375 swprintf(wci->m_szAutoName, ARRAY_SIZE(wci->m_szAutoName), L"ATL:%p", wci);
376 TRACE("auto-generated class name %s\n", debugstr_w(wci->m_szAutoName));
377 wci->m_wc.lpszClassName = wci->m_szAutoName;
380 atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
381 if (!atom)
383 wci->m_wc.hInstance = pm->m_hInst;
384 wci->m_wc.hCursor = LoadCursorW( wci->m_bSystemCursor ? NULL : pm->m_hInst,
385 wci->m_lpszCursorID );
386 atom = RegisterClassExW(&wci->m_wc);
388 wci->pWndProc = wci->m_wc.lpfnWndProc;
389 wci->m_atom = atom;
392 if (wci->m_lpszOrigName) *pProc = wci->pWndProc;
394 TRACE("returning 0x%04x\n", atom);
395 return atom;
398 /***********************************************************************
399 * AtlModuleAddCreateWndData [ATL.@]
401 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, void* pvObject)
403 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
405 pData->m_pThis = pvObject;
406 pData->m_dwThreadID = GetCurrentThreadId();
408 EnterCriticalSection(&pM->m_csWindowCreate);
409 pData->m_pNext = pM->m_pCreateWndList;
410 pM->m_pCreateWndList = pData;
411 LeaveCriticalSection(&pM->m_csWindowCreate);
414 /***********************************************************************
415 * AtlModuleExtractCreateWndData [ATL.@]
417 * NOTE: Tests show that this function extracts one of _AtlCreateWndData
418 * records from the current thread from a list
421 void* WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
423 _AtlCreateWndData **ppData;
424 void *ret = NULL;
426 TRACE("(%p)\n", pM);
428 EnterCriticalSection(&pM->m_csWindowCreate);
430 for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
432 if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
434 _AtlCreateWndData *pData = *ppData;
435 *ppData = pData->m_pNext;
436 ret = pData->m_pThis;
437 break;
441 LeaveCriticalSection(&pM->m_csWindowCreate);
442 return ret;
445 /***********************************************************************
446 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
449 HRESULT WINAPI AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW* pM, LPCOLESTR lpszRes,
450 BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg)
452 TRACE("(%p %s %d %p %p)\n", pM, debugstr_w(lpszRes), bRegister, pMapEntries, pReg);
454 return AtlUpdateRegistryFromResourceD(pM->m_hInst, lpszRes, bRegister, pMapEntries, pReg);
457 static HRESULT WINAPI RegistrarCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppvObject)
459 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
461 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
462 *ppvObject = iface;
463 IClassFactory_AddRef( iface );
464 return S_OK;
467 return E_NOINTERFACE;
470 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
472 return 2;
475 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
477 return 1;
480 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
481 REFIID riid, void **ppv)
483 IRegistrar *registrar;
484 HRESULT hres;
486 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
488 if(pUnkOuter) {
489 *ppv = NULL;
490 return CLASS_E_NOAGGREGATION;
493 hres = AtlCreateRegistrar(&registrar);
494 if(FAILED(hres))
495 return hres;
497 hres = IRegistrar_QueryInterface(registrar, riid, ppv);
498 IRegistrar_Release(registrar);
499 return hres;
502 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
504 TRACE("(%p)->(%x)\n", iface, lock);
505 return S_OK;
508 static const IClassFactoryVtbl IRegistrarCFVtbl = {
509 RegistrarCF_QueryInterface,
510 RegistrarCF_AddRef,
511 RegistrarCF_Release,
512 RegistrarCF_CreateInstance,
513 RegistrarCF_LockServer
516 static IClassFactory RegistrarCF = { &IRegistrarCFVtbl };
518 /**************************************************************
519 * DllGetClassObject (ATL.2)
521 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, LPVOID *ppvObject)
523 TRACE("(%s %s %p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppvObject);
525 if(IsEqualGUID(&CLSID_Registrar, clsid))
526 return IClassFactory_QueryInterface( &RegistrarCF, riid, ppvObject );
528 FIXME("Not supported class %s\n", debugstr_guid(clsid));
529 return CLASS_E_CLASSNOTAVAILABLE;
532 /***********************************************************************
533 * DllRegisterServer (ATL.@)
535 HRESULT WINAPI DllRegisterServer(void)
537 return __wine_register_resources( atl_instance );
540 /***********************************************************************
541 * DllUnRegisterServer (ATL.@)
543 HRESULT WINAPI DllUnregisterServer(void)
545 return __wine_unregister_resources( atl_instance );
548 /***********************************************************************
549 * DllCanUnloadNow (ATL.@)
551 HRESULT WINAPI DllCanUnloadNow(void)
553 return S_FALSE;