Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / atl / atl_main.c
blob374b0d26f24073d3f0f8b6abc86025abbe4baf43
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 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
101 BSTR *pbstrPath, ITypeLib **ppTypeLib)
103 HRESULT hRes;
104 OLECHAR path[MAX_PATH+8]; /* leave some space for index */
106 TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
108 if (!pM)
109 return E_INVALIDARG;
111 GetModuleFileNameW(pM->m_hInstTypeLib, path, MAX_PATH);
112 if (lpszIndex)
113 lstrcatW(path, lpszIndex);
115 hRes = LoadTypeLib(path, ppTypeLib);
116 if (FAILED(hRes))
117 return hRes;
119 *pbstrPath = SysAllocString(path);
121 return S_OK;
124 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEW* pM)
126 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
128 TRACE("(%p)\n", pM);
130 while(iter) {
131 iter->pFunc(iter->dw);
132 tmp = iter;
133 iter = iter->pNext;
134 HeapFree(GetProcessHeap(), 0, tmp);
137 HeapFree(GetProcessHeap(), 0, pM);
139 return S_OK;
142 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULEW *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
144 _ATL_TERMFUNC_ELEM *termfunc_elem;
146 TRACE("(%p %p %ld)\n", pM, pFunc, dw);
148 termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
149 termfunc_elem->pFunc = pFunc;
150 termfunc_elem->dw = dw;
151 termfunc_elem->pNext = pM->m_pTermFuncs;
153 pM->m_pTermFuncs = termfunc_elem;
155 return S_OK;
158 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
159 DWORD dwFlags)
161 HRESULT hRes = S_OK;
162 int i=0;
164 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
166 if (pM == NULL)
167 return E_INVALIDARG;
169 while(pM->m_pObjMap[i].pclsid != NULL)
171 IUnknown* pUnknown;
172 _ATL_OBJMAP_ENTRYW *obj = &(pM->m_pObjMap[i]);
173 HRESULT rc;
175 TRACE("Registering object %i\n",i);
176 if (obj->pfnGetClassObject)
178 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
179 (LPVOID*)&pUnknown);
180 if (SUCCEEDED (rc) )
182 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
183 dwFlags, &obj->dwRegister);
184 if (pUnknown)
185 IUnknown_Release(pUnknown);
188 i++;
191 return hRes;
194 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
196 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
197 return S_OK;
201 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
203 TRACE("(%p %p)\n", pp, p);
205 if (p) IUnknown_AddRef(p);
206 if (*pp) IUnknown_Release(*pp);
207 *pp = p;
208 return p;
211 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
213 IUnknown *new_p = NULL;
215 TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
217 if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
218 if (*pp) IUnknown_Release(*pp);
219 *pp = new_p;
220 return new_p;
224 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
226 int i = 0;
227 HRESULT rc = E_NOINTERFACE;
228 TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
230 if (IsEqualGUID(iid,&IID_IUnknown))
232 TRACE("Returning IUnknown\n");
233 *ppvObject = ((LPSTR)this+pEntries[0].dw);
234 IUnknown_AddRef((IUnknown*)*ppvObject);
235 return S_OK;
238 while (pEntries[i].pFunc != 0)
240 TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
241 pEntries[i].dw, pEntries[i].pFunc);
243 if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
245 TRACE("MATCH\n");
246 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
248 TRACE("Offset\n");
249 *ppvObject = ((LPSTR)this+pEntries[i].dw);
250 IUnknown_AddRef((IUnknown*)*ppvObject);
251 rc = S_OK;
253 else
255 TRACE("Function\n");
256 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
258 break;
260 i++;
262 TRACE("Done returning (0x%x)\n",rc);
263 return rc;
266 /***********************************************************************
267 * AtlModuleRegisterServer [ATL.@]
270 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
272 int i;
273 HRESULT hRes;
275 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
277 if (pM == NULL)
278 return E_INVALIDARG;
280 for (i = 0; pM->m_pObjMap[i].pclsid != NULL; i++) /* register CLSIDs */
282 if (!clsid || IsEqualCLSID(pM->m_pObjMap[i].pclsid, clsid))
284 const _ATL_OBJMAP_ENTRYW *obj = &pM->m_pObjMap[i];
286 TRACE("Registering clsid %s\n", debugstr_guid(obj->pclsid));
287 hRes = obj->pfnUpdateRegistry(TRUE); /* register */
288 if (FAILED(hRes))
289 return hRes;
293 if (bRegTypeLib)
295 hRes = AtlModuleRegisterTypeLib(pM, NULL);
296 if (FAILED(hRes))
297 return hRes;
300 return S_OK;
303 /***********************************************************************
304 * AtlAdvise [ATL.@]
306 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
308 FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
309 return E_FAIL;
312 /***********************************************************************
313 * AtlUnadvise [ATL.@]
315 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
317 FIXME("%p %p %d\n", pUnkCP, iid, dw);
318 return S_OK;
321 /***********************************************************************
322 * AtlFreeMarshalStream [ATL.@]
324 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
326 FIXME("%p\n", stm);
327 return S_OK;
330 /***********************************************************************
331 * AtlMarshalPtrInProc [ATL.@]
333 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
335 FIXME("%p %p %p\n", pUnk, iid, pstm);
336 return E_FAIL;
339 /***********************************************************************
340 * AtlUnmarshalPtr [ATL.@]
342 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
344 FIXME("%p %p %p\n", stm, iid, ppUnk);
345 return E_FAIL;
348 /***********************************************************************
349 * AtlModuleGetClassObject [ATL.@]
351 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
352 REFIID riid, LPVOID *ppv)
354 int i;
355 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
357 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
359 if (pm == NULL)
360 return E_INVALIDARG;
362 for (i = 0; pm->m_pObjMap[i].pclsid != NULL; i++)
364 if (IsEqualCLSID(pm->m_pObjMap[i].pclsid, rclsid))
366 _ATL_OBJMAP_ENTRYW *obj = &pm->m_pObjMap[i];
368 TRACE("found object %i\n", i);
369 if (obj->pfnGetClassObject)
371 if (!obj->pCF)
372 hres = obj->pfnGetClassObject(obj->pfnCreateInstance,
373 &IID_IUnknown,
374 (void **)&obj->pCF);
375 if (obj->pCF)
376 hres = IUnknown_QueryInterface(obj->pCF, riid, ppv);
377 break;
382 WARN("no class object found for %s\n", debugstr_guid(rclsid));
384 return hres;
387 /***********************************************************************
388 * AtlModuleGetClassObject [ATL.@]
390 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
392 HRESULT hRes;
393 BSTR path;
394 ITypeLib *typelib;
396 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
398 if (!pm)
399 return E_INVALIDARG;
401 hRes = AtlModuleLoadTypeLib(pm, lpszIndex, &path, &typelib);
403 if (SUCCEEDED(hRes))
405 hRes = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
406 ITypeLib_Release(typelib);
407 SysFreeString(path);
410 return hRes;
413 /***********************************************************************
414 * AtlModuleRevokeClassObjects [ATL.@]
416 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
418 FIXME("%p\n", pm);
419 return E_FAIL;
422 /***********************************************************************
423 * AtlModuleUnregisterServer [ATL.@]
425 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
427 FIXME("%p %s\n", pm, debugstr_guid(clsid));
428 return E_FAIL;
431 /***********************************************************************
432 * AtlModuleRegisterWndClassInfoW [ATL.@]
434 * PARAMS
435 * pm [IO] Information about the module registering the window.
436 * wci [IO] Information about the window being registered.
437 * pProc [O] Window procedure of the registered class.
439 * RETURNS
440 * Atom representing the registered class.
442 * NOTES
443 * Can be called multiple times without error, unlike RegisterClassEx().
445 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
446 * registered, where the 'x's represent a unique value.
449 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
451 ATOM atom;
453 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
455 atom = wci->m_atom;
456 if (!atom)
458 WNDCLASSEXW wc;
460 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
462 if (!wci->m_wc.lpszClassName)
464 static const WCHAR szFormat[] = {'A','T','L','%','0','8','x',0};
465 sprintfW(wci->m_szAutoName, szFormat, (UINT)(UINT_PTR)wci);
466 TRACE("auto-generated class name %s\n", debugstr_w(wci->m_szAutoName));
467 wci->m_wc.lpszClassName = wci->m_szAutoName;
470 atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
471 if (!atom)
472 atom = RegisterClassExW(&wci->m_wc);
474 wci->pWndProc = wci->m_wc.lpfnWndProc;
475 wci->m_atom = atom;
477 *pProc = wci->pWndProc;
479 TRACE("returning 0x%04x\n", atom);
480 return atom;
483 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
485 HDC dc = GetDC(NULL);
486 lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
487 lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
488 ReleaseDC( NULL, dc );
491 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
493 HDC dc = GetDC(NULL);
494 lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
495 lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
496 ReleaseDC( NULL, dc );
499 /***********************************************************************
500 * AtlModuleAddCreateWndData [ATL.@]
502 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, void* pvObject)
504 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
506 pData->m_pThis = pvObject;
507 pData->m_dwThreadID = GetCurrentThreadId();
508 pData->m_pNext = pM->m_pCreateWndList;
509 pM->m_pCreateWndList = pData;
512 /***********************************************************************
513 * AtlModuleExtractCreateWndData [ATL.@]
515 * NOTE: I failed to find any good description of this function.
516 * Tests show that this function extracts one of _AtlCreateWndData
517 * records from the current thread from a list
520 void* WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
522 _AtlCreateWndData **ppData;
524 TRACE("(%p)\n", pM);
526 for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
528 if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
530 _AtlCreateWndData *pData = *ppData;
531 *ppData = pData->m_pNext;
532 return pData->m_pThis;
535 return NULL;
538 /* FIXME: should be in a header file */
539 typedef struct ATL_PROPMAP_ENTRY
541 LPCOLESTR szDesc;
542 DISPID dispid;
543 const CLSID* pclsidPropPage;
544 const IID* piidDispatch;
545 DWORD dwOffsetData;
546 DWORD dwSizeData;
547 VARTYPE vt;
548 } ATL_PROPMAP_ENTRY;
550 /***********************************************************************
551 * AtlIPersistStreamInit_Load [ATL.@]
553 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
554 void *pThis, IUnknown *pUnk)
556 FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
558 return S_OK;
561 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
562 ATL_PROPMAP_ENTRY *pMap, void *pThis,
563 IUnknown *pUnk)
565 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
567 return S_OK;