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
22 #define WIN32_NO_STATUS
24 #define COM_NO_WINDOWS_H
39 #include <wine/debug.h>
40 #include <wine/unicode.h>
42 WINE_DEFAULT_DEBUG_CHANNEL(atl
);
44 static HINSTANCE hInst
;
46 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
48 TRACE("(0x%p, %d, %p)\n",hinstDLL
,fdwReason
,lpvReserved
);
50 if (fdwReason
== DLL_PROCESS_ATTACH
) {
51 DisableThreadLibraryCalls(hinstDLL
);
57 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
59 HRESULT WINAPI
AtlModuleInit(_ATL_MODULEW
* pM
, _ATL_OBJMAP_ENTRYW
* p
, HINSTANCE h
)
64 TRACE("(%p %p %p)\n", pM
, p
, h
);
70 case sizeof(_ATL_MODULEW
):
72 case sizeof(_ATL_MODULEW
) + sizeof(void *):
76 WARN("Unknown structure version (size %i)\n",size
);
80 memset(pM
,0,pM
->cbSize
);
83 pM
->m_hInstResource
= h
;
84 pM
->m_hInstTypeLib
= h
;
86 pM
->m_hHeap
= GetProcessHeap();
88 InitializeCriticalSection(&pM
->u
.m_csTypeInfoHolder
);
89 InitializeCriticalSection(&pM
->m_csWindowCreate
);
90 InitializeCriticalSection(&pM
->m_csObjMap
);
94 if (pM
->m_pObjMap
!= NULL
&& size
> ATLVer1Size
)
96 while (pM
->m_pObjMap
[i
].pclsid
!= NULL
)
98 TRACE("Initializing object %i %p\n",i
,p
[i
].pfnObjectMain
);
99 if (p
[i
].pfnObjectMain
)
100 p
[i
].pfnObjectMain(TRUE
);
108 static _ATL_OBJMAP_ENTRYW_V1
*get_objmap_entry( _ATL_MODULEW
*mod
, unsigned int index
)
110 _ATL_OBJMAP_ENTRYW_V1
*ret
;
112 if (mod
->cbSize
== ATLVer1Size
)
113 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)mod
->m_pObjMap
+ index
;
115 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)(mod
->m_pObjMap
+ index
);
117 if (!ret
->pclsid
) ret
= NULL
;
121 HRESULT WINAPI
AtlModuleLoadTypeLib(_ATL_MODULEW
*pM
, LPCOLESTR lpszIndex
,
122 BSTR
*pbstrPath
, ITypeLib
**ppTypeLib
)
124 TRACE("(%p, %s, %p, %p)\n", pM
, debugstr_w(lpszIndex
), pbstrPath
, ppTypeLib
);
129 return AtlLoadTypeLib(pM
->m_hInstTypeLib
, lpszIndex
, pbstrPath
, ppTypeLib
);
132 HRESULT WINAPI
AtlModuleTerm(_ATL_MODULE
*pM
)
134 _ATL_TERMFUNC_ELEM
*iter
, *tmp
;
138 if (pM
->cbSize
> ATLVer1Size
)
140 iter
= pM
->m_pTermFuncs
;
143 iter
->pFunc(iter
->dw
);
146 HeapFree(GetProcessHeap(), 0, tmp
);
153 HRESULT WINAPI
AtlModuleAddTermFunc(_ATL_MODULEW
*pM
, _ATL_TERMFUNC
*pFunc
, DWORD_PTR dw
)
155 _ATL_TERMFUNC_ELEM
*termfunc_elem
;
157 TRACE("(%p %p %ld)\n", pM
, pFunc
, dw
);
159 if (pM
->cbSize
> ATLVer1Size
)
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
;
166 pM
->m_pTermFuncs
= termfunc_elem
;
172 HRESULT WINAPI
AtlModuleRegisterClassObjects(_ATL_MODULEW
*pM
, DWORD dwClsContext
,
175 _ATL_OBJMAP_ENTRYW_V1
*obj
;
178 TRACE("(%p %i %i)\n",pM
, dwClsContext
, dwFlags
);
183 while ((obj
= get_objmap_entry( pM
, i
++ )))
188 TRACE("Registering object %i\n",i
);
189 if (obj
->pfnGetClassObject
)
191 rc
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
, &IID_IUnknown
,
195 rc
= CoRegisterClassObject(obj
->pclsid
, pUnknown
, dwClsContext
,
196 dwFlags
, &obj
->dwRegister
);
199 WARN("Failed to register object %i: 0x%08x\n", i
, rc
);
202 IUnknown_Release(pUnknown
);
210 HRESULT WINAPI
AtlModuleUnregisterServerEx(_ATL_MODULEW
* pM
, BOOL bUnRegTypeLib
, const CLSID
* pCLSID
)
212 FIXME("(%p, %i, %p) stub\n", pM
, bUnRegTypeLib
, pCLSID
);
216 /***********************************************************************
217 * AtlModuleRegisterServer [ATL.@]
220 HRESULT WINAPI
AtlModuleRegisterServer(_ATL_MODULEW
* pM
, BOOL bRegTypeLib
, const CLSID
* clsid
)
222 const _ATL_OBJMAP_ENTRYW_V1
*obj
;
226 TRACE("%p %d %s\n", pM
, bRegTypeLib
, debugstr_guid(clsid
));
231 for (i
= 0; (obj
= get_objmap_entry( pM
, i
)) != NULL
; i
++) /* register CLSIDs */
233 if (!clsid
|| IsEqualCLSID(obj
->pclsid
, clsid
))
235 TRACE("Registering clsid %s\n", debugstr_guid(obj
->pclsid
));
236 hRes
= obj
->pfnUpdateRegistry(TRUE
); /* register */
240 if(pM
->cbSize
> ATLVer1Size
) {
241 const struct _ATL_CATMAP_ENTRY
*catmap
;
243 catmap
= ((const _ATL_OBJMAP_ENTRYW
*)obj
)->pfnGetCategoryMap();
245 hRes
= AtlRegisterClassCategoriesHelper(obj
->pclsid
, catmap
, TRUE
);
255 hRes
= AtlRegisterTypeLib(pM
->m_hInstTypeLib
, NULL
);
263 /***********************************************************************
264 * AtlModuleGetClassObject [ATL.@]
266 HRESULT WINAPI
AtlModuleGetClassObject(_ATL_MODULEW
*pm
, REFCLSID rclsid
,
267 REFIID riid
, LPVOID
*ppv
)
269 _ATL_OBJMAP_ENTRYW_V1
*obj
;
271 HRESULT hres
= CLASS_E_CLASSNOTAVAILABLE
;
273 TRACE("%p %s %s %p\n", pm
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
278 for (i
= 0; (obj
= get_objmap_entry( pm
, i
)) != NULL
; i
++)
280 if (IsEqualCLSID(obj
->pclsid
, rclsid
))
282 TRACE("found object %i\n", i
);
283 if (obj
->pfnGetClassObject
)
286 hres
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
,
290 hres
= IUnknown_QueryInterface(obj
->pCF
, riid
, ppv
);
296 WARN("no class object found for %s\n", debugstr_guid(rclsid
));
301 /***********************************************************************
302 * AtlModuleGetClassObject [ATL.@]
304 HRESULT WINAPI
AtlModuleRegisterTypeLib(_ATL_MODULEW
*pm
, LPCOLESTR lpszIndex
)
306 TRACE("%p %s\n", pm
, debugstr_w(lpszIndex
));
311 return AtlRegisterTypeLib(pm
->m_hInstTypeLib
, lpszIndex
);
314 /***********************************************************************
315 * AtlModuleRevokeClassObjects [ATL.@]
317 HRESULT WINAPI
AtlModuleRevokeClassObjects(_ATL_MODULEW
*pm
)
323 /***********************************************************************
324 * AtlModuleUnregisterServer [ATL.@]
326 HRESULT WINAPI
AtlModuleUnregisterServer(_ATL_MODULEW
*pm
, const CLSID
*clsid
)
328 FIXME("%p %s\n", pm
, debugstr_guid(clsid
));
332 /***********************************************************************
333 * AtlModuleRegisterWndClassInfoA [ATL.@]
335 * See AtlModuleRegisterWndClassInfoW.
337 ATOM WINAPI
AtlModuleRegisterWndClassInfoA(_ATL_MODULEA
*pm
, _ATL_WNDCLASSINFOA
*wci
, WNDPROC
*pProc
)
341 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
348 TRACE("wci->m_wc.lpszClassName = %s\n", wci
->m_wc
.lpszClassName
);
350 if (wci
->m_lpszOrigName
)
351 FIXME( "subclassing %s not implemented\n", debugstr_a(wci
->m_lpszOrigName
));
353 if (!wci
->m_wc
.lpszClassName
)
355 snprintf(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
), "ATL%08lx", (UINT_PTR
)wci
);
356 TRACE("auto-generated class name %s\n", wci
->m_szAutoName
);
357 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
360 atom
= GetClassInfoExA(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
363 wci
->m_wc
.hInstance
= pm
->m_hInst
;
364 wci
->m_wc
.hCursor
= LoadCursorA( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
365 wci
->m_lpszCursorID
);
366 atom
= RegisterClassExA(&wci
->m_wc
);
368 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
372 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
374 TRACE("returning 0x%04x\n", atom
);
378 /***********************************************************************
379 * AtlModuleRegisterWndClassInfoW [ATL.@]
382 * pm [IO] Information about the module registering the window.
383 * wci [IO] Information about the window being registered.
384 * pProc [O] Window procedure of the registered class.
387 * Atom representing the registered class.
390 * Can be called multiple times without error, unlike RegisterClassEx().
392 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
393 * registered, where the 'x's represent a unique value.
396 ATOM WINAPI
AtlModuleRegisterWndClassInfoW(_ATL_MODULEW
*pm
, _ATL_WNDCLASSINFOW
*wci
, WNDPROC
*pProc
)
400 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
407 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci
->m_wc
.lpszClassName
));
409 if (wci
->m_lpszOrigName
)
410 FIXME( "subclassing %s not implemented\n", debugstr_w(wci
->m_lpszOrigName
));
412 if (!wci
->m_wc
.lpszClassName
)
414 static const WCHAR szFormat
[] = {'A','T','L','%','0','8','l','x',0};
415 snprintfW(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
)/sizeof(WCHAR
), szFormat
, (UINT_PTR
)wci
);
416 TRACE("auto-generated class name %s\n", debugstr_w(wci
->m_szAutoName
));
417 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
420 atom
= GetClassInfoExW(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
423 wci
->m_wc
.hInstance
= pm
->m_hInst
;
424 wci
->m_wc
.hCursor
= LoadCursorW( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
425 wci
->m_lpszCursorID
);
426 atom
= RegisterClassExW(&wci
->m_wc
);
428 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
432 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
434 TRACE("returning 0x%04x\n", atom
);
438 /***********************************************************************
439 * AtlModuleAddCreateWndData [ATL.@]
441 void WINAPI
AtlModuleAddCreateWndData(_ATL_MODULEW
*pM
, _AtlCreateWndData
*pData
, void* pvObject
)
443 TRACE("(%p, %p, %p)\n", pM
, pData
, pvObject
);
445 pData
->m_pThis
= pvObject
;
446 pData
->m_dwThreadID
= GetCurrentThreadId();
448 EnterCriticalSection(&pM
->m_csWindowCreate
);
449 pData
->m_pNext
= pM
->m_pCreateWndList
;
450 pM
->m_pCreateWndList
= pData
;
451 LeaveCriticalSection(&pM
->m_csWindowCreate
);
454 /***********************************************************************
455 * AtlModuleExtractCreateWndData [ATL.@]
457 * NOTE: Tests show that this function extracts one of _AtlCreateWndData
458 * records from the current thread from a list
461 void* WINAPI
AtlModuleExtractCreateWndData(_ATL_MODULEW
*pM
)
463 _AtlCreateWndData
**ppData
;
468 EnterCriticalSection(&pM
->m_csWindowCreate
);
470 for(ppData
= &pM
->m_pCreateWndList
; *ppData
!=NULL
; ppData
= &(*ppData
)->m_pNext
)
472 if ((*ppData
)->m_dwThreadID
== GetCurrentThreadId())
474 _AtlCreateWndData
*pData
= *ppData
;
475 *ppData
= pData
->m_pNext
;
476 ret
= pData
->m_pThis
;
481 LeaveCriticalSection(&pM
->m_csWindowCreate
);
485 /***********************************************************************
486 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
489 HRESULT WINAPI
AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW
* pM
, LPCOLESTR lpszRes
,
490 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
* pMapEntries
, IRegistrar
* pReg
)
492 TRACE("(%p %s %d %p %p)\n", pM
, debugstr_w(lpszRes
), bRegister
, pMapEntries
, pReg
);
494 return AtlUpdateRegistryFromResourceD(pM
->m_hInst
, lpszRes
, bRegister
, pMapEntries
, pReg
);
497 static HRESULT WINAPI
RegistrarCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppvObject
)
499 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
501 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IClassFactory
, riid
)) {
503 IClassFactory_AddRef( iface
);
507 return E_NOINTERFACE
;
510 static ULONG WINAPI
RegistrarCF_AddRef(IClassFactory
*iface
)
515 static ULONG WINAPI
RegistrarCF_Release(IClassFactory
*iface
)
520 static HRESULT WINAPI
RegistrarCF_CreateInstance(IClassFactory
*iface
, LPUNKNOWN pUnkOuter
,
521 REFIID riid
, void **ppv
)
523 IRegistrar
*registrar
;
526 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
530 return CLASS_E_NOAGGREGATION
;
533 hres
= AtlCreateRegistrar(®istrar
);
537 hres
= IRegistrar_QueryInterface(registrar
, riid
, ppv
);
538 IRegistrar_Release(registrar
);
542 static HRESULT WINAPI
RegistrarCF_LockServer(IClassFactory
*iface
, BOOL lock
)
544 TRACE("(%p)->(%x)\n", iface
, lock
);
548 static const IClassFactoryVtbl IRegistrarCFVtbl
= {
549 RegistrarCF_QueryInterface
,
552 RegistrarCF_CreateInstance
,
553 RegistrarCF_LockServer
556 static IClassFactory RegistrarCF
= { &IRegistrarCFVtbl
};
558 static HRESULT
do_register_dll_server(IRegistrar
*pRegistrar
, LPCOLESTR wszDll
,
559 LPCOLESTR wszId
, BOOL do_register
,
560 const struct _ATL_REGMAP_ENTRY
* pMapEntries
)
562 IRegistrar
*registrar
;
564 const struct _ATL_REGMAP_ENTRY
*pMapEntry
;
566 static const WCHAR wszModule
[] = {'M','O','D','U','L','E',0};
567 static const WCHAR wszRegistry
[] = {'R','E','G','I','S','T','R','Y',0};
570 registrar
= pRegistrar
;
572 hres
= AtlCreateRegistrar(®istrar
);
577 IRegistrar_AddReplacement(registrar
, wszModule
, wszDll
);
579 for (pMapEntry
= pMapEntries
; pMapEntry
&& pMapEntry
->szKey
; pMapEntry
++)
580 IRegistrar_AddReplacement(registrar
, pMapEntry
->szKey
, pMapEntry
->szData
);
583 hres
= IRegistrar_ResourceRegisterSz(registrar
, wszDll
, wszId
, wszRegistry
);
585 hres
= IRegistrar_ResourceUnregisterSz(registrar
, wszDll
, wszId
, wszRegistry
);
587 if(registrar
!= pRegistrar
)
588 IRegistrar_Release(registrar
);
592 static HRESULT
do_register_server(BOOL do_register
)
594 static const WCHAR CLSID_RegistrarW
[] =
595 {'C','L','S','I','D','_','R','e','g','i','s','t','r','a','r',0};
596 static const WCHAR atl_dllW
[] = {'a','t','l','.','d','l','l',0};
599 const struct _ATL_REGMAP_ENTRY reg_map
[] = {{CLSID_RegistrarW
, clsid_str
}, {NULL
,NULL
}};
601 StringFromGUID2(&CLSID_Registrar
, clsid_str
, sizeof(clsid_str
)/sizeof(WCHAR
));
602 return do_register_dll_server(NULL
, atl_dllW
, MAKEINTRESOURCEW(101), do_register
, reg_map
);
605 /**************************************************************
606 * DllGetClassObject (ATL.2)
608 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID riid
, LPVOID
*ppvObject
)
610 TRACE("(%s %s %p)\n", debugstr_guid(clsid
), debugstr_guid(riid
), ppvObject
);
612 if(IsEqualGUID(&CLSID_Registrar
, clsid
))
613 return IClassFactory_QueryInterface( &RegistrarCF
, riid
, ppvObject
);
615 FIXME("Not supported class %s\n", debugstr_guid(clsid
));
616 return CLASS_E_CLASSNOTAVAILABLE
;
619 /***********************************************************************
620 * DllRegisterServer (ATL.@)
622 HRESULT WINAPI
DllRegisterServer(void)
624 /* Note: we can't use __wine_register_server here because it uses CLSID_Registrar which isn't registred yet */
625 return do_register_server(TRUE
);
628 /***********************************************************************
629 * DllUnRegisterServer (ATL.@)
631 HRESULT WINAPI
DllUnregisterServer(void)
633 return do_register_server(FALSE
);
636 /***********************************************************************
637 * DllCanUnloadNow (ATL.@)
639 HRESULT WINAPI
DllCanUnloadNow(void)
644 /***********************************************************************
645 * AtlGetVersion [ATL.@]
647 DWORD WINAPI
AtlGetVersion(void *pReserved
)
652 /**********************************************************************
653 * AtlAxWin class window procedure
655 static LRESULT CALLBACK
AtlAxWin_wndproc( HWND hWnd
, UINT wMsg
, WPARAM wParam
, LPARAM lParam
)
657 if ( wMsg
== WM_CREATE
)
659 DWORD len
= GetWindowTextLengthW( hWnd
) + 1;
660 WCHAR
*ptr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
663 GetWindowTextW( hWnd
, ptr
, len
);
664 AtlAxCreateControlEx( ptr
, hWnd
, NULL
, NULL
, NULL
, NULL
, NULL
);
665 HeapFree( GetProcessHeap(), 0, ptr
);
668 return DefWindowProcW( hWnd
, wMsg
, wParam
, lParam
);
671 BOOL WINAPI
AtlAxWinInit(void)
674 const WCHAR AtlAxWin
[] = {'A','t','l','A','x','W','i','n',0};
676 FIXME("semi-stub\n");
678 if ( FAILED( OleInitialize(NULL
) ) )
681 wcex
.cbSize
= sizeof(wcex
);
682 wcex
.style
= CS_GLOBALCLASS
;
685 wcex
.hInstance
= GetModuleHandleW( NULL
);
688 wcex
.hbrBackground
= NULL
;
689 wcex
.lpszMenuName
= NULL
;
692 wcex
.lpfnWndProc
= AtlAxWin_wndproc
;
693 wcex
.lpszClassName
= AtlAxWin
;
694 if ( !RegisterClassExW( &wcex
) )