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
27 extern HINSTANCE atl_instance
;
29 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
31 HRESULT WINAPI
AtlModuleInit(_ATL_MODULEW
* pM
, _ATL_OBJMAP_ENTRYW
* p
, HINSTANCE h
)
36 TRACE("(%p %p %p)\n", pM
, p
, h
);
42 case sizeof(_ATL_MODULEW
):
44 case sizeof(_ATL_MODULEW
) + sizeof(void *):
48 WARN("Unknown structure version (size %i)\n",size
);
52 memset(pM
,0,pM
->cbSize
);
55 pM
->m_hInstResource
= h
;
56 pM
->m_hInstTypeLib
= h
;
58 pM
->m_hHeap
= GetProcessHeap();
60 InitializeCriticalSection(&pM
->u
.m_csTypeInfoHolder
);
61 InitializeCriticalSection(&pM
->m_csWindowCreate
);
62 InitializeCriticalSection(&pM
->m_csObjMap
);
66 if (pM
->m_pObjMap
!= NULL
&& size
> ATLVer1Size
)
68 while (pM
->m_pObjMap
[i
].pclsid
!= NULL
)
70 TRACE("Initializing object %i %p\n",i
,p
[i
].pfnObjectMain
);
71 if (p
[i
].pfnObjectMain
)
72 p
[i
].pfnObjectMain(TRUE
);
80 static _ATL_OBJMAP_ENTRYW_V1
*get_objmap_entry( _ATL_MODULEW
*mod
, unsigned int index
)
82 _ATL_OBJMAP_ENTRYW_V1
*ret
;
84 if (mod
->cbSize
== ATLVer1Size
)
85 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)mod
->m_pObjMap
+ index
;
87 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)(mod
->m_pObjMap
+ index
);
89 if (!ret
->pclsid
) ret
= NULL
;
93 HRESULT WINAPI
AtlModuleLoadTypeLib(_ATL_MODULEW
*pM
, LPCOLESTR lpszIndex
,
94 BSTR
*pbstrPath
, ITypeLib
**ppTypeLib
)
96 TRACE("(%p, %s, %p, %p)\n", pM
, debugstr_w(lpszIndex
), pbstrPath
, ppTypeLib
);
101 return AtlLoadTypeLib(pM
->m_hInstTypeLib
, lpszIndex
, pbstrPath
, ppTypeLib
);
104 HRESULT WINAPI
AtlModuleTerm(_ATL_MODULE
*pM
)
106 _ATL_TERMFUNC_ELEM
*iter
, *tmp
;
110 if (pM
->cbSize
> ATLVer1Size
)
112 iter
= pM
->m_pTermFuncs
;
115 iter
->pFunc(iter
->dw
);
118 HeapFree(GetProcessHeap(), 0, tmp
);
125 HRESULT WINAPI
AtlModuleRegisterClassObjects(_ATL_MODULEW
*pM
, DWORD dwClsContext
,
128 _ATL_OBJMAP_ENTRYW_V1
*obj
;
131 TRACE("(%p %i %i)\n",pM
, dwClsContext
, dwFlags
);
136 while ((obj
= get_objmap_entry( pM
, i
++ )))
141 TRACE("Registering object %i\n",i
);
142 if (obj
->pfnGetClassObject
)
144 rc
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
, &IID_IUnknown
,
148 rc
= CoRegisterClassObject(obj
->pclsid
, pUnknown
, dwClsContext
,
149 dwFlags
, &obj
->dwRegister
);
152 WARN("Failed to register object %i: 0x%08x\n", i
, rc
);
155 IUnknown_Release(pUnknown
);
163 HRESULT WINAPI
AtlModuleUnregisterServerEx(_ATL_MODULEW
* pM
, BOOL bUnRegTypeLib
, const CLSID
* pCLSID
)
165 FIXME("(%p, %i, %p) stub\n", pM
, bUnRegTypeLib
, pCLSID
);
169 /***********************************************************************
170 * AtlModuleRegisterServer [ATL.@]
173 HRESULT WINAPI
AtlModuleRegisterServer(_ATL_MODULEW
* pM
, BOOL bRegTypeLib
, const CLSID
* clsid
)
175 const _ATL_OBJMAP_ENTRYW_V1
*obj
;
179 TRACE("%p %d %s\n", pM
, bRegTypeLib
, debugstr_guid(clsid
));
184 for (i
= 0; (obj
= get_objmap_entry( pM
, i
)) != NULL
; i
++) /* register CLSIDs */
186 if (!clsid
|| IsEqualCLSID(obj
->pclsid
, clsid
))
188 TRACE("Registering clsid %s\n", debugstr_guid(obj
->pclsid
));
189 hRes
= obj
->pfnUpdateRegistry(TRUE
); /* register */
193 if(pM
->cbSize
> ATLVer1Size
) {
194 const struct _ATL_CATMAP_ENTRY
*catmap
;
196 catmap
= ((const _ATL_OBJMAP_ENTRYW
*)obj
)->pfnGetCategoryMap();
198 hRes
= AtlRegisterClassCategoriesHelper(obj
->pclsid
, catmap
, TRUE
);
208 hRes
= AtlRegisterTypeLib(pM
->m_hInstTypeLib
, NULL
);
216 /***********************************************************************
217 * AtlModuleGetClassObject [ATL.@]
219 HRESULT WINAPI
AtlModuleGetClassObject(_ATL_MODULEW
*pm
, REFCLSID rclsid
,
220 REFIID riid
, LPVOID
*ppv
)
222 _ATL_OBJMAP_ENTRYW_V1
*obj
;
224 HRESULT hres
= CLASS_E_CLASSNOTAVAILABLE
;
226 TRACE("%p %s %s %p\n", pm
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
231 for (i
= 0; (obj
= get_objmap_entry( pm
, i
)) != NULL
; i
++)
233 if (IsEqualCLSID(obj
->pclsid
, rclsid
))
235 TRACE("found object %i\n", i
);
236 if (obj
->pfnGetClassObject
)
239 hres
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
,
243 hres
= IUnknown_QueryInterface(obj
->pCF
, riid
, ppv
);
249 WARN("no class object found for %s\n", debugstr_guid(rclsid
));
254 /***********************************************************************
255 * AtlModuleGetClassObject [ATL.@]
257 HRESULT WINAPI
AtlModuleRegisterTypeLib(_ATL_MODULEW
*pm
, LPCOLESTR lpszIndex
)
259 TRACE("%p %s\n", pm
, debugstr_w(lpszIndex
));
264 return AtlRegisterTypeLib(pm
->m_hInstTypeLib
, lpszIndex
);
267 /***********************************************************************
268 * AtlModuleRevokeClassObjects [ATL.@]
270 HRESULT WINAPI
AtlModuleRevokeClassObjects(_ATL_MODULEW
*pm
)
276 /***********************************************************************
277 * AtlModuleUnregisterServer [ATL.@]
279 HRESULT WINAPI
AtlModuleUnregisterServer(_ATL_MODULEW
*pm
, const CLSID
*clsid
)
281 FIXME("%p %s\n", pm
, debugstr_guid(clsid
));
285 /***********************************************************************
286 * AtlModuleRegisterWndClassInfoA [ATL.@]
288 * See AtlModuleRegisterWndClassInfoW.
290 ATOM WINAPI
AtlModuleRegisterWndClassInfoA(_ATL_MODULEA
*pm
, _ATL_WNDCLASSINFOA
*wci
, WNDPROC
*pProc
)
294 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
301 TRACE("wci->m_wc.lpszClassName = %s\n", wci
->m_wc
.lpszClassName
);
303 if (wci
->m_lpszOrigName
)
304 FIXME( "subclassing %s not implemented\n", debugstr_a(wci
->m_lpszOrigName
));
306 if (!wci
->m_wc
.lpszClassName
)
308 snprintf(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
), "ATL%08lx", (UINT_PTR
)wci
);
309 TRACE("auto-generated class name %s\n", wci
->m_szAutoName
);
310 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
313 atom
= GetClassInfoExA(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
316 wci
->m_wc
.hInstance
= pm
->m_hInst
;
317 wci
->m_wc
.hCursor
= LoadCursorA( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
318 wci
->m_lpszCursorID
);
319 atom
= RegisterClassExA(&wci
->m_wc
);
321 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
325 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
327 TRACE("returning 0x%04x\n", atom
);
331 /***********************************************************************
332 * AtlModuleRegisterWndClassInfoW [ATL.@]
335 * pm [IO] Information about the module registering the window.
336 * wci [IO] Information about the window being registered.
337 * pProc [O] Window procedure of the registered class.
340 * Atom representing the registered class.
343 * Can be called multiple times without error, unlike RegisterClassEx().
345 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
346 * registered, where the 'x's represent a unique value.
349 ATOM WINAPI
AtlModuleRegisterWndClassInfoW(_ATL_MODULEW
*pm
, _ATL_WNDCLASSINFOW
*wci
, WNDPROC
*pProc
)
353 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
360 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci
->m_wc
.lpszClassName
));
362 if (wci
->m_lpszOrigName
)
363 FIXME( "subclassing %s not implemented\n", debugstr_w(wci
->m_lpszOrigName
));
365 if (!wci
->m_wc
.lpszClassName
)
367 static const WCHAR szFormat
[] = {'A','T','L','%','0','8','l','x',0};
368 snprintfW(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
)/sizeof(WCHAR
), szFormat
, (UINT_PTR
)wci
);
369 TRACE("auto-generated class name %s\n", debugstr_w(wci
->m_szAutoName
));
370 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
373 atom
= GetClassInfoExW(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
376 wci
->m_wc
.hInstance
= pm
->m_hInst
;
377 wci
->m_wc
.hCursor
= LoadCursorW( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
378 wci
->m_lpszCursorID
);
379 atom
= RegisterClassExW(&wci
->m_wc
);
381 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
385 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
387 TRACE("returning 0x%04x\n", atom
);
391 /***********************************************************************
392 * AtlModuleAddCreateWndData [ATL.@]
394 void WINAPI
AtlModuleAddCreateWndData(_ATL_MODULEW
*pM
, _AtlCreateWndData
*pData
, void* pvObject
)
396 TRACE("(%p, %p, %p)\n", pM
, pData
, pvObject
);
398 pData
->m_pThis
= pvObject
;
399 pData
->m_dwThreadID
= GetCurrentThreadId();
401 EnterCriticalSection(&pM
->m_csWindowCreate
);
402 pData
->m_pNext
= pM
->m_pCreateWndList
;
403 pM
->m_pCreateWndList
= pData
;
404 LeaveCriticalSection(&pM
->m_csWindowCreate
);
407 /***********************************************************************
408 * AtlModuleExtractCreateWndData [ATL.@]
410 * NOTE: Tests show that this function extracts one of _AtlCreateWndData
411 * records from the current thread from a list
414 void* WINAPI
AtlModuleExtractCreateWndData(_ATL_MODULEW
*pM
)
416 _AtlCreateWndData
**ppData
;
421 EnterCriticalSection(&pM
->m_csWindowCreate
);
423 for(ppData
= &pM
->m_pCreateWndList
; *ppData
!=NULL
; ppData
= &(*ppData
)->m_pNext
)
425 if ((*ppData
)->m_dwThreadID
== GetCurrentThreadId())
427 _AtlCreateWndData
*pData
= *ppData
;
428 *ppData
= pData
->m_pNext
;
429 ret
= pData
->m_pThis
;
434 LeaveCriticalSection(&pM
->m_csWindowCreate
);
438 /***********************************************************************
439 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
442 HRESULT WINAPI
AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW
* pM
, LPCOLESTR lpszRes
,
443 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
* pMapEntries
, IRegistrar
* pReg
)
445 TRACE("(%p %s %d %p %p)\n", pM
, debugstr_w(lpszRes
), bRegister
, pMapEntries
, pReg
);
447 return AtlUpdateRegistryFromResourceD(pM
->m_hInst
, lpszRes
, bRegister
, pMapEntries
, pReg
);
450 static HRESULT WINAPI
RegistrarCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppvObject
)
452 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
454 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IClassFactory
, riid
)) {
456 IClassFactory_AddRef( iface
);
460 return E_NOINTERFACE
;
463 static ULONG WINAPI
RegistrarCF_AddRef(IClassFactory
*iface
)
468 static ULONG WINAPI
RegistrarCF_Release(IClassFactory
*iface
)
473 static HRESULT WINAPI
RegistrarCF_CreateInstance(IClassFactory
*iface
, LPUNKNOWN pUnkOuter
,
474 REFIID riid
, void **ppv
)
476 IRegistrar
*registrar
;
479 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
483 return CLASS_E_NOAGGREGATION
;
486 hres
= AtlCreateRegistrar(®istrar
);
490 hres
= IRegistrar_QueryInterface(registrar
, riid
, ppv
);
491 IRegistrar_Release(registrar
);
495 static HRESULT WINAPI
RegistrarCF_LockServer(IClassFactory
*iface
, BOOL lock
)
497 TRACE("(%p)->(%x)\n", iface
, lock
);
501 static const IClassFactoryVtbl IRegistrarCFVtbl
= {
502 RegistrarCF_QueryInterface
,
505 RegistrarCF_CreateInstance
,
506 RegistrarCF_LockServer
509 static IClassFactory RegistrarCF
= { &IRegistrarCFVtbl
};
512 static HRESULT
do_register_dll_server(IRegistrar
*pRegistrar
, LPCOLESTR wszDll
,
513 LPCOLESTR wszId
, BOOL do_register
,
514 const struct _ATL_REGMAP_ENTRY
* pMapEntries
)
516 IRegistrar
*registrar
;
518 const struct _ATL_REGMAP_ENTRY
*pMapEntry
;
520 static const WCHAR wszModule
[] = {'M','O','D','U','L','E',0};
521 static const WCHAR wszRegistry
[] = {'R','E','G','I','S','T','R','Y',0};
524 registrar
= pRegistrar
;
526 hres
= AtlCreateRegistrar(®istrar
);
531 IRegistrar_AddReplacement(registrar
, wszModule
, wszDll
);
533 for (pMapEntry
= pMapEntries
; pMapEntry
&& pMapEntry
->szKey
; pMapEntry
++)
534 IRegistrar_AddReplacement(registrar
, pMapEntry
->szKey
, pMapEntry
->szData
);
537 hres
= IRegistrar_ResourceRegisterSz(registrar
, wszDll
, wszId
, wszRegistry
);
539 hres
= IRegistrar_ResourceUnregisterSz(registrar
, wszDll
, wszId
, wszRegistry
);
541 if(registrar
!= pRegistrar
)
542 IRegistrar_Release(registrar
);
546 static HRESULT
do_register_server(BOOL do_register
)
548 static const WCHAR CLSID_RegistrarW
[] =
549 {'C','L','S','I','D','_','R','e','g','i','s','t','r','a','r',0};
550 static const WCHAR atl_dllW
[] = {'a','t','l','.','d','l','l',0};
553 const struct _ATL_REGMAP_ENTRY reg_map
[] = {{CLSID_RegistrarW
, clsid_str
}, {NULL
,NULL
}};
555 StringFromGUID2(&CLSID_Registrar
, clsid_str
, sizeof(clsid_str
)/sizeof(WCHAR
));
556 return do_register_dll_server(NULL
, atl_dllW
, MAKEINTRESOURCEW(101), do_register
, reg_map
);
560 /**************************************************************
561 * DllGetClassObject (ATL.2)
563 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID riid
, LPVOID
*ppvObject
)
565 TRACE("(%s %s %p)\n", debugstr_guid(clsid
), debugstr_guid(riid
), ppvObject
);
567 if(IsEqualGUID(&CLSID_Registrar
, clsid
))
568 return IClassFactory_QueryInterface( &RegistrarCF
, riid
, ppvObject
);
570 FIXME("Not supported class %s\n", debugstr_guid(clsid
));
571 return CLASS_E_CLASSNOTAVAILABLE
;
574 /***********************************************************************
575 * DllRegisterServer (ATL.@)
577 HRESULT WINAPI
DllRegisterServer(void)
579 /* Note: we can't use __wine_register_server here because it uses CLSID_Registrar which isn't registred yet */
580 return do_register_server(TRUE
);
583 /***********************************************************************
584 * DllUnRegisterServer (ATL.@)
586 HRESULT WINAPI
DllUnregisterServer(void)
588 return do_register_server(FALSE
);
591 /***********************************************************************
592 * DllCanUnloadNow (ATL.@)
594 HRESULT WINAPI
DllCanUnloadNow(void)