2 * Implementation of Active Template Library (atl.dll)
4 * Copyright 2004 Aric Stewart for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(atl);
45 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
47 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
49 if (fdwReason == DLL_PROCESS_ATTACH) {
50 DisableThreadLibraryCalls(hinstDLL);
56 #define ATLVer1Size 100
58 HRESULT WINAPI AtlModuleInit(_ATL_MODULEW* pM, _ATL_OBJMAP_ENTRYW* p, HINSTANCE h)
63 FIXME("SEMI-STUB (%p %p %p)\n",pM,p,h);
66 if (size != sizeof(_ATL_MODULEW) && size != ATLVer1Size)
68 FIXME("Unknown structure version (size %i)\n",size);
72 memset(pM,0,pM->cbSize);
75 pM->m_hInstResource = h;
76 pM->m_hInstTypeLib = h;
78 pM->m_hHeap = GetProcessHeap();
80 InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
81 InitializeCriticalSection(&pM->m_csWindowCreate);
82 InitializeCriticalSection(&pM->m_csObjMap);
86 if (pM->m_pObjMap != NULL && size > ATLVer1Size)
88 while (pM->m_pObjMap[i].pclsid != NULL)
90 TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
91 if (p[i].pfnObjectMain)
92 p[i].pfnObjectMain(TRUE);
100 static _ATL_OBJMAP_ENTRYW_V1 *get_objmap_entry( _ATL_MODULEW *mod, unsigned int index )
102 _ATL_OBJMAP_ENTRYW_V1 *ret;
104 if (mod->cbSize == ATLVer1Size)
105 ret = (_ATL_OBJMAP_ENTRYW_V1 *)mod->m_pObjMap + index;
107 ret = (_ATL_OBJMAP_ENTRYW_V1 *)(mod->m_pObjMap + index);
109 if (!ret->pclsid) ret = NULL;
113 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
114 BSTR *pbstrPath, ITypeLib **ppTypeLib)
117 OLECHAR path[MAX_PATH+8]; /* leave some space for index */
119 TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
124 GetModuleFileNameW(pM->m_hInstTypeLib, path, MAX_PATH);
126 lstrcatW(path, lpszIndex);
128 hRes = LoadTypeLib(path, ppTypeLib);
132 *pbstrPath = SysAllocString(path);
137 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEW* pM)
139 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
144 iter->pFunc(iter->dw);
147 HeapFree(GetProcessHeap(), 0, tmp);
150 HeapFree(GetProcessHeap(), 0, pM);
155 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULEW *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
157 _ATL_TERMFUNC_ELEM *termfunc_elem;
159 TRACE("(%p %p %ld)\n", pM, pFunc, dw);
161 termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
162 termfunc_elem->pFunc = pFunc;
163 termfunc_elem->dw = dw;
164 termfunc_elem->pNext = pM->m_pTermFuncs;
166 pM->m_pTermFuncs = termfunc_elem;
171 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
174 _ATL_OBJMAP_ENTRYW_V1 *obj;
178 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
183 while ((obj = get_objmap_entry( pM, i++ )))
188 TRACE("Registering object %i\n",i);
189 if (obj->pfnGetClassObject)
191 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
195 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
196 dwFlags, &obj->dwRegister);
198 IUnknown_Release(pUnknown);
206 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
208 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
213 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
215 TRACE("(%p %p)\n", pp, p);
217 if (p) IUnknown_AddRef(p);
218 if (*pp) IUnknown_Release(*pp);
223 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
225 IUnknown *new_p = NULL;
227 TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
229 if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
230 if (*pp) IUnknown_Release(*pp);
236 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
239 HRESULT rc = E_NOINTERFACE;
240 TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
242 if (IsEqualGUID(iid,&IID_IUnknown))
244 TRACE("Returning IUnknown\n");
245 *ppvObject = ((LPSTR)this+pEntries[0].dw);
246 IUnknown_AddRef((IUnknown*)*ppvObject);
250 while (pEntries[i].pFunc != 0)
252 TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
253 pEntries[i].dw, pEntries[i].pFunc);
255 if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
258 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
261 *ppvObject = ((LPSTR)this+pEntries[i].dw);
262 IUnknown_AddRef((IUnknown*)*ppvObject);
268 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
274 TRACE("Done returning (0x%x)\n",rc);
278 /***********************************************************************
279 * AtlModuleRegisterServer [ATL.@]
282 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
284 const _ATL_OBJMAP_ENTRYW_V1 *obj;
288 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
293 for (i = 0; (obj = get_objmap_entry( pM, i )) != NULL; i++) /* register CLSIDs */
295 if (!clsid || IsEqualCLSID(obj->pclsid, clsid))
297 TRACE("Registering clsid %s\n", debugstr_guid(obj->pclsid));
298 hRes = obj->pfnUpdateRegistry(TRUE); /* register */
306 hRes = AtlModuleRegisterTypeLib(pM, NULL);
314 /***********************************************************************
317 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
319 FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
323 /***********************************************************************
324 * AtlUnadvise [ATL.@]
326 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
328 FIXME("%p %p %d\n", pUnkCP, iid, dw);
332 /***********************************************************************
333 * AtlFreeMarshalStream [ATL.@]
335 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
341 /***********************************************************************
342 * AtlMarshalPtrInProc [ATL.@]
344 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
346 FIXME("%p %p %p\n", pUnk, iid, pstm);
350 /***********************************************************************
351 * AtlUnmarshalPtr [ATL.@]
353 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
355 FIXME("%p %p %p\n", stm, iid, ppUnk);
359 /***********************************************************************
360 * AtlModuleGetClassObject [ATL.@]
362 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
363 REFIID riid, LPVOID *ppv)
365 _ATL_OBJMAP_ENTRYW_V1 *obj;
367 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
369 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
374 for (i = 0; (obj = get_objmap_entry( pm, i )) != NULL; i++)
376 if (IsEqualCLSID(obj->pclsid, rclsid))
378 TRACE("found object %i\n", i);
379 if (obj->pfnGetClassObject)
382 hres = obj->pfnGetClassObject(obj->pfnCreateInstance,
386 hres = IUnknown_QueryInterface(obj->pCF, riid, ppv);
392 WARN("no class object found for %s\n", debugstr_guid(rclsid));
397 /***********************************************************************
398 * AtlModuleGetClassObject [ATL.@]
400 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
406 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
411 hRes = AtlModuleLoadTypeLib(pm, lpszIndex, &path, &typelib);
415 hRes = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
416 ITypeLib_Release(typelib);
423 /***********************************************************************
424 * AtlModuleRevokeClassObjects [ATL.@]
426 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
432 /***********************************************************************
433 * AtlModuleUnregisterServer [ATL.@]
435 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
437 FIXME("%p %s\n", pm, debugstr_guid(clsid));
441 /***********************************************************************
442 * AtlModuleRegisterWndClassInfoW [ATL.@]
445 * pm [IO] Information about the module registering the window.
446 * wci [IO] Information about the window being registered.
447 * pProc [O] Window procedure of the registered class.
450 * Atom representing the registered class.
453 * Can be called multiple times without error, unlike RegisterClassEx().
455 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
456 * registered, where the 'x's represent a unique value.
459 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
463 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
470 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
472 if (!wci->m_wc.lpszClassName)
474 static const WCHAR szFormat[] = {'A','T','L','%','0','8','x',0};
475 sprintfW(wci->m_szAutoName, szFormat, (UINT)(UINT_PTR)wci);
476 TRACE("auto-generated class name %s\n", debugstr_w(wci->m_szAutoName));
477 wci->m_wc.lpszClassName = wci->m_szAutoName;
480 atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
482 atom = RegisterClassExW(&wci->m_wc);
484 wci->pWndProc = wci->m_wc.lpfnWndProc;
487 *pProc = wci->pWndProc;
489 TRACE("returning 0x%04x\n", atom);
493 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
495 HDC dc = GetDC(NULL);
496 lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
497 lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
498 ReleaseDC( NULL, dc );
501 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
503 HDC dc = GetDC(NULL);
504 lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
505 lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
506 ReleaseDC( NULL, dc );
509 /***********************************************************************
510 * AtlModuleAddCreateWndData [ATL.@]
512 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, void* pvObject)
514 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
516 pData->m_pThis = pvObject;
517 pData->m_dwThreadID = GetCurrentThreadId();
518 pData->m_pNext = pM->m_pCreateWndList;
519 pM->m_pCreateWndList = pData;
522 /***********************************************************************
523 * AtlModuleExtractCreateWndData [ATL.@]
525 * NOTE: I failed to find any good description of this function.
526 * Tests show that this function extracts one of _AtlCreateWndData
527 * records from the current thread from a list
530 void* WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
532 _AtlCreateWndData **ppData;
536 for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
538 if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
540 _AtlCreateWndData *pData = *ppData;
541 *ppData = pData->m_pNext;
542 return pData->m_pThis;
548 /* FIXME: should be in a header file */
549 typedef struct ATL_PROPMAP_ENTRY
553 const CLSID* pclsidPropPage;
554 const IID* piidDispatch;
560 /***********************************************************************
561 * AtlIPersistStreamInit_Load [ATL.@]
563 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
564 void *pThis, IUnknown *pUnk)
566 FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
571 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
572 ATL_PROPMAP_ENTRY *pMap, void *pThis,
575 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);