75f9a11116ca0b54faca0dbdf362ad7b0c429dba
[reactos.git] / reactos / dll / win32 / 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 static _ATL_OBJMAP_ENTRYW_V1 *get_objmap_entry( _ATL_MODULEW *mod, unsigned int index )
101 {
102 _ATL_OBJMAP_ENTRYW_V1 *ret;
103
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);
108
109 if (!ret->pclsid) ret = NULL;
110 return ret;
111 }
112
113 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEW *pM, LPCOLESTR lpszIndex,
114 BSTR *pbstrPath, ITypeLib **ppTypeLib)
115 {
116 HRESULT hRes;
117 OLECHAR path[MAX_PATH+8]; /* leave some space for index */
118
119 TRACE("(%p, %s, %p, %p)\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
120
121 if (!pM)
122 return E_INVALIDARG;
123
124 GetModuleFileNameW(pM->m_hInstTypeLib, path, MAX_PATH);
125 if (lpszIndex)
126 lstrcatW(path, lpszIndex);
127
128 hRes = LoadTypeLib(path, ppTypeLib);
129 if (FAILED(hRes))
130 return hRes;
131
132 *pbstrPath = SysAllocString(path);
133
134 return S_OK;
135 }
136
137 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEW* pM)
138 {
139 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
140
141 TRACE("(%p)\n", pM);
142
143 while(iter) {
144 iter->pFunc(iter->dw);
145 tmp = iter;
146 iter = iter->pNext;
147 HeapFree(GetProcessHeap(), 0, tmp);
148 }
149
150 HeapFree(GetProcessHeap(), 0, pM);
151
152 return S_OK;
153 }
154
155 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULEW *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
156 {
157 _ATL_TERMFUNC_ELEM *termfunc_elem;
158
159 TRACE("(%p %p %ld)\n", pM, pFunc, dw);
160
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;
165
166 pM->m_pTermFuncs = termfunc_elem;
167
168 return S_OK;
169 }
170
171 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
172 DWORD dwFlags)
173 {
174 _ATL_OBJMAP_ENTRYW_V1 *obj;
175 HRESULT hRes = S_OK;
176 int i=0;
177
178 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
179
180 if (pM == NULL)
181 return E_INVALIDARG;
182
183 while ((obj = get_objmap_entry( pM, i++ )))
184 {
185 IUnknown* pUnknown;
186 HRESULT rc;
187
188 TRACE("Registering object %i\n",i);
189 if (obj->pfnGetClassObject)
190 {
191 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
192 (LPVOID*)&pUnknown);
193 if (SUCCEEDED (rc) )
194 {
195 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
196 dwFlags, &obj->dwRegister);
197 if (pUnknown)
198 IUnknown_Release(pUnknown);
199 }
200 }
201 }
202
203 return hRes;
204 }
205
206 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
207 {
208 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
209 return S_OK;
210 }
211
212
213 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
214 {
215 TRACE("(%p %p)\n", pp, p);
216
217 if (p) IUnknown_AddRef(p);
218 if (*pp) IUnknown_Release(*pp);
219 *pp = p;
220 return p;
221 }
222
223 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
224 {
225 IUnknown *new_p = NULL;
226
227 TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
228
229 if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
230 if (*pp) IUnknown_Release(*pp);
231 *pp = new_p;
232 return new_p;
233 }
234
235
236 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
237 {
238 int i = 0;
239 HRESULT rc = E_NOINTERFACE;
240 TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
241
242 if (IsEqualGUID(iid,&IID_IUnknown))
243 {
244 TRACE("Returning IUnknown\n");
245 *ppvObject = ((LPSTR)this+pEntries[0].dw);
246 IUnknown_AddRef((IUnknown*)*ppvObject);
247 return S_OK;
248 }
249
250 while (pEntries[i].pFunc != 0)
251 {
252 TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
253 pEntries[i].dw, pEntries[i].pFunc);
254
255 if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
256 {
257 TRACE("MATCH\n");
258 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
259 {
260 TRACE("Offset\n");
261 *ppvObject = ((LPSTR)this+pEntries[i].dw);
262 IUnknown_AddRef((IUnknown*)*ppvObject);
263 rc = S_OK;
264 }
265 else
266 {
267 TRACE("Function\n");
268 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
269 }
270 break;
271 }
272 i++;
273 }
274 TRACE("Done returning (0x%x)\n",rc);
275 return rc;
276 }
277
278 /***********************************************************************
279 * AtlModuleRegisterServer [ATL.@]
280 *
281 */
282 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
283 {
284 const _ATL_OBJMAP_ENTRYW_V1 *obj;
285 int i;
286 HRESULT hRes;
287
288 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
289
290 if (pM == NULL)
291 return E_INVALIDARG;
292
293 for (i = 0; (obj = get_objmap_entry( pM, i )) != NULL; i++) /* register CLSIDs */
294 {
295 if (!clsid || IsEqualCLSID(obj->pclsid, clsid))
296 {
297 TRACE("Registering clsid %s\n", debugstr_guid(obj->pclsid));
298 hRes = obj->pfnUpdateRegistry(TRUE); /* register */
299 if (FAILED(hRes))
300 return hRes;
301 }
302 }
303
304 if (bRegTypeLib)
305 {
306 hRes = AtlModuleRegisterTypeLib(pM, NULL);
307 if (FAILED(hRes))
308 return hRes;
309 }
310
311 return S_OK;
312 }
313
314 /***********************************************************************
315 * AtlAdvise [ATL.@]
316 */
317 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
318 {
319 FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
320 return E_FAIL;
321 }
322
323 /***********************************************************************
324 * AtlUnadvise [ATL.@]
325 */
326 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
327 {
328 FIXME("%p %p %d\n", pUnkCP, iid, dw);
329 return S_OK;
330 }
331
332 /***********************************************************************
333 * AtlFreeMarshalStream [ATL.@]
334 */
335 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
336 {
337 FIXME("%p\n", stm);
338 return S_OK;
339 }
340
341 /***********************************************************************
342 * AtlMarshalPtrInProc [ATL.@]
343 */
344 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
345 {
346 FIXME("%p %p %p\n", pUnk, iid, pstm);
347 return E_FAIL;
348 }
349
350 /***********************************************************************
351 * AtlUnmarshalPtr [ATL.@]
352 */
353 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
354 {
355 FIXME("%p %p %p\n", stm, iid, ppUnk);
356 return E_FAIL;
357 }
358
359 /***********************************************************************
360 * AtlModuleGetClassObject [ATL.@]
361 */
362 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
363 REFIID riid, LPVOID *ppv)
364 {
365 _ATL_OBJMAP_ENTRYW_V1 *obj;
366 int i;
367 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
368
369 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
370
371 if (pm == NULL)
372 return E_INVALIDARG;
373
374 for (i = 0; (obj = get_objmap_entry( pm, i )) != NULL; i++)
375 {
376 if (IsEqualCLSID(obj->pclsid, rclsid))
377 {
378 TRACE("found object %i\n", i);
379 if (obj->pfnGetClassObject)
380 {
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;
388 }
389 }
390 }
391
392 WARN("no class object found for %s\n", debugstr_guid(rclsid));
393
394 return hres;
395 }
396
397 /***********************************************************************
398 * AtlModuleGetClassObject [ATL.@]
399 */
400 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
401 {
402 HRESULT hRes;
403 BSTR path;
404 ITypeLib *typelib;
405
406 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
407
408 if (!pm)
409 return E_INVALIDARG;
410
411 hRes = AtlModuleLoadTypeLib(pm, lpszIndex, &path, &typelib);
412
413 if (SUCCEEDED(hRes))
414 {
415 hRes = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
416 ITypeLib_Release(typelib);
417 SysFreeString(path);
418 }
419
420 return hRes;
421 }
422
423 /***********************************************************************
424 * AtlModuleRevokeClassObjects [ATL.@]
425 */
426 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
427 {
428 FIXME("%p\n", pm);
429 return E_FAIL;
430 }
431
432 /***********************************************************************
433 * AtlModuleUnregisterServer [ATL.@]
434 */
435 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
436 {
437 FIXME("%p %s\n", pm, debugstr_guid(clsid));
438 return E_FAIL;
439 }
440
441 /***********************************************************************
442 * AtlModuleRegisterWndClassInfoW [ATL.@]
443 *
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.
448 *
449 * RETURNS
450 * Atom representing the registered class.
451 *
452 * NOTES
453 * Can be called multiple times without error, unlike RegisterClassEx().
454 *
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.
457 *
458 */
459 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
460 {
461 ATOM atom;
462
463 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
464
465 atom = wci->m_atom;
466 if (!atom)
467 {
468 WNDCLASSEXW wc;
469
470 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
471
472 if (!wci->m_wc.lpszClassName)
473 {
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;
478 }
479
480 atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
481 if (!atom)
482 atom = RegisterClassExW(&wci->m_wc);
483
484 wci->pWndProc = wci->m_wc.lpfnWndProc;
485 wci->m_atom = atom;
486 }
487 *pProc = wci->pWndProc;
488
489 TRACE("returning 0x%04x\n", atom);
490 return atom;
491 }
492
493 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
494 {
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 );
499 }
500
501 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
502 {
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 );
507 }
508
509 /***********************************************************************
510 * AtlModuleAddCreateWndData [ATL.@]
511 */
512 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, void* pvObject)
513 {
514 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
515
516 pData->m_pThis = pvObject;
517 pData->m_dwThreadID = GetCurrentThreadId();
518 pData->m_pNext = pM->m_pCreateWndList;
519 pM->m_pCreateWndList = pData;
520 }
521
522 /***********************************************************************
523 * AtlModuleExtractCreateWndData [ATL.@]
524 *
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
528 *
529 */
530 void* WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
531 {
532 _AtlCreateWndData **ppData;
533
534 TRACE("(%p)\n", pM);
535
536 for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
537 {
538 if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
539 {
540 _AtlCreateWndData *pData = *ppData;
541 *ppData = pData->m_pNext;
542 return pData->m_pThis;
543 }
544 }
545 return NULL;
546 }
547
548 /* FIXME: should be in a header file */
549 typedef struct ATL_PROPMAP_ENTRY
550 {
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;
559
560 /***********************************************************************
561 * AtlIPersistStreamInit_Load [ATL.@]
562 */
563 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
564 void *pThis, IUnknown *pUnk)
565 {
566 FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
567
568 return S_OK;
569 }
570
571 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
572 ATL_PROPMAP_ENTRY *pMap, void *pThis,
573 IUnknown *pUnk)
574 {
575 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
576
577 return S_OK;
578 }