push ac4b6ebdd3fd886d6a18959265755579ed195b88
[wine/hacks.git] / dlls / atl / atl_main.c
blob75f9a11116ca0b54faca0dbdf362ad7b0c429dba
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/debug.h"
32 #include "objbase.h"
33 #include "objidl.h"
34 #include "ole2.h"
35 #include "atlbase.h"
36 #include "atliface.h"
37 #include "atlwin.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(atl);
43 HINSTANCE hInst;
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);
51 hInst = hinstDLL;
53 return TRUE;
56 #define ATLVer1Size 100
58 HRESULT WINAPI AtlModuleInit(_ATL_MODULEW* pM, _ATL_OBJMAP_ENTRYW* p, HINSTANCE h)
60 INT i;
61 UINT size;
63 FIXME("SEMI-STUB (%p %p %p)\n",pM,p,h);
65 size = pM->cbSize;
66 if (size != sizeof(_ATL_MODULEW) && size != ATLVer1Size)
68 FIXME("Unknown structure version (size %i)\n",size);
69 return E_INVALIDARG;
72 memset(pM,0,pM->cbSize);
73 pM->cbSize = size;
74 pM->m_hInst = h;
75 pM->m_hInstResource = h;
76 pM->m_hInstTypeLib = h;
77 pM->m_pObjMap = p;
78 pM->m_hHeap = GetProcessHeap();
80 InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
81 InitializeCriticalSection(&pM->m_csWindowCreate);
82 InitializeCriticalSection(&pM->m_csObjMap);
84 /* call mains */
85 i = 0;
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);
93 i++;
97 return S_OK;
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;
106 else
107 ret = (_ATL_OBJMAP_ENTRYW_V1 *)(mod->m_pObjMap + index);
109 if (!ret->pclsid) ret = NULL;
110 return ret;
113 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
114 BSTR *pbstrPath, ITypeLib **ppTypeLib)
116 HRESULT hRes;
117 OLECHAR path[MAX_PATH+8]; /* leave some space for index */
119 TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
121 if (!pM)
122 return E_INVALIDARG;
124 GetModuleFileNameW(pM->m_hInstTypeLib, path, MAX_PATH);
125 if (lpszIndex)
126 lstrcatW(path, lpszIndex);
128 hRes = LoadTypeLib(path, ppTypeLib);
129 if (FAILED(hRes))
130 return hRes;
132 *pbstrPath = SysAllocString(path);
134 return S_OK;
137 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEW* pM)
139 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
141 TRACE("(%p)\n", pM);
143 while(iter) {
144 iter->pFunc(iter->dw);
145 tmp = iter;
146 iter = iter->pNext;
147 HeapFree(GetProcessHeap(), 0, tmp);
150 HeapFree(GetProcessHeap(), 0, pM);
152 return S_OK;
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;
168 return S_OK;
171 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
172 DWORD dwFlags)
174 _ATL_OBJMAP_ENTRYW_V1 *obj;
175 HRESULT hRes = S_OK;
176 int i=0;
178 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
180 if (pM == NULL)
181 return E_INVALIDARG;
183 while ((obj = get_objmap_entry( pM, i++ )))
185 IUnknown* pUnknown;
186 HRESULT rc;
188 TRACE("Registering object %i\n",i);
189 if (obj->pfnGetClassObject)
191 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
192 (LPVOID*)&pUnknown);
193 if (SUCCEEDED (rc) )
195 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
196 dwFlags, &obj->dwRegister);
197 if (pUnknown)
198 IUnknown_Release(pUnknown);
203 return hRes;
206 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
208 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
209 return S_OK;
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);
219 *pp = p;
220 return p;
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);
231 *pp = new_p;
232 return new_p;
236 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
238 int i = 0;
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);
247 return S_OK;
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))
257 TRACE("MATCH\n");
258 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
260 TRACE("Offset\n");
261 *ppvObject = ((LPSTR)this+pEntries[i].dw);
262 IUnknown_AddRef((IUnknown*)*ppvObject);
263 rc = S_OK;
265 else
267 TRACE("Function\n");
268 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
270 break;
272 i++;
274 TRACE("Done returning (0x%x)\n",rc);
275 return rc;
278 /***********************************************************************
279 * AtlModuleRegisterServer [ATL.@]
282 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
284 const _ATL_OBJMAP_ENTRYW_V1 *obj;
285 int i;
286 HRESULT hRes;
288 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
290 if (pM == NULL)
291 return E_INVALIDARG;
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 */
299 if (FAILED(hRes))
300 return hRes;
304 if (bRegTypeLib)
306 hRes = AtlModuleRegisterTypeLib(pM, NULL);
307 if (FAILED(hRes))
308 return hRes;
311 return S_OK;
314 /***********************************************************************
315 * AtlAdvise [ATL.@]
317 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
319 FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
320 return E_FAIL;
323 /***********************************************************************
324 * AtlUnadvise [ATL.@]
326 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
328 FIXME("%p %p %d\n", pUnkCP, iid, dw);
329 return S_OK;
332 /***********************************************************************
333 * AtlFreeMarshalStream [ATL.@]
335 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
337 FIXME("%p\n", stm);
338 return S_OK;
341 /***********************************************************************
342 * AtlMarshalPtrInProc [ATL.@]
344 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
346 FIXME("%p %p %p\n", pUnk, iid, pstm);
347 return E_FAIL;
350 /***********************************************************************
351 * AtlUnmarshalPtr [ATL.@]
353 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
355 FIXME("%p %p %p\n", stm, iid, ppUnk);
356 return E_FAIL;
359 /***********************************************************************
360 * AtlModuleGetClassObject [ATL.@]
362 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
363 REFIID riid, LPVOID *ppv)
365 _ATL_OBJMAP_ENTRYW_V1 *obj;
366 int i;
367 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
369 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
371 if (pm == NULL)
372 return E_INVALIDARG;
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)
381 if (!obj->pCF)
382 hres = obj->pfnGetClassObject(obj->pfnCreateInstance,
383 &IID_IUnknown,
384 (void **)&obj->pCF);
385 if (obj->pCF)
386 hres = IUnknown_QueryInterface(obj->pCF, riid, ppv);
387 break;
392 WARN("no class object found for %s\n", debugstr_guid(rclsid));
394 return hres;
397 /***********************************************************************
398 * AtlModuleGetClassObject [ATL.@]
400 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
402 HRESULT hRes;
403 BSTR path;
404 ITypeLib *typelib;
406 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
408 if (!pm)
409 return E_INVALIDARG;
411 hRes = AtlModuleLoadTypeLib(pm, lpszIndex, &path, &typelib);
413 if (SUCCEEDED(hRes))
415 hRes = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
416 ITypeLib_Release(typelib);
417 SysFreeString(path);
420 return hRes;
423 /***********************************************************************
424 * AtlModuleRevokeClassObjects [ATL.@]
426 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
428 FIXME("%p\n", pm);
429 return E_FAIL;
432 /***********************************************************************
433 * AtlModuleUnregisterServer [ATL.@]
435 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
437 FIXME("%p %s\n", pm, debugstr_guid(clsid));
438 return E_FAIL;
441 /***********************************************************************
442 * AtlModuleRegisterWndClassInfoW [ATL.@]
444 * PARAMS
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.
449 * RETURNS
450 * Atom representing the registered class.
452 * NOTES
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)
461 ATOM atom;
463 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
465 atom = wci->m_atom;
466 if (!atom)
468 WNDCLASSEXW wc;
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);
481 if (!atom)
482 atom = RegisterClassExW(&wci->m_wc);
484 wci->pWndProc = wci->m_wc.lpfnWndProc;
485 wci->m_atom = atom;
487 *pProc = wci->pWndProc;
489 TRACE("returning 0x%04x\n", atom);
490 return 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;
534 TRACE("(%p)\n", pM);
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;
545 return NULL;
548 /* FIXME: should be in a header file */
549 typedef struct ATL_PROPMAP_ENTRY
551 LPCOLESTR szDesc;
552 DISPID dispid;
553 const CLSID* pclsidPropPage;
554 const IID* piidDispatch;
555 DWORD dwOffsetData;
556 DWORD dwSizeData;
557 VARTYPE vt;
558 } ATL_PROPMAP_ENTRY;
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);
568 return S_OK;
571 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
572 ATL_PROPMAP_ENTRY *pMap, void *pThis,
573 IUnknown *pUnk)
575 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
577 return S_OK;