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 FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
58 HRESULT WINAPI
AtlModuleInit(_ATL_MODULEW
* pM
, _ATL_OBJMAP_ENTRYW
* p
, HINSTANCE h
)
63 FIXME("SEMI-STUB (%p %p %p)\n",pM
,p
,h
);
69 case sizeof(_ATL_MODULEW
):
71 case sizeof(_ATL_MODULEW
) + sizeof(void *):
75 WARN("Unknown structure version (size %i)\n",size
);
79 memset(pM
,0,pM
->cbSize
);
82 pM
->m_hInstResource
= h
;
83 pM
->m_hInstTypeLib
= h
;
85 pM
->m_hHeap
= GetProcessHeap();
87 InitializeCriticalSection(&pM
->u
.m_csTypeInfoHolder
);
88 InitializeCriticalSection(&pM
->m_csWindowCreate
);
89 InitializeCriticalSection(&pM
->m_csObjMap
);
93 if (pM
->m_pObjMap
!= NULL
&& size
> ATLVer1Size
)
95 while (pM
->m_pObjMap
[i
].pclsid
!= NULL
)
97 TRACE("Initializing object %i %p\n",i
,p
[i
].pfnObjectMain
);
98 if (p
[i
].pfnObjectMain
)
99 p
[i
].pfnObjectMain(TRUE
);
107 static _ATL_OBJMAP_ENTRYW_V1
*get_objmap_entry( _ATL_MODULEW
*mod
, unsigned int index
)
109 _ATL_OBJMAP_ENTRYW_V1
*ret
;
111 if (mod
->cbSize
== ATLVer1Size
)
112 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)mod
->m_pObjMap
+ index
;
114 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)(mod
->m_pObjMap
+ index
);
116 if (!ret
->pclsid
) ret
= NULL
;
120 HRESULT WINAPI
AtlModuleLoadTypeLib(_ATL_MODULEW
*pM
, LPCOLESTR lpszIndex
,
121 BSTR
*pbstrPath
, ITypeLib
**ppTypeLib
)
124 OLECHAR path
[MAX_PATH
+8]; /* leave some space for index */
126 TRACE("(%p, %s, %p, %p)\n", pM
, debugstr_w(lpszIndex
), pbstrPath
, ppTypeLib
);
131 GetModuleFileNameW(pM
->m_hInstTypeLib
, path
, MAX_PATH
);
133 lstrcatW(path
, lpszIndex
);
135 hRes
= LoadTypeLib(path
, ppTypeLib
);
139 *pbstrPath
= SysAllocString(path
);
144 HRESULT WINAPI
AtlModuleTerm(_ATL_MODULEW
* pM
)
146 _ATL_TERMFUNC_ELEM
*iter
= pM
->m_pTermFuncs
, *tmp
;
151 iter
->pFunc(iter
->dw
);
154 HeapFree(GetProcessHeap(), 0, tmp
);
157 HeapFree(GetProcessHeap(), 0, pM
);
162 HRESULT WINAPI
AtlModuleAddTermFunc(_ATL_MODULEW
*pM
, _ATL_TERMFUNC
*pFunc
, DWORD_PTR dw
)
164 _ATL_TERMFUNC_ELEM
*termfunc_elem
;
166 TRACE("(%p %p %ld)\n", pM
, pFunc
, dw
);
168 termfunc_elem
= HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM
));
169 termfunc_elem
->pFunc
= pFunc
;
170 termfunc_elem
->dw
= dw
;
171 termfunc_elem
->pNext
= pM
->m_pTermFuncs
;
173 pM
->m_pTermFuncs
= termfunc_elem
;
178 HRESULT WINAPI
AtlModuleRegisterClassObjects(_ATL_MODULEW
*pM
, DWORD dwClsContext
,
181 _ATL_OBJMAP_ENTRYW_V1
*obj
;
185 TRACE("(%p %i %i)\n",pM
, dwClsContext
, dwFlags
);
190 while ((obj
= get_objmap_entry( pM
, i
++ )))
195 TRACE("Registering object %i\n",i
);
196 if (obj
->pfnGetClassObject
)
198 rc
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
, &IID_IUnknown
,
202 CoRegisterClassObject(obj
->pclsid
, pUnknown
, dwClsContext
,
203 dwFlags
, &obj
->dwRegister
);
205 IUnknown_Release(pUnknown
);
213 HRESULT WINAPI
AtlModuleUnregisterServerEx(_ATL_MODULEW
* pM
, BOOL bUnRegTypeLib
, const CLSID
* pCLSID
)
215 FIXME("(%p, %i, %p) stub\n", pM
, bUnRegTypeLib
, pCLSID
);
220 IUnknown
* WINAPI
AtlComPtrAssign(IUnknown
** pp
, IUnknown
*p
)
222 TRACE("(%p %p)\n", pp
, p
);
224 if (p
) IUnknown_AddRef(p
);
225 if (*pp
) IUnknown_Release(*pp
);
230 IUnknown
* WINAPI
AtlComQIPtrAssign(IUnknown
** pp
, IUnknown
*p
, REFIID riid
)
232 IUnknown
*new_p
= NULL
;
234 TRACE("(%p %p %s)\n", pp
, p
, debugstr_guid(riid
));
236 if (p
) IUnknown_QueryInterface(p
, riid
, (void **)&new_p
);
237 if (*pp
) IUnknown_Release(*pp
);
243 HRESULT WINAPI
AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY
* pEntries
, REFIID iid
, void** ppvObject
)
246 HRESULT rc
= E_NOINTERFACE
;
247 TRACE("(%p, %p, %s, %p)\n",this, pEntries
, debugstr_guid(iid
), ppvObject
);
249 if (IsEqualGUID(iid
,&IID_IUnknown
))
251 TRACE("Returning IUnknown\n");
252 *ppvObject
= ((LPSTR
)this+pEntries
[0].dw
);
253 IUnknown_AddRef((IUnknown
*)*ppvObject
);
257 while (pEntries
[i
].pFunc
!= 0)
259 TRACE("Trying entry %i (%s %i %p)\n",i
,debugstr_guid(pEntries
[i
].piid
),
260 pEntries
[i
].dw
, pEntries
[i
].pFunc
);
262 if (pEntries
[i
].piid
&& IsEqualGUID(iid
,pEntries
[i
].piid
))
265 if (pEntries
[i
].pFunc
== (_ATL_CREATORARGFUNC
*)1)
268 *ppvObject
= ((LPSTR
)this+pEntries
[i
].dw
);
269 IUnknown_AddRef((IUnknown
*)*ppvObject
);
275 rc
= pEntries
[i
].pFunc(this, iid
, ppvObject
, pEntries
[i
].dw
);
281 TRACE("Done returning (0x%x)\n",rc
);
285 /***********************************************************************
286 * AtlModuleRegisterServer [ATL.@]
289 HRESULT WINAPI
AtlModuleRegisterServer(_ATL_MODULEW
* pM
, BOOL bRegTypeLib
, const CLSID
* clsid
)
291 const _ATL_OBJMAP_ENTRYW_V1
*obj
;
295 TRACE("%p %d %s\n", pM
, bRegTypeLib
, debugstr_guid(clsid
));
300 for (i
= 0; (obj
= get_objmap_entry( pM
, i
)) != NULL
; i
++) /* register CLSIDs */
302 if (!clsid
|| IsEqualCLSID(obj
->pclsid
, clsid
))
304 TRACE("Registering clsid %s\n", debugstr_guid(obj
->pclsid
));
305 hRes
= obj
->pfnUpdateRegistry(TRUE
); /* register */
313 hRes
= AtlModuleRegisterTypeLib(pM
, NULL
);
321 /***********************************************************************
324 HRESULT WINAPI
AtlAdvise(IUnknown
*pUnkCP
, IUnknown
*pUnk
, const IID
*iid
, LPDWORD pdw
)
326 FIXME("%p %p %p %p\n", pUnkCP
, pUnk
, iid
, pdw
);
330 /***********************************************************************
331 * AtlUnadvise [ATL.@]
333 HRESULT WINAPI
AtlUnadvise(IUnknown
*pUnkCP
, const IID
*iid
, DWORD dw
)
335 FIXME("%p %p %d\n", pUnkCP
, iid
, dw
);
339 /***********************************************************************
340 * AtlFreeMarshalStream [ATL.@]
342 HRESULT WINAPI
AtlFreeMarshalStream(IStream
*stm
)
348 /***********************************************************************
349 * AtlMarshalPtrInProc [ATL.@]
351 HRESULT WINAPI
AtlMarshalPtrInProc(IUnknown
*pUnk
, const IID
*iid
, IStream
**pstm
)
353 FIXME("%p %p %p\n", pUnk
, iid
, pstm
);
357 /***********************************************************************
358 * AtlUnmarshalPtr [ATL.@]
360 HRESULT WINAPI
AtlUnmarshalPtr(IStream
*stm
, const IID
*iid
, IUnknown
**ppUnk
)
362 FIXME("%p %p %p\n", stm
, iid
, ppUnk
);
366 /***********************************************************************
367 * AtlModuleGetClassObject [ATL.@]
369 HRESULT WINAPI
AtlModuleGetClassObject(_ATL_MODULEW
*pm
, REFCLSID rclsid
,
370 REFIID riid
, LPVOID
*ppv
)
372 _ATL_OBJMAP_ENTRYW_V1
*obj
;
374 HRESULT hres
= CLASS_E_CLASSNOTAVAILABLE
;
376 TRACE("%p %s %s %p\n", pm
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
381 for (i
= 0; (obj
= get_objmap_entry( pm
, i
)) != NULL
; i
++)
383 if (IsEqualCLSID(obj
->pclsid
, rclsid
))
385 TRACE("found object %i\n", i
);
386 if (obj
->pfnGetClassObject
)
389 hres
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
,
393 hres
= IUnknown_QueryInterface(obj
->pCF
, riid
, ppv
);
399 WARN("no class object found for %s\n", debugstr_guid(rclsid
));
404 /***********************************************************************
405 * AtlModuleGetClassObject [ATL.@]
407 HRESULT WINAPI
AtlModuleRegisterTypeLib(_ATL_MODULEW
*pm
, LPCOLESTR lpszIndex
)
413 TRACE("%p %s\n", pm
, debugstr_w(lpszIndex
));
418 hRes
= AtlModuleLoadTypeLib(pm
, lpszIndex
, &path
, &typelib
);
422 hRes
= RegisterTypeLib(typelib
, path
, NULL
); /* FIXME: pass help directory */
423 ITypeLib_Release(typelib
);
430 /***********************************************************************
431 * AtlModuleRevokeClassObjects [ATL.@]
433 HRESULT WINAPI
AtlModuleRevokeClassObjects(_ATL_MODULEW
*pm
)
439 /***********************************************************************
440 * AtlModuleUnregisterServer [ATL.@]
442 HRESULT WINAPI
AtlModuleUnregisterServer(_ATL_MODULEW
*pm
, const CLSID
*clsid
)
444 FIXME("%p %s\n", pm
, debugstr_guid(clsid
));
448 /***********************************************************************
449 * AtlModuleRegisterWndClassInfoA [ATL.@]
451 * See AtlModuleRegisterWndClassInfoW.
453 ATOM WINAPI
AtlModuleRegisterWndClassInfoA(_ATL_MODULEA
*pm
, _ATL_WNDCLASSINFOA
*wci
, WNDPROC
*pProc
)
457 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
464 TRACE("wci->m_wc.lpszClassName = %s\n", wci
->m_wc
.lpszClassName
);
466 if (!wci
->m_wc
.lpszClassName
)
468 sprintf(wci
->m_szAutoName
, "ATL%08lx", (UINT_PTR
)wci
);
469 TRACE("auto-generated class name %s\n", wci
->m_szAutoName
);
470 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
473 atom
= GetClassInfoExA(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
475 atom
= RegisterClassExA(&wci
->m_wc
);
477 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
480 *pProc
= wci
->pWndProc
;
482 TRACE("returning 0x%04x\n", atom
);
486 /***********************************************************************
487 * AtlModuleRegisterWndClassInfoW [ATL.@]
490 * pm [IO] Information about the module registering the window.
491 * wci [IO] Information about the window being registered.
492 * pProc [O] Window procedure of the registered class.
495 * Atom representing the registered class.
498 * Can be called multiple times without error, unlike RegisterClassEx().
500 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
501 * registered, where the 'x's represent a unique value.
504 ATOM WINAPI
AtlModuleRegisterWndClassInfoW(_ATL_MODULEW
*pm
, _ATL_WNDCLASSINFOW
*wci
, WNDPROC
*pProc
)
508 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
515 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci
->m_wc
.lpszClassName
));
517 if (!wci
->m_wc
.lpszClassName
)
519 static const WCHAR szFormat
[] = {'A','T','L','%','0','8','l','x',0};
520 sprintfW(wci
->m_szAutoName
, szFormat
, (UINT_PTR
)wci
);
521 TRACE("auto-generated class name %s\n", debugstr_w(wci
->m_szAutoName
));
522 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
525 atom
= GetClassInfoExW(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
527 atom
= RegisterClassExW(&wci
->m_wc
);
529 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
532 *pProc
= wci
->pWndProc
;
534 TRACE("returning 0x%04x\n", atom
);
538 void WINAPI
AtlHiMetricToPixel(const SIZEL
* lpHiMetric
, SIZEL
* lpPix
)
540 HDC dc
= GetDC(NULL
);
541 lpPix
->cx
= lpHiMetric
->cx
* GetDeviceCaps( dc
, LOGPIXELSX
) / 100;
542 lpPix
->cy
= lpHiMetric
->cy
* GetDeviceCaps( dc
, LOGPIXELSY
) / 100;
543 ReleaseDC( NULL
, dc
);
546 void WINAPI
AtlPixelToHiMetric(const SIZEL
* lpPix
, SIZEL
* lpHiMetric
)
548 HDC dc
= GetDC(NULL
);
549 lpHiMetric
->cx
= 100 * lpPix
->cx
/ GetDeviceCaps( dc
, LOGPIXELSX
);
550 lpHiMetric
->cy
= 100 * lpPix
->cy
/ GetDeviceCaps( dc
, LOGPIXELSY
);
551 ReleaseDC( NULL
, dc
);
554 /***********************************************************************
555 * AtlCreateTargetDC [ATL.@]
557 HDC WINAPI
AtlCreateTargetDC( HDC hdc
, DVTARGETDEVICE
*dv
)
559 static const WCHAR displayW
[] = {'d','i','s','p','l','a','y',0};
560 const WCHAR
*driver
= NULL
, *device
= NULL
, *port
= NULL
;
561 DEVMODEW
*devmode
= NULL
;
563 TRACE( "(%p, %p)\n", hdc
, dv
);
567 if (dv
->tdDriverNameOffset
) driver
= (WCHAR
*)((char *)dv
+ dv
->tdDriverNameOffset
);
568 if (dv
->tdDeviceNameOffset
) device
= (WCHAR
*)((char *)dv
+ dv
->tdDeviceNameOffset
);
569 if (dv
->tdPortNameOffset
) port
= (WCHAR
*)((char *)dv
+ dv
->tdPortNameOffset
);
570 if (dv
->tdExtDevmodeOffset
) devmode
= (DEVMODEW
*)((char *)dv
+ dv
->tdExtDevmodeOffset
);
577 return CreateDCW( driver
, device
, port
, devmode
);
580 /***********************************************************************
581 * AtlModuleAddCreateWndData [ATL.@]
583 void WINAPI
AtlModuleAddCreateWndData(_ATL_MODULEW
*pM
, _AtlCreateWndData
*pData
, void* pvObject
)
585 TRACE("(%p, %p, %p)\n", pM
, pData
, pvObject
);
587 pData
->m_pThis
= pvObject
;
588 pData
->m_dwThreadID
= GetCurrentThreadId();
589 pData
->m_pNext
= pM
->m_pCreateWndList
;
590 pM
->m_pCreateWndList
= pData
;
593 /***********************************************************************
594 * AtlModuleExtractCreateWndData [ATL.@]
596 * NOTE: I failed to find any good description of this function.
597 * Tests show that this function extracts one of _AtlCreateWndData
598 * records from the current thread from a list
601 void* WINAPI
AtlModuleExtractCreateWndData(_ATL_MODULEW
*pM
)
603 _AtlCreateWndData
**ppData
;
607 for(ppData
= &pM
->m_pCreateWndList
; *ppData
!=NULL
; ppData
= &(*ppData
)->m_pNext
)
609 if ((*ppData
)->m_dwThreadID
== GetCurrentThreadId())
611 _AtlCreateWndData
*pData
= *ppData
;
612 *ppData
= pData
->m_pNext
;
613 return pData
->m_pThis
;
619 /* FIXME: should be in a header file */
620 typedef struct ATL_PROPMAP_ENTRY
624 const CLSID
* pclsidPropPage
;
625 const IID
* piidDispatch
;
631 /***********************************************************************
632 * AtlIPersistStreamInit_Load [ATL.@]
634 HRESULT WINAPI
AtlIPersistStreamInit_Load( LPSTREAM pStm
, ATL_PROPMAP_ENTRY
*pMap
,
635 void *pThis
, IUnknown
*pUnk
)
637 FIXME("(%p, %p, %p, %p)\n", pStm
, pMap
, pThis
, pUnk
);
642 HRESULT WINAPI
AtlIPersistStreamInit_Save(LPSTREAM pStm
, BOOL fClearDirty
,
643 ATL_PROPMAP_ENTRY
*pMap
, void *pThis
,
646 FIXME("(%p, %d, %p, %p, %p)\n", pStm
, fClearDirty
, pMap
, pThis
, pUnk
);