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 static HINSTANCE hInst
;
38 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
40 TRACE("(0x%p, %d, %p)\n",hinstDLL
,fdwReason
,lpvReserved
);
42 if (fdwReason
== DLL_PROCESS_ATTACH
) {
43 DisableThreadLibraryCalls(hinstDLL
);
49 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
51 HRESULT WINAPI
AtlModuleInit(_ATL_MODULEW
* pM
, _ATL_OBJMAP_ENTRYW
* p
, HINSTANCE h
)
56 TRACE("(%p %p %p)\n", pM
, p
, h
);
62 case sizeof(_ATL_MODULEW
):
64 case sizeof(_ATL_MODULEW
) + sizeof(void *):
68 WARN("Unknown structure version (size %i)\n",size
);
72 memset(pM
,0,pM
->cbSize
);
75 pM
->m_hInstResource
= h
;
76 pM
->m_hInstTypeLib
= h
;
78 pM
->m_hHeap
= GetProcessHeap();
80 InitializeCriticalSection(&pM
->u
.m_csTypeInfoHolder
);
81 InitializeCriticalSection(&pM
->m_csWindowCreate
);
82 InitializeCriticalSection(&pM
->m_csObjMap
);
86 if (pM
->m_pObjMap
!= NULL
&& size
> ATLVer1Size
)
88 while (pM
->m_pObjMap
[i
].pclsid
!= NULL
)
90 TRACE("Initializing object %i %p\n",i
,p
[i
].pfnObjectMain
);
91 if (p
[i
].pfnObjectMain
)
92 p
[i
].pfnObjectMain(TRUE
);
100 static _ATL_OBJMAP_ENTRYW_V1
*get_objmap_entry( _ATL_MODULEW
*mod
, unsigned int index
)
102 _ATL_OBJMAP_ENTRYW_V1
*ret
;
104 if (mod
->cbSize
== ATLVer1Size
)
105 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)mod
->m_pObjMap
+ index
;
107 ret
= (_ATL_OBJMAP_ENTRYW_V1
*)(mod
->m_pObjMap
+ index
);
109 if (!ret
->pclsid
) ret
= NULL
;
113 HRESULT WINAPI
AtlModuleLoadTypeLib(_ATL_MODULEW
*pM
, LPCOLESTR lpszIndex
,
114 BSTR
*pbstrPath
, ITypeLib
**ppTypeLib
)
116 TRACE("(%p, %s, %p, %p)\n", pM
, debugstr_w(lpszIndex
), pbstrPath
, ppTypeLib
);
121 return AtlLoadTypeLib(pM
->m_hInstTypeLib
, lpszIndex
, pbstrPath
, ppTypeLib
);
124 HRESULT WINAPI
AtlModuleTerm(_ATL_MODULE
*pM
)
126 _ATL_TERMFUNC_ELEM
*iter
, *tmp
;
130 if (pM
->cbSize
> ATLVer1Size
)
132 iter
= pM
->m_pTermFuncs
;
135 iter
->pFunc(iter
->dw
);
138 HeapFree(GetProcessHeap(), 0, tmp
);
145 HRESULT WINAPI
AtlModuleAddTermFunc(_ATL_MODULEW
*pM
, _ATL_TERMFUNC
*pFunc
, DWORD_PTR dw
)
147 _ATL_TERMFUNC_ELEM
*termfunc_elem
;
149 TRACE("version %04x (%p %p %ld)\n", _ATL_VER
, pM
, pFunc
, dw
);
151 if (pM
->cbSize
> ATLVer1Size
)
153 termfunc_elem
= HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM
));
154 termfunc_elem
->pFunc
= pFunc
;
155 termfunc_elem
->dw
= dw
;
156 termfunc_elem
->pNext
= pM
->m_pTermFuncs
;
158 pM
->m_pTermFuncs
= termfunc_elem
;
164 HRESULT WINAPI
AtlModuleRegisterClassObjects(_ATL_MODULEW
*pM
, DWORD dwClsContext
,
167 _ATL_OBJMAP_ENTRYW_V1
*obj
;
170 TRACE("(%p %i %i)\n",pM
, dwClsContext
, dwFlags
);
175 while ((obj
= get_objmap_entry( pM
, i
++ )))
180 TRACE("Registering object %i\n",i
);
181 if (obj
->pfnGetClassObject
)
183 rc
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
, &IID_IUnknown
,
187 rc
= CoRegisterClassObject(obj
->pclsid
, pUnknown
, dwClsContext
,
188 dwFlags
, &obj
->dwRegister
);
191 WARN("Failed to register object %i: 0x%08x\n", i
, rc
);
194 IUnknown_Release(pUnknown
);
202 HRESULT WINAPI
AtlModuleUnregisterServerEx(_ATL_MODULEW
* pM
, BOOL bUnRegTypeLib
, const CLSID
* pCLSID
)
204 FIXME("(%p, %i, %p) stub\n", pM
, bUnRegTypeLib
, pCLSID
);
208 /***********************************************************************
209 * AtlModuleRegisterServer [ATL.@]
212 HRESULT WINAPI
AtlModuleRegisterServer(_ATL_MODULEW
* pM
, BOOL bRegTypeLib
, const CLSID
* clsid
)
214 const _ATL_OBJMAP_ENTRYW_V1
*obj
;
218 TRACE("%p %d %s\n", pM
, bRegTypeLib
, debugstr_guid(clsid
));
223 for (i
= 0; (obj
= get_objmap_entry( pM
, i
)) != NULL
; i
++) /* register CLSIDs */
225 if (!clsid
|| IsEqualCLSID(obj
->pclsid
, clsid
))
227 TRACE("Registering clsid %s\n", debugstr_guid(obj
->pclsid
));
228 hRes
= obj
->pfnUpdateRegistry(TRUE
); /* register */
232 if(pM
->cbSize
> ATLVer1Size
) {
233 const struct _ATL_CATMAP_ENTRY
*catmap
;
235 catmap
= ((const _ATL_OBJMAP_ENTRYW
*)obj
)->pfnGetCategoryMap();
237 hRes
= AtlRegisterClassCategoriesHelper(obj
->pclsid
, catmap
, TRUE
);
247 hRes
= AtlRegisterTypeLib(pM
->m_hInstTypeLib
, NULL
);
255 /***********************************************************************
256 * AtlModuleGetClassObject [ATL.@]
258 HRESULT WINAPI
AtlModuleGetClassObject(_ATL_MODULEW
*pm
, REFCLSID rclsid
,
259 REFIID riid
, LPVOID
*ppv
)
261 _ATL_OBJMAP_ENTRYW_V1
*obj
;
263 HRESULT hres
= CLASS_E_CLASSNOTAVAILABLE
;
265 TRACE("%p %s %s %p\n", pm
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
270 for (i
= 0; (obj
= get_objmap_entry( pm
, i
)) != NULL
; i
++)
272 if (IsEqualCLSID(obj
->pclsid
, rclsid
))
274 TRACE("found object %i\n", i
);
275 if (obj
->pfnGetClassObject
)
278 hres
= obj
->pfnGetClassObject(obj
->pfnCreateInstance
,
282 hres
= IUnknown_QueryInterface(obj
->pCF
, riid
, ppv
);
288 WARN("no class object found for %s\n", debugstr_guid(rclsid
));
293 /***********************************************************************
294 * AtlModuleGetClassObject [ATL.@]
296 HRESULT WINAPI
AtlModuleRegisterTypeLib(_ATL_MODULEW
*pm
, LPCOLESTR lpszIndex
)
298 TRACE("%p %s\n", pm
, debugstr_w(lpszIndex
));
303 return AtlRegisterTypeLib(pm
->m_hInstTypeLib
, lpszIndex
);
306 /***********************************************************************
307 * AtlModuleRevokeClassObjects [ATL.@]
309 HRESULT WINAPI
AtlModuleRevokeClassObjects(_ATL_MODULEW
*pm
)
315 /***********************************************************************
316 * AtlModuleUnregisterServer [ATL.@]
318 HRESULT WINAPI
AtlModuleUnregisterServer(_ATL_MODULEW
*pm
, const CLSID
*clsid
)
320 FIXME("%p %s\n", pm
, debugstr_guid(clsid
));
324 /***********************************************************************
325 * AtlModuleRegisterWndClassInfoA [ATL.@]
327 * See AtlModuleRegisterWndClassInfoW.
329 ATOM WINAPI
AtlModuleRegisterWndClassInfoA(_ATL_MODULEA
*pm
, _ATL_WNDCLASSINFOA
*wci
, WNDPROC
*pProc
)
333 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
340 TRACE("wci->m_wc.lpszClassName = %s\n", wci
->m_wc
.lpszClassName
);
342 if (wci
->m_lpszOrigName
)
343 FIXME( "subclassing %s not implemented\n", debugstr_a(wci
->m_lpszOrigName
));
345 if (!wci
->m_wc
.lpszClassName
)
347 snprintf(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
), "ATL%08lx", (UINT_PTR
)wci
);
348 TRACE("auto-generated class name %s\n", wci
->m_szAutoName
);
349 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
352 atom
= GetClassInfoExA(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
355 wci
->m_wc
.hInstance
= pm
->m_hInst
;
356 wci
->m_wc
.hCursor
= LoadCursorA( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
357 wci
->m_lpszCursorID
);
358 atom
= RegisterClassExA(&wci
->m_wc
);
360 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
364 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
366 TRACE("returning 0x%04x\n", atom
);
370 /***********************************************************************
371 * AtlModuleRegisterWndClassInfoW [ATL.@]
374 * pm [IO] Information about the module registering the window.
375 * wci [IO] Information about the window being registered.
376 * pProc [O] Window procedure of the registered class.
379 * Atom representing the registered class.
382 * Can be called multiple times without error, unlike RegisterClassEx().
384 * If the class name is NULL, then a class with a name of "ATLxxxxxxxx" is
385 * registered, where the 'x's represent a unique value.
388 ATOM WINAPI
AtlModuleRegisterWndClassInfoW(_ATL_MODULEW
*pm
, _ATL_WNDCLASSINFOW
*wci
, WNDPROC
*pProc
)
392 FIXME("%p %p %p semi-stub\n", pm
, wci
, pProc
);
399 TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci
->m_wc
.lpszClassName
));
401 if (wci
->m_lpszOrigName
)
402 FIXME( "subclassing %s not implemented\n", debugstr_w(wci
->m_lpszOrigName
));
404 if (!wci
->m_wc
.lpszClassName
)
406 static const WCHAR szFormat
[] = {'A','T','L','%','0','8','l','x',0};
407 snprintfW(wci
->m_szAutoName
, sizeof(wci
->m_szAutoName
)/sizeof(WCHAR
), szFormat
, (UINT_PTR
)wci
);
408 TRACE("auto-generated class name %s\n", debugstr_w(wci
->m_szAutoName
));
409 wci
->m_wc
.lpszClassName
= wci
->m_szAutoName
;
412 atom
= GetClassInfoExW(pm
->m_hInst
, wci
->m_wc
.lpszClassName
, &wc
);
415 wci
->m_wc
.hInstance
= pm
->m_hInst
;
416 wci
->m_wc
.hCursor
= LoadCursorW( wci
->m_bSystemCursor
? NULL
: pm
->m_hInst
,
417 wci
->m_lpszCursorID
);
418 atom
= RegisterClassExW(&wci
->m_wc
);
420 wci
->pWndProc
= wci
->m_wc
.lpfnWndProc
;
424 if (wci
->m_lpszOrigName
) *pProc
= wci
->pWndProc
;
426 TRACE("returning 0x%04x\n", atom
);
430 /***********************************************************************
431 * AtlModuleAddCreateWndData [ATL.@]
433 void WINAPI
AtlModuleAddCreateWndData(_ATL_MODULEW
*pM
, _AtlCreateWndData
*pData
, void* pvObject
)
435 TRACE("(%p, %p, %p)\n", pM
, pData
, pvObject
);
437 pData
->m_pThis
= pvObject
;
438 pData
->m_dwThreadID
= GetCurrentThreadId();
440 EnterCriticalSection(&pM
->m_csWindowCreate
);
441 pData
->m_pNext
= pM
->m_pCreateWndList
;
442 pM
->m_pCreateWndList
= pData
;
443 LeaveCriticalSection(&pM
->m_csWindowCreate
);
446 /***********************************************************************
447 * AtlModuleExtractCreateWndData [ATL.@]
449 * NOTE: Tests show that this function extracts one of _AtlCreateWndData
450 * records from the current thread from a list
453 void* WINAPI
AtlModuleExtractCreateWndData(_ATL_MODULEW
*pM
)
455 _AtlCreateWndData
**ppData
;
460 EnterCriticalSection(&pM
->m_csWindowCreate
);
462 for(ppData
= &pM
->m_pCreateWndList
; *ppData
!=NULL
; ppData
= &(*ppData
)->m_pNext
)
464 if ((*ppData
)->m_dwThreadID
== GetCurrentThreadId())
466 _AtlCreateWndData
*pData
= *ppData
;
467 *ppData
= pData
->m_pNext
;
468 ret
= pData
->m_pThis
;
473 LeaveCriticalSection(&pM
->m_csWindowCreate
);
477 /***********************************************************************
478 * AtlModuleUpdateRegistryFromResourceD [ATL.@]
481 HRESULT WINAPI
AtlModuleUpdateRegistryFromResourceD(_ATL_MODULEW
* pM
, LPCOLESTR lpszRes
,
482 BOOL bRegister
, struct _ATL_REGMAP_ENTRY
* pMapEntries
, IRegistrar
* pReg
)
484 TRACE("(%p %s %d %p %p)\n", pM
, debugstr_w(lpszRes
), bRegister
, pMapEntries
, pReg
);
486 return AtlUpdateRegistryFromResourceD(pM
->m_hInst
, lpszRes
, bRegister
, pMapEntries
, pReg
);
489 static HRESULT WINAPI
RegistrarCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppvObject
)
491 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
493 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IClassFactory
, riid
)) {
495 IClassFactory_AddRef( iface
);
499 return E_NOINTERFACE
;
502 static ULONG WINAPI
RegistrarCF_AddRef(IClassFactory
*iface
)
507 static ULONG WINAPI
RegistrarCF_Release(IClassFactory
*iface
)
512 static HRESULT WINAPI
RegistrarCF_CreateInstance(IClassFactory
*iface
, LPUNKNOWN pUnkOuter
,
513 REFIID riid
, void **ppv
)
515 IRegistrar
*registrar
;
518 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
522 return CLASS_E_NOAGGREGATION
;
525 hres
= AtlCreateRegistrar(®istrar
);
529 hres
= IRegistrar_QueryInterface(registrar
, riid
, ppv
);
530 IRegistrar_Release(registrar
);
534 static HRESULT WINAPI
RegistrarCF_LockServer(IClassFactory
*iface
, BOOL lock
)
536 TRACE("(%p)->(%x)\n", iface
, lock
);
540 static const IClassFactoryVtbl IRegistrarCFVtbl
= {
541 RegistrarCF_QueryInterface
,
544 RegistrarCF_CreateInstance
,
545 RegistrarCF_LockServer
548 static IClassFactory RegistrarCF
= { &IRegistrarCFVtbl
};
550 /**************************************************************
551 * DllGetClassObject (ATL.2)
553 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID riid
, LPVOID
*ppvObject
)
555 TRACE("(%s %s %p)\n", debugstr_guid(clsid
), debugstr_guid(riid
), ppvObject
);
557 if(IsEqualGUID(&CLSID_Registrar
, clsid
))
558 return IClassFactory_QueryInterface( &RegistrarCF
, riid
, ppvObject
);
560 FIXME("Not supported class %s\n", debugstr_guid(clsid
));
561 return CLASS_E_CLASSNOTAVAILABLE
;
564 /***********************************************************************
565 * DllRegisterServer (ATL.@)
567 HRESULT WINAPI
DllRegisterServer(void)
569 return __wine_register_resources( hInst
);
572 /***********************************************************************
573 * DllUnRegisterServer (ATL.@)
575 HRESULT WINAPI
DllUnregisterServer(void)
577 return __wine_unregister_resources( hInst
);
580 /***********************************************************************
581 * DllCanUnloadNow (ATL.@)
583 HRESULT WINAPI
DllCanUnloadNow(void)
588 /***********************************************************************
589 * AtlGetVersion [ATL.@]
591 DWORD WINAPI
AtlGetVersion(void *pReserved
)
593 TRACE("version %04x (%p)\n", _ATL_VER
, pReserved
);
597 /**********************************************************************
598 * AtlAxWin class window procedure
600 static LRESULT CALLBACK
AtlAxWin_wndproc( HWND hWnd
, UINT wMsg
, WPARAM wParam
, LPARAM lParam
)
602 if ( wMsg
== WM_CREATE
)
604 DWORD len
= GetWindowTextLengthW( hWnd
) + 1;
605 WCHAR
*ptr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
608 GetWindowTextW( hWnd
, ptr
, len
);
609 AtlAxCreateControlEx( ptr
, hWnd
, NULL
, NULL
, NULL
, NULL
, NULL
);
610 HeapFree( GetProcessHeap(), 0, ptr
);
613 return DefWindowProcW( hWnd
, wMsg
, wParam
, lParam
);
616 BOOL WINAPI
AtlAxWinInit(void)
619 const WCHAR AtlAxWin
[] = {'A','t','l','A','x','W','i','n',0};
621 FIXME("version %04x semi-stub\n", _ATL_VER
);
623 if ( FAILED( OleInitialize(NULL
) ) )
626 wcex
.cbSize
= sizeof(wcex
);
627 wcex
.style
= CS_GLOBALCLASS
;
630 wcex
.hInstance
= GetModuleHandleW( NULL
);
633 wcex
.hbrBackground
= NULL
;
634 wcex
.lpszMenuName
= NULL
;
637 wcex
.lpfnWndProc
= AtlAxWin_wndproc
;
638 wcex
.lpszClassName
= AtlAxWin
;
639 if ( !RegisterClassExW( &wcex
) )