ws2_32: Cope with invalid hints in getaddrinfo/GetAddrInfoW.
[wine.git] / dlls / atl / atl_main.c
blob509d4bb95f6c0ee39d39f4132b2153ddc65ccbc0
1 /*
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 #include <stdio.h>
24 #define COBJMACROS
26 #include "objidl.h"
27 #include "rpcproxy.h"
28 #include "atlbase.h"
29 #include "atlwin.h"
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);
44 hInst = hinstDLL;
46 return TRUE;
49 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
51 HRESULT WINAPI AtlModuleInit(_ATL_MODULEW* pM, _ATL_OBJMAP_ENTRYW* p, HINSTANCE h)
53 INT i;
54 UINT size;
56 TRACE("(%p %p %p)\n", pM, p, h);
58 size = pM->cbSize;
59 switch (size)
61 case ATLVer1Size:
62 case sizeof(_ATL_MODULEW):
63 #ifdef _WIN64
64 case sizeof(_ATL_MODULEW) + sizeof(void *):
65 #endif
66 break;
67 default:
68 WARN("Unknown structure version (size %i)\n",size);
69 return E_INVALIDARG;
72 memset(pM,0,pM->cbSize);
73 pM->cbSize = size;
74 pM->m_hInst = h;
75 pM->m_hInstResource = h;
76 pM->m_hInstTypeLib = h;
77 pM->m_pObjMap = p;
78 pM->m_hHeap = GetProcessHeap();
80 InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
81 InitializeCriticalSection(&pM->m_csWindowCreate);
82 InitializeCriticalSection(&pM->m_csObjMap);
84 /* call mains */
85 i = 0;
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);
93 i++;
97 return S_OK;
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;
106 else
107 ret = (_ATL_OBJMAP_ENTRYW_V1 *)(mod->m_pObjMap + index);
109 if (!ret->pclsid) ret = NULL;
110 return ret;
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);
118 if (!pM)
119 return E_INVALIDARG;
121 return AtlLoadTypeLib(pM->m_hInstTypeLib, lpszIndex, pbstrPath, ppTypeLib);
124 HRESULT WINAPI AtlModuleTerm(_ATL_MODULE *pM)
126 _ATL_TERMFUNC_ELEM *iter, *tmp;
128 TRACE("(%p)\n", pM);
130 if (pM->cbSize > ATLVer1Size)
132 iter = pM->m_pTermFuncs;
134 while(iter) {
135 iter->pFunc(iter->dw);
136 tmp = iter;
137 iter = iter->pNext;
138 HeapFree(GetProcessHeap(), 0, tmp);
142 return S_OK;
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;
161 return S_OK;
164 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEW *pM, DWORD dwClsContext,
165 DWORD dwFlags)
167 _ATL_OBJMAP_ENTRYW_V1 *obj;
168 int i=0;
170 TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
172 if (pM == NULL)
173 return E_INVALIDARG;
175 while ((obj = get_objmap_entry( pM, i++ )))
177 IUnknown* pUnknown;
178 HRESULT rc;
180 TRACE("Registering object %i\n",i);
181 if (obj->pfnGetClassObject)
183 rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
184 (LPVOID*)&pUnknown);
185 if (SUCCEEDED (rc) )
187 rc = CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
188 dwFlags, &obj->dwRegister);
190 if (FAILED (rc) )
191 WARN("Failed to register object %i: 0x%08x\n", i, rc);
193 if (pUnknown)
194 IUnknown_Release(pUnknown);
199 return S_OK;
202 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEW* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
204 FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
205 return S_OK;
208 /***********************************************************************
209 * AtlModuleRegisterServer [ATL.@]
212 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid)
214 const _ATL_OBJMAP_ENTRYW_V1 *obj;
215 int i;
216 HRESULT hRes;
218 TRACE("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
220 if (pM == NULL)
221 return E_INVALIDARG;
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 */
229 if (FAILED(hRes))
230 return hRes;
232 if(pM->cbSize > ATLVer1Size) {
233 const struct _ATL_CATMAP_ENTRY *catmap;
235 catmap = ((const _ATL_OBJMAP_ENTRYW*)obj)->pfnGetCategoryMap();
236 if(catmap) {
237 hRes = AtlRegisterClassCategoriesHelper(obj->pclsid, catmap, TRUE);
238 if(FAILED(hRes))
239 return hRes;
245 if (bRegTypeLib)
247 hRes = AtlRegisterTypeLib(pM->m_hInstTypeLib, NULL);
248 if (FAILED(hRes))
249 return hRes;
252 return S_OK;
255 /***********************************************************************
256 * AtlModuleGetClassObject [ATL.@]
258 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
259 REFIID riid, LPVOID *ppv)
261 _ATL_OBJMAP_ENTRYW_V1 *obj;
262 int i;
263 HRESULT hres = CLASS_E_CLASSNOTAVAILABLE;
265 TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
267 if (pm == NULL)
268 return E_INVALIDARG;
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)
277 if (!obj->pCF)
278 hres = obj->pfnGetClassObject(obj->pfnCreateInstance,
279 &IID_IUnknown,
280 (void **)&obj->pCF);
281 if (obj->pCF)
282 hres = IUnknown_QueryInterface(obj->pCF, riid, ppv);
283 break;
288 WARN("no class object found for %s\n", debugstr_guid(rclsid));
290 return hres;
293 /***********************************************************************
294 * AtlModuleGetClassObject [ATL.@]
296 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
298 TRACE("%p %s\n", pm, debugstr_w(lpszIndex));
300 if (!pm)
301 return E_INVALIDARG;
303 return AtlRegisterTypeLib(pm->m_hInstTypeLib, lpszIndex);
306 /***********************************************************************
307 * AtlModuleRevokeClassObjects [ATL.@]
309 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
311 FIXME("%p\n", pm);
312 return E_FAIL;
315 /***********************************************************************
316 * AtlModuleUnregisterServer [ATL.@]
318 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
320 FIXME("%p %s\n", pm, debugstr_guid(clsid));
321 return E_FAIL;
324 /***********************************************************************
325 * AtlModuleRegisterWndClassInfoA [ATL.@]
327 * See AtlModuleRegisterWndClassInfoW.
329 ATOM WINAPI AtlModuleRegisterWndClassInfoA(_ATL_MODULEA *pm, _ATL_WNDCLASSINFOA *wci, WNDPROC *pProc)
331 ATOM atom;
333 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
335 atom = wci->m_atom;
336 if (!atom)
338 WNDCLASSEXA wc;
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);
353 if (!atom)
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;
361 wci->m_atom = atom;
364 if (wci->m_lpszOrigName) *pProc = wci->pWndProc;
366 TRACE("returning 0x%04x\n", atom);
367 return atom;
370 /***********************************************************************
371 * AtlModuleRegisterWndClassInfoW [ATL.@]
373 * PARAMS
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.
378 * RETURNS
379 * Atom representing the registered class.
381 * NOTES
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)
390 ATOM atom;
392 FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
394 atom = wci->m_atom;
395 if (!atom)
397 WNDCLASSEXW wc;
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);
413 if (!atom)
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;
421 wci->m_atom = atom;
424 if (wci->m_lpszOrigName) *pProc = wci->pWndProc;
426 TRACE("returning 0x%04x\n", atom);
427 return 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;
456 void *ret = NULL;
458 TRACE("(%p)\n", pM);
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;
469 break;
473 LeaveCriticalSection(&pM->m_csWindowCreate);
474 return ret;
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)) {
494 *ppvObject = iface;
495 IClassFactory_AddRef( iface );
496 return S_OK;
499 return E_NOINTERFACE;
502 static ULONG WINAPI RegistrarCF_AddRef(IClassFactory *iface)
504 return 2;
507 static ULONG WINAPI RegistrarCF_Release(IClassFactory *iface)
509 return 1;
512 static HRESULT WINAPI RegistrarCF_CreateInstance(IClassFactory *iface, LPUNKNOWN pUnkOuter,
513 REFIID riid, void **ppv)
515 IRegistrar *registrar;
516 HRESULT hres;
518 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
520 if(pUnkOuter) {
521 *ppv = NULL;
522 return CLASS_E_NOAGGREGATION;
525 hres = AtlCreateRegistrar(&registrar);
526 if(FAILED(hres))
527 return hres;
529 hres = IRegistrar_QueryInterface(registrar, riid, ppv);
530 IRegistrar_Release(registrar);
531 return hres;
534 static HRESULT WINAPI RegistrarCF_LockServer(IClassFactory *iface, BOOL lock)
536 TRACE("(%p)->(%x)\n", iface, lock);
537 return S_OK;
540 static const IClassFactoryVtbl IRegistrarCFVtbl = {
541 RegistrarCF_QueryInterface,
542 RegistrarCF_AddRef,
543 RegistrarCF_Release,
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)
585 return S_FALSE;
588 /***********************************************************************
589 * AtlGetVersion [ATL.@]
591 DWORD WINAPI AtlGetVersion(void *pReserved)
593 TRACE("version %04x (%p)\n", _ATL_VER, pReserved);
594 return _ATL_VER;
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) );
606 if (!ptr)
607 return 1;
608 GetWindowTextW( hWnd, ptr, len );
609 AtlAxCreateControlEx( ptr, hWnd, NULL, NULL, NULL, NULL, NULL );
610 HeapFree( GetProcessHeap(), 0, ptr );
611 return 0;
613 return DefWindowProcW( hWnd, wMsg, wParam, lParam );
616 BOOL WINAPI AtlAxWinInit(void)
618 WNDCLASSEXW wcex;
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) ) )
624 return FALSE;
626 wcex.cbSize = sizeof(wcex);
627 wcex.style = CS_GLOBALCLASS;
628 wcex.cbClsExtra = 0;
629 wcex.cbWndExtra = 0;
630 wcex.hInstance = GetModuleHandleW( NULL );
631 wcex.hIcon = NULL;
632 wcex.hCursor = NULL;
633 wcex.hbrBackground = NULL;
634 wcex.lpszMenuName = NULL;
635 wcex.hIconSm = 0;
637 wcex.lpfnWndProc = AtlAxWin_wndproc;
638 wcex.lpszClassName = AtlAxWin;
639 if ( !RegisterClassExW( &wcex ) )
640 return FALSE;
642 return TRUE;