2 * Implementation of Active Template Library (atl.dll)
4 * Copyright 2004 Aric Stewart for CodeWeavers
5 * Copyright 2005 Jacek Caban
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(atl
);
36 extern HINSTANCE atl_instance
;
38 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
40 HRESULT WINAPI
AtlModuleInit(_ATL_MODULEW
* pM
, _ATL_OBJMAP_ENTRYW
* p
, HINSTANCE h
)
45 TRACE("(%p %p %p)\n", pM
, p
, h
);
51 case sizeof(_ATL_MODULEW
):
53 case sizeof(_ATL_MODULEW
) + sizeof(void *):
57 WARN("Unknown structure version (size %i)\n",size
);
61 memset(pM
,0,pM
->cbSize
);
64 pM
->m_hInstResource
= h
;
65 pM
->m_hInstTypeLib
= h
;
67 pM
->m_hHeap
= GetProcessHeap();
69 InitializeCriticalSection(&pM
->u
.m_csTypeInfoHolder
);
70 InitializeCriticalSection(&pM
->m_csWindowCreate
);
71 InitializeCriticalSection(&pM
->m_csObjMap
);
75 if (pM
->m_pObjMap
!= NULL
&& size
> ATLVer1Size
)
77 while (pM
->m_pObjMap
[i
].pclsid
!= NULL
)
79 TRACE("Initializing object %i %p\n",i
,p
[i
].pfnObjectMain
);
80 if (p
[i
].pfnObjectMain
)
81 p
[i
].pfnObjectMain(TRUE
);
89 static _ATL_OBJMAP_ENTRYW_V1
*get_objmap_entry( _ATL_MODULEW
*mod
, unsigned int index
)
91 _ATL_OBJMAP_ENTRYW_V1
*ret
;
93 if (mod
->cbSize
== ATLVer1Size
)
94 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)mod
->m_pObjMap
+ index
;
96 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)(mod
->m_pObjMap
+ index
);
98 if (!ret
->pclsid
) ret
= NULL
;
102 HRESULT WINAPI
AtlModuleLoadTypeLib(_ATL_MODULEW
*pM
, LPCOLESTR lpszIndex
,
103 BSTR
*pbstrPath
, ITypeLib
**ppTypeLib
)
105 TRACE("(%p, %s, %p, %p)\n", pM
, debugstr_w(lpszIndex
), pbstrPath
, ppTypeLib
);
110 return AtlLoadTypeLib(pM
->m_hInstTypeLib
, lpszIndex
, pbstrPath
, ppTypeLib
);
113 HRESULT WINAPI
AtlModuleTerm(_ATL_MODULE
*pM
)
115 _ATL_TERMFUNC_ELEM
*iter
, *tmp
;
119 if (pM
->cbSize
> ATLVer1Size
)
121 iter
= pM
->m_pTermFuncs
;
124 iter
->pFunc(iter
->dw
);
127 HeapFree(GetProcessHeap(), 0, tmp
);
134 HRESULT WINAPI
AtlModuleRegisterClassObjects(_ATL_MODULEW
*pM
, DWORD dwClsContext
,
137 _ATL_OBJMAP_ENTRYW_V1
*obj
;
140 TRACE("(%p %i %i)\n",pM
, dwClsContext
, dwFlags
);
145 while ((obj
= get_objmap_entry( pM
, i
++ )))
150 TRACE("Registering object %i\n",i
);
151 if (obj
->pfnGetClassObject
)
153 rc
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
, &IID_IUnknown
,
157 rc
= CoRegisterClassObject(obj
->pclsid
, pUnknown
, dwClsContext
,
158 dwFlags
, &obj
->dwRegister
);
161 WARN("Failed to register object %i: 0x%08x\n", i
, rc
);
164 IUnknown_Release(pUnknown
);
172 HRESULT WINAPI
AtlModuleUnregisterServerEx(_ATL_MODULEW
* pM
, BOOL bUnRegTypeLib
, const CLSID
* pCLSID
)
174 FIXME("(%p, %i, %p) stub\n", pM
, bUnRegTypeLib
, pCLSID
);
178 /***********************************************************************
179 * AtlModuleRegisterServer [ATL.@]
182 HRESULT WINAPI
AtlModuleRegisterServer(_ATL_MODULEW
* pM
, BOOL bRegTypeLib
, const CLSID
* clsid
)
184 const _ATL_OBJMAP_ENTRYW_V1
*obj
;
188 TRACE("%p %d %s\n", pM
, bRegTypeLib
, debugstr_guid(clsid
));
193 for (i
= 0; (obj
= get_objmap_entry( pM
, i
)) != NULL
; i
++) /* register CLSIDs */
195 if (!clsid
|| IsEqualCLSID(obj
->pclsid
, clsid
))
197 TRACE("Registering clsid %s\n", debugstr_guid(obj
->pclsid
));
198 hRes
= obj
->pfnUpdateRegistry(TRUE
); /* register */
202 if(pM
->cbSize
> ATLVer1Size
) {
203 const struct _ATL_CATMAP_ENTRY
*catmap
;
205 catmap
= ((const _ATL_OBJMAP_ENTRYW
*)obj
)->pfnGetCategoryMap();
207 hRes
= AtlRegisterClassCategoriesHelper(obj
->pclsid
, catmap
, TRUE
);
217 hRes
= AtlRegisterTypeLib(pM
->m_hInstTypeLib
, NULL
);
225 /***********************************************************************
226 * AtlModuleGetClassObject [ATL.@]
228 HRESULT WINAPI
AtlModuleGetClassObject(_ATL_MODULEW
*pm
, REFCLSID rclsid
,
229 REFIID riid
, LPVOID
*ppv
)
231 _ATL_OBJMAP_ENTRYW_V1
*obj
;
233 HRESULT hres
= CLASS_E_CLASSNOTAVAILABLE
;
235 TRACE("%p %s %s %p\n", pm
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
240 for (i
= 0; (obj
= get_objmap_entry( pm
, i
)) != NULL
; i
++)
242 if (IsEqualCLSID(obj
->pclsid
, rclsid
))
244 TRACE("found object %i\n", i
);
245 if (obj
->pfnGetClassObject
)
248 hres
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
,
252 hres
= IUnknown_QueryInterface(obj
->pCF
, riid
, ppv
);
258 WARN("no class object found for %s\n", debugstr_guid(rclsid
));
263 /***********************************************************************
264 * AtlModuleGetClassObject [ATL.@]
266 HRESULT WINAPI
AtlModuleRegisterTypeLib(_ATL_MODULEW
*pm
, LPCOLESTR lpszIndex
)
268 TRACE("%p %s\n", pm
, debugstr_w(lpszIndex
));
273 return AtlRegisterTypeLib(pm
->m_hInstTypeLib
, lpszIndex
);
276 /***********************************************************************
277 * AtlModuleRevokeClassObjects [ATL.@]
279 HRESULT WINAPI
AtlModuleRevokeClassObjects(_ATL_MODULEW
*pm
)
285 /***********************************************************************
286 * AtlModuleUnregisterServer [ATL.@]
288 HRESULT WINAPI
AtlModuleUnregisterServer(_ATL_MODULEW
*pm
, const CLSID
*clsid
)
290 FIXME("%p %s\n", pm
, debugstr_guid(clsid
));
294 /***********************************************************************
295 * AtlModuleRegisterWndClassInfoA [ATL.@]
297 * See AtlModuleRegisterWndClassInfoW.
299 ATOM WINAPI
AtlModuleRegisterWndClassInfoA(_ATL_MODULEA
*pm
, _ATL_WNDCLASSINFOA
*wci
, WNDPROC
*pProc
)
303 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
310 TRACE("wci->m_wc.lpszClassName = %s\n", wci
->m_wc
.lpszClassName
);
312 if (wci
->m_lpszOrigName
)
313 FIXME( "subclassing %s not implemented\n", debugstr_a(wci
->m_lpszOrigName
));
315 if (!wci
->m_wc
.lpszClassName
)
317 snprintf(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
), "ATL%08lx", (UINT_PTR
)wci
);
318 TRACE("auto-generated class name %s\n", wci
->m_szAutoName
);
319 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
322 atom
= GetClassInfoExA(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
325 wci
->m_wc
.hInstance
= pm
->m_hInst
;
326 wci
->m_wc
.hCursor
= LoadCursorA( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
327 wci
->m_lpszCursorID
);
328 atom
= RegisterClassExA(&wci
->m_wc
);
330 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
334 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
336 TRACE("returning 0x%04x\n", atom
);
340 /***********************************************************************
341 * AtlModuleRegisterWndClassInfoW [ATL.@]
344 * pm [IO] Information about the module registering the window.
345 * wci [IO] Information about the window being registered.
346 * pProc [O] Window procedure of the registered class.
349 * Atom representing the registered class.
352 * Can be called multiple times without error, unlike RegisterClassEx().
354 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
355 * registered, where the 'x's represent a unique value.
358 ATOM WINAPI
AtlModuleRegisterWndClassInfoW(_ATL_MODULEW
*pm
, _ATL_WNDCLASSINFOW
*wci
, WNDPROC
*pProc
)
362 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
369 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci
->m_wc
.lpszClassName
));
371 if (wci
->m_lpszOrigName
)
372 FIXME( "subclassing %s not implemented\n", debugstr_w(wci
->m_lpszOrigName
));
374 if (!wci
->m_wc
.lpszClassName
)
376 static const WCHAR szFormat
[] = {'A','T','L','%','0','8','l','x',0};
377 snprintfW(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
)/sizeof(WCHAR
), szFormat
, (UINT_PTR
)wci
);
378 TRACE("auto-generated class name %s\n", debugstr_w(wci
->m_szAutoName
));
379 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
382 atom
= GetClassInfoExW(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
385 wci
->m_wc
.hInstance
= pm
->m_hInst
;
386 wci
->m_wc
.hCursor
= LoadCursorW( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
387 wci
->m_lpszCursorID
);
388 atom
= RegisterClassExW(&wci
->m_wc
);
390 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
394 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
396 TRACE("returning 0x%04x\n", atom
);
400 /***********************************************************************
401 * AtlModuleAddCreateWndData [ATL.@]
403 void WINAPI
AtlModuleAddCreateWndData(_ATL_MODULEW
*pM
, _AtlCreateWndData
*pData
, void* pvObject
)
405 TRACE("(%p, %p, %p)\n", pM
, pData
, pvObject
);
407 pData
->m_pThis
= pvObject
;
408 pData
->m_dwThreadID
= GetCurrentThreadId();
410 EnterCriticalSection(&pM
->m_csWindowCreate
);
411 pData
->m_pNext
= pM
->m_pCreateWndList
;
412 pM
->m_pCreateWndList
= pData
;
413 LeaveCriticalSection(&pM
->m_csWindowCreate
);
416 /***********************************************************************
417 * AtlModuleExtractCreateWndData [ATL.@]
419 * NOTE: Tests show that this function extracts one of _AtlCreateWndData
420 * records from the current thread from a list
423 void* WINAPI
AtlModuleExtractCreateWndData(_ATL_MODULEW
*pM
)
425 _AtlCreateWndData
**ppData
;
430 EnterCriticalSection(&pM
->m_csWindowCreate
);
432 for(ppData
= &pM
->m_pCreateWndList
; *ppData
!=NULL
; ppData
= &(*ppData
)->m_pNext
)
434 if ((*ppData
)->m_dwThreadID
== GetCurrentThreadId())
436 _AtlCreateWndData
*pData
= *ppData
;
437 *ppData
= pData
->m_pNext
;
438 ret
= pData
->m_pThis
;
443 LeaveCriticalSection(&pM
->m_csWindowCreate
);
447 /***********************************************************************
448 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
451 HRESULT WINAPI
AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW
* pM
, LPCOLESTR lpszRes
,
452 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
* pMapEntries
, IRegistrar
* pReg
)
454 TRACE("(%p %s %d %p %p)\n", pM
, debugstr_w(lpszRes
), bRegister
, pMapEntries
, pReg
);
456 return AtlUpdateRegistryFromResourceD(pM
->m_hInst
, lpszRes
, bRegister
, pMapEntries
, pReg
);
459 static HRESULT WINAPI
RegistrarCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppvObject
)
461 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
463 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IClassFactory
, riid
)) {
465 IClassFactory_AddRef( iface
);
469 return E_NOINTERFACE
;
472 static ULONG WINAPI
RegistrarCF_AddRef(IClassFactory
*iface
)
477 static ULONG WINAPI
RegistrarCF_Release(IClassFactory
*iface
)
482 static HRESULT WINAPI
RegistrarCF_CreateInstance(IClassFactory
*iface
, LPUNKNOWN pUnkOuter
,
483 REFIID riid
, void **ppv
)
485 IRegistrar
*registrar
;
488 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
492 return CLASS_E_NOAGGREGATION
;
495 hres
= AtlCreateRegistrar(®istrar
);
499 hres
= IRegistrar_QueryInterface(registrar
, riid
, ppv
);
500 IRegistrar_Release(registrar
);
504 static HRESULT WINAPI
RegistrarCF_LockServer(IClassFactory
*iface
, BOOL lock
)
506 TRACE("(%p)->(%x)\n", iface
, lock
);
510 static const IClassFactoryVtbl IRegistrarCFVtbl
= {
511 RegistrarCF_QueryInterface
,
514 RegistrarCF_CreateInstance
,
515 RegistrarCF_LockServer
518 static IClassFactory RegistrarCF
= { &IRegistrarCFVtbl
};
520 /**************************************************************
521 * DllGetClassObject (ATL.2)
523 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID riid
, LPVOID
*ppvObject
)
525 TRACE("(%s %s %p)\n", debugstr_guid(clsid
), debugstr_guid(riid
), ppvObject
);
527 if(IsEqualGUID(&CLSID_Registrar
, clsid
))
528 return IClassFactory_QueryInterface( &RegistrarCF
, riid
, ppvObject
);
530 FIXME("Not supported class %s\n", debugstr_guid(clsid
));
531 return CLASS_E_CLASSNOTAVAILABLE
;
534 /***********************************************************************
535 * DllRegisterServer (ATL.@)
537 HRESULT WINAPI
DllRegisterServer(void)
539 return __wine_register_resources( atl_instance
);
542 /***********************************************************************
543 * DllUnRegisterServer (ATL.@)
545 HRESULT WINAPI
DllUnregisterServer(void)
547 return __wine_unregister_resources( atl_instance
);
550 /***********************************************************************
551 * DllCanUnloadNow (ATL.@)
553 HRESULT WINAPI
DllCanUnloadNow(void)