atl: AtlModuleGetClassObject should store an intermediate pointer in obj->pCF and...
[wine] / dlls / atl / atl_main.c
1 /*
2  * Implementation of Active Template Library (atl.dll)
3  *
4  * Copyright 2004 Aric Stewart for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
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"
38
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(atl);
42
43 HINSTANCE hInst;
44
45 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
46 {
47     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
48
49     if (fdwReason == DLL_PROCESS_ATTACH) {
50         DisableThreadLibraryCalls(hinstDLL);
51         hInst = hinstDLL;
52     }
53     return TRUE;
54 }
55
56 #define ATLVer1Size 100
57
58 HRESULT WINAPI AtlModuleInit(_ATL_MODULEW* pM, _ATL_OBJMAP_ENTRYW* p, HINSTANCE h)
59 {
60     INT i;
61     UINT size;
62
63     FIXME("SEMI-STUB (%p %p %p)\n",pM,p,h);
64
65     size = pM->cbSize;
66     if  (size != sizeof(_ATL_MODULEW) && size != ATLVer1Size)
67     {
68         FIXME("Unknown structure version (size %i)\n",size);
69         return E_INVALIDARG;
70     }
71
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();
79
80     InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
81     InitializeCriticalSection(&pM->m_csWindowCreate);
82     InitializeCriticalSection(&pM->m_csObjMap);
83
84     /* call mains */
85     i = 0;
86     if (pM->m_pObjMap != NULL  && size > ATLVer1Size)
87     {
88         while (pM->m_pObjMap[i].pclsid != NULL)
89         {
90             TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
91             if (p[i].pfnObjectMain)
92                 p[i].pfnObjectMain(TRUE);
93             i++;
94         }
95     }
96
97     return S_OK;
98 }
99
100 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
101                                     BSTR *pbstrPath, ITypeLib **ppTypeLib)
102 {
103     HRESULT hRes;
104     OLECHAR path[MAX_PATH+8]; /* leave some space for index */
105
106     TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
107
108     if (!pM)
109         return E_INVALIDARG;
110
111     GetModuleFileNameW(pM->m_hInstTypeLib, path, MAX_PATH);
112     if (lpszIndex)
113         lstrcatW(path, lpszIndex);
114
115     hRes = LoadTypeLib(path, ppTypeLib);
116     if (FAILED(hRes))
117         return hRes;
118
119     *pbstrPath = SysAllocString(path);
120
121     return S_OK;
122 }
123
124 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEW* pM)
125 {
126     _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
127
128     TRACE("(%p)\n", pM);
129
130     while(iter) {
131         iter->pFunc(iter->dw);
132         tmp = iter;
133         iter = iter->pNext;
134         HeapFree(GetProcessHeap(), 0, tmp);
135     }
136
137     HeapFree(GetProcessHeap(), 0, pM);
138
139     return S_OK;
140 }
141
142 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULEW *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
143 {
144     _ATL_TERMFUNC_ELEM *termfunc_elem;
145
146     TRACE("(%p %p %ld)\n", pM, pFunc, dw);
147
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;
152
153     pM->m_pTermFuncs = termfunc_elem;
154
155     return S_OK;
156 }
157
158 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
159                                              DWORD dwFlags)
160 {
161     HRESULT hRes = S_OK;
162     int i=0;
163
164     TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
165
166     if (pM == NULL)
167         return E_INVALIDARG;
168
169     while(pM->m_pObjMap[i].pclsid != NULL)
170     {
171         IUnknown* pUnknown;
172         _ATL_OBJMAP_ENTRYW *obj = &(pM->m_pObjMap[i]);
173         HRESULT rc;
174
175         TRACE("Registering object %i\n",i);
176         if (obj->pfnGetClassObject)
177         {
178             rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
179                                    (LPVOID*)&pUnknown);
180             if (SUCCEEDED (rc) )
181             {
182                 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
183                                       dwFlags, &obj->dwRegister);
184                 if (pUnknown)
185                     IUnknown_Release(pUnknown);
186             }
187         }
188         i++;
189     }
190
191    return hRes;
192 }
193
194 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
195 {
196     FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
197     return S_OK;
198 }
199
200
201 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
202 {
203     TRACE("(%p %p)\n", pp, p);
204
205     if (p) IUnknown_AddRef(p);
206     if (*pp) IUnknown_Release(*pp);
207     *pp = p;
208     return p;
209 }
210
211
212 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries,  REFIID iid, void** ppvObject)
213 {
214     int i = 0;
215     HRESULT rc = E_NOINTERFACE;
216     TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
217
218     if (IsEqualGUID(iid,&IID_IUnknown))
219     {
220         TRACE("Returning IUnknown\n");
221         *ppvObject = ((LPSTR)this+pEntries[0].dw);
222         IUnknown_AddRef((IUnknown*)*ppvObject);
223         return S_OK;
224     }
225
226     while (pEntries[i].pFunc != 0)
227     {
228         TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
229               pEntries[i].dw, pEntries[i].pFunc);
230
231         if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
232         {
233             TRACE("MATCH\n");
234             if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
235             {
236                 TRACE("Offset\n");
237                 *ppvObject = ((LPSTR)this+pEntries[i].dw);
238                 IUnknown_AddRef((IUnknown*)*ppvObject);
239                 rc = S_OK;
240             }
241             else
242             {
243                 TRACE("Function\n");
244                 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
245             }
246             break;
247         }
248         i++;
249     }
250     TRACE("Done returning (0x%x)\n",rc);
251     return rc;
252 }
253
254 /***********************************************************************
255  *           AtlModuleRegisterServer         [ATL.@]
256  *
257  */
258 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
259 {
260     int i;
261     HRESULT hRes;
262
263     TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
264
265     if (pM == NULL)
266         return E_INVALIDARG;
267
268     for (i = 0; pM->m_pObjMap[i].pclsid != NULL; i++) /* register CLSIDs */
269     {
270         if (!clsid || IsEqualCLSID(pM->m_pObjMap[i].pclsid, clsid))
271         {
272             const _ATL_OBJMAP_ENTRYW *obj = &pM->m_pObjMap[i];
273
274             TRACE("Registering clsid %s\n", debugstr_guid(obj->pclsid));
275             hRes = obj->pfnUpdateRegistry(TRUE); /* register */
276             if (FAILED(hRes))
277                 return hRes;
278         }
279     }
280
281     if (bRegTypeLib)
282     {
283         hRes = AtlModuleRegisterTypeLib(pM, NULL);
284         if (FAILED(hRes))
285             return hRes;
286     }
287
288     return S_OK;
289 }
290
291 /***********************************************************************
292  *           AtlAdvise         [ATL.@]
293  */
294 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
295 {
296     FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
297     return E_FAIL;
298 }
299
300 /***********************************************************************
301  *           AtlUnadvise         [ATL.@]
302  */
303 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
304 {
305     FIXME("%p %p %d\n", pUnkCP, iid, dw);
306     return S_OK;
307 }
308
309 /***********************************************************************
310  *           AtlFreeMarshalStream         [ATL.@]
311  */
312 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
313 {
314     FIXME("%p\n", stm);
315     return S_OK;
316 }
317
318 /***********************************************************************
319  *           AtlMarshalPtrInProc         [ATL.@]
320  */
321 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
322 {
323     FIXME("%p %p %p\n", pUnk, iid, pstm);
324     return E_FAIL;
325 }
326
327 /***********************************************************************
328  *           AtlUnmarshalPtr              [ATL.@]
329  */
330 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
331 {
332     FIXME("%p %p %p\n", stm, iid, ppUnk);
333     return E_FAIL;
334 }
335
336 /***********************************************************************
337  *           AtlModuleGetClassObject              [ATL.@]
338  */
339 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
340                                        REFIID riid, LPVOID *ppv)
341 {
342     int i;
343     HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
344
345     TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
346
347     if (pm == NULL)
348         return E_INVALIDARG;
349
350     for (i = 0; pm->m_pObjMap[i].pclsid != NULL; i++)
351     {
352         if (IsEqualCLSID(pm->m_pObjMap[i].pclsid, rclsid))
353         {
354             _ATL_OBJMAP_ENTRYW *obj = &pm->m_pObjMap[i];
355
356             TRACE("found object %i\n", i);
357             if (obj->pfnGetClassObject)
358             {
359                 if (!obj->pCF)
360                     hres = obj->pfnGetClassObject(obj->pfnCreateInstance,
361                                                   &IID_IUnknown,
362                                                   (void **)&obj->pCF);
363                 if (obj->pCF)
364                     hres = IUnknown_QueryInterface(obj->pCF, riid, ppv);
365                 break;
366             }
367         }
368     }
369
370     WARN("no class object found for %s\n", debugstr_guid(rclsid));
371
372     return hres;
373 }
374
375 /***********************************************************************
376  *           AtlModuleGetClassObject              [ATL.@]
377  */
378 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
379 {
380     HRESULT hRes;
381     BSTR path;
382     ITypeLib *typelib;
383
384     TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
385
386     if (!pm)
387         return E_INVALIDARG;
388
389     hRes = AtlModuleLoadTypeLib(pm, lpszIndex, &path, &typelib);
390
391     if (SUCCEEDED(hRes))
392     {
393         hRes = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
394         ITypeLib_Release(typelib);
395         SysFreeString(path);
396     }
397
398     return hRes;
399 }
400
401 /***********************************************************************
402  *           AtlModuleRevokeClassObjects          [ATL.@]
403  */
404 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
405 {
406     FIXME("%p\n", pm);
407     return E_FAIL;
408 }
409
410 /***********************************************************************
411  *           AtlModuleUnregisterServer           [ATL.@]
412  */
413 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
414 {
415     FIXME("%p %s\n", pm, debugstr_guid(clsid));
416     return E_FAIL;
417 }
418
419 /***********************************************************************
420  *           AtlModuleRegisterWndClassInfoW           [ATL.@]
421  *
422  * PARAMS
423  *  pm   [IO] Information about the module registering the window.
424  *  wci  [IO] Information about the window being registered.
425  *  pProc [O] Window procedure of the registered class.
426  *
427  * RETURNS
428  *  Atom representing the registered class.
429  *
430  * NOTES
431  *  Can be called multiple times without error, unlike RegisterClassEx().
432  *
433  *  If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
434  *  registered, where the 'x's represent a unique value.
435  *
436  */
437 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
438 {
439     ATOM atom;
440
441     FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
442
443     atom = wci->m_atom;
444     if (!atom)
445     {
446         WNDCLASSEXW wc;
447
448         TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
449
450         if (!wci->m_wc.lpszClassName)
451         {
452             static const WCHAR szFormat[] = {'A','T','L','%','0','8','x',0};
453             sprintfW(wci->m_szAutoName, szFormat, (UINT)(UINT_PTR)wci);
454             TRACE("auto-generated class name %s\n", debugstr_w(wci->m_szAutoName));
455             wci->m_wc.lpszClassName = wci->m_szAutoName;
456         }
457
458         atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
459         if (!atom)
460             atom = RegisterClassExW(&wci->m_wc);
461
462         wci->pWndProc = wci->m_wc.lpfnWndProc;
463         wci->m_atom = atom;
464     }
465     *pProc = wci->pWndProc;
466
467     TRACE("returning 0x%04x\n", atom);
468     return atom;
469 }
470
471 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
472 {
473     HDC dc = GetDC(NULL);
474     lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
475     lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
476     ReleaseDC( NULL, dc );
477 }
478
479 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
480 {
481     HDC dc = GetDC(NULL);
482     lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
483     lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
484     ReleaseDC( NULL, dc );
485 }
486
487 /***********************************************************************
488  *           AtlModuleAddCreateWndData          [ATL.@]
489  */
490 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, void* pvObject)
491 {
492     TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
493
494     pData->m_pThis = pvObject;
495     pData->m_dwThreadID = GetCurrentThreadId();
496     pData->m_pNext = pM->m_pCreateWndList;
497     pM->m_pCreateWndList = pData;
498 }
499
500 /***********************************************************************
501  *           AtlModuleExtractCreateWndData      [ATL.@]
502  *
503  *  NOTE: I failed to find any good description of this function.
504  *        Tests show that this function extracts one of _AtlCreateWndData
505  *        records from the current thread from a list
506  *
507  */
508 void* WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
509 {
510     _AtlCreateWndData **ppData;
511
512     TRACE("(%p)\n", pM);
513
514     for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
515     {
516         if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
517         {
518             _AtlCreateWndData *pData = *ppData;
519             *ppData = pData->m_pNext;
520             return pData->m_pThis;
521         }
522     }
523     return NULL;
524 }