2 * Copyright 2012 Stefan Leichter
3 * Copyright 2012 Jacek Caban for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(atl100);
29 typedef unsigned char cpp_bool;
31 static inline void *heap_alloc(size_t len)
33 return HeapAlloc(GetProcessHeap(), 0, len);
36 static inline BOOL heap_free(void *mem)
38 return HeapFree(GetProcessHeap(), 0, mem);
41 static ICatRegister *catreg;
43 /***********************************************************************
44 * AtlAdvise [atl100.@]
46 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, DWORD *pdw)
48 IConnectionPointContainer *container;
52 TRACE("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
54 hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
58 hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
59 IConnectionPointContainer_Release(container);
63 hres = IConnectionPoint_Advise(cp, pUnk, pdw);
64 IConnectionPoint_Release(cp);
68 /***********************************************************************
69 * AtlUnadvise [atl100.@]
71 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
73 IConnectionPointContainer *container;
77 TRACE("%p %p %d\n", pUnkCP, iid, dw);
79 hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
83 hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
84 IConnectionPointContainer_Release(container);
88 hres = IConnectionPoint_Unadvise(cp, dw);
89 IConnectionPoint_Release(cp);
93 /***********************************************************************
94 * AtlFreeMarshalStream [atl100.@]
96 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
102 /***********************************************************************
103 * AtlMarshalPtrInProc [atl100.@]
105 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
107 FIXME("%p %p %p\n", pUnk, iid, pstm);
111 /***********************************************************************
112 * AtlUnmarshalPtr [atl100.@]
114 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
116 FIXME("%p %p %p\n", stm, iid, ppUnk);
120 /***********************************************************************
121 * AtlCreateTargetDC [atl100.@]
123 HDC WINAPI AtlCreateTargetDC( HDC hdc, DVTARGETDEVICE *dv )
125 static const WCHAR displayW[] = {'d','i','s','p','l','a','y',0};
126 const WCHAR *driver = NULL, *device = NULL, *port = NULL;
127 DEVMODEW *devmode = NULL;
129 TRACE( "(%p, %p)\n", hdc, dv );
133 if (dv->tdDriverNameOffset) driver = (WCHAR *)((char *)dv + dv->tdDriverNameOffset);
134 if (dv->tdDeviceNameOffset) device = (WCHAR *)((char *)dv + dv->tdDeviceNameOffset);
135 if (dv->tdPortNameOffset) port = (WCHAR *)((char *)dv + dv->tdPortNameOffset);
136 if (dv->tdExtDevmodeOffset) devmode = (DEVMODEW *)((char *)dv + dv->tdExtDevmodeOffset);
143 return CreateDCW( driver, device, port, devmode );
146 /***********************************************************************
147 * AtlHiMetricToPixel [atl100.@]
149 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
151 HDC dc = GetDC(NULL);
152 lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
153 lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
154 ReleaseDC( NULL, dc );
157 /***********************************************************************
158 * AtlPixelToHiMetric [atl100.@]
160 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
162 HDC dc = GetDC(NULL);
163 lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
164 lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
165 ReleaseDC( NULL, dc );
168 /***********************************************************************
169 * AtlComPtrAssign [atl100.@]
171 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
173 TRACE("(%p %p)\n", pp, p);
175 if (p) IUnknown_AddRef(p);
176 if (*pp) IUnknown_Release(*pp);
181 /***********************************************************************
182 * AtlComQIPtrAssign [atl100.@]
184 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
186 IUnknown *new_p = NULL;
188 TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
190 if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
191 if (*pp) IUnknown_Release(*pp);
196 /***********************************************************************
197 * AtlInternalQueryInterface [atl100.@]
199 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
202 HRESULT rc = E_NOINTERFACE;
203 TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
205 if (IsEqualGUID(iid,&IID_IUnknown))
207 TRACE("Returning IUnknown\n");
208 *ppvObject = ((LPSTR)this+pEntries[0].dw);
209 IUnknown_AddRef((IUnknown*)*ppvObject);
213 while (pEntries[i].pFunc != 0)
215 TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
216 pEntries[i].dw, pEntries[i].pFunc);
218 if (!pEntries[i].piid || IsEqualGUID(iid,pEntries[i].piid))
221 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
224 *ppvObject = ((LPSTR)this+pEntries[i].dw);
225 IUnknown_AddRef((IUnknown*)*ppvObject);
231 rc = pEntries[i].pFunc(this, iid, ppvObject, pEntries[i].dw);
232 if(rc==S_OK || pEntries[i].piid)
238 TRACE("Done returning (0x%x)\n",rc);
242 /* FIXME: should be in a header file */
243 typedef struct ATL_PROPMAP_ENTRY
247 const CLSID* pclsidPropPage;
248 const IID* piidDispatch;
254 /***********************************************************************
255 * AtlIPersistStreamInit_Load [atl100.@]
257 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
258 void *pThis, IUnknown *pUnk)
260 FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
265 /***********************************************************************
266 * AtlIPersistStreamInit_Save [atl100.@]
268 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
269 ATL_PROPMAP_ENTRY *pMap, void *pThis,
272 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
277 /***********************************************************************
278 * AtlModuleAddTermFunc [atl100.@]
280 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULE *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
282 _ATL_TERMFUNC_ELEM *termfunc_elem;
284 TRACE("(%p %p %ld)\n", pM, pFunc, dw);
286 termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
287 termfunc_elem->pFunc = pFunc;
288 termfunc_elem->dw = dw;
289 termfunc_elem->pNext = pM->m_pTermFuncs;
291 pM->m_pTermFuncs = termfunc_elem;
296 /***********************************************************************
297 * AtlCallTermFunc [atl100.@]
299 void WINAPI AtlCallTermFunc(_ATL_MODULE *pM)
301 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
306 iter->pFunc(iter->dw);
309 HeapFree(GetProcessHeap(), 0, tmp);
312 pM->m_pTermFuncs = NULL;
315 /***********************************************************************
316 * AtlLoadTypeLib [atl100.56]
318 HRESULT WINAPI AtlLoadTypeLib(HINSTANCE inst, LPCOLESTR lpszIndex,
319 BSTR *pbstrPath, ITypeLib **ppTypeLib)
321 size_t path_len, index_len;
322 ITypeLib *typelib = NULL;
326 static const WCHAR tlb_extW[] = {'.','t','l','b',0};
328 TRACE("(%p %s %p %p)\n", inst, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
330 index_len = lpszIndex ? strlenW(lpszIndex) : 0;
331 path = heap_alloc((MAX_PATH+index_len)*sizeof(WCHAR) + sizeof(tlb_extW));
333 return E_OUTOFMEMORY;
335 path_len = GetModuleFileNameW(inst, path, MAX_PATH);
338 return HRESULT_FROM_WIN32(GetLastError());
342 memcpy(path+path_len, lpszIndex, (index_len+1)*sizeof(WCHAR));
344 hres = LoadTypeLib(path, &typelib);
348 for(ptr = path+path_len-1; ptr > path && *ptr != '\\' && *ptr != '.'; ptr--);
351 memcpy(ptr, tlb_extW, sizeof(tlb_extW));
352 hres = LoadTypeLib(path, &typelib);
355 if(SUCCEEDED(hres)) {
356 *pbstrPath = SysAllocString(path);
358 ITypeLib_Release(typelib);
359 hres = E_OUTOFMEMORY;
367 *ppTypeLib = typelib;
371 /***********************************************************************
372 * AtlWinModuleInit [atl100.65]
374 HRESULT WINAPI AtlWinModuleInit(_ATL_WIN_MODULE *winmod)
376 TRACE("(%p\n", winmod);
378 if(winmod->cbSize != sizeof(*winmod))
381 InitializeCriticalSection(&winmod->m_csWindowCreate);
382 winmod->m_pCreateWndList = NULL;
386 /***********************************************************************
387 * AtlWinModuleAddCreateWndData [atl100.43]
389 void WINAPI AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE *pM, _AtlCreateWndData *pData, void *pvObject)
391 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
393 pData->m_pThis = pvObject;
394 pData->m_dwThreadID = GetCurrentThreadId();
396 EnterCriticalSection(&pM->m_csWindowCreate);
397 pData->m_pNext = pM->m_pCreateWndList;
398 pM->m_pCreateWndList = pData;
399 LeaveCriticalSection(&pM->m_csWindowCreate);
402 /***********************************************************************
403 * AtlWinModuleExtractCreateWndData [atl100.44]
405 void* WINAPI AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE *winmod)
407 _AtlCreateWndData *iter, *prev = NULL;
410 TRACE("(%p)\n", winmod);
412 thread_id = GetCurrentThreadId();
414 EnterCriticalSection(&winmod->m_csWindowCreate);
416 for(iter = winmod->m_pCreateWndList; iter && iter->m_dwThreadID != thread_id; iter = iter->m_pNext)
420 prev->m_pNext = iter->m_pNext;
422 winmod->m_pCreateWndList = iter->m_pNext;
425 LeaveCriticalSection(&winmod->m_csWindowCreate);
427 return iter ? iter->m_pThis : NULL;
430 /***********************************************************************
431 * AtlComModuleGetClassObject [atl100.15]
433 HRESULT WINAPI AtlComModuleGetClassObject(_ATL_COM_MODULE *pm, REFCLSID rclsid, REFIID riid, void **ppv)
435 _ATL_OBJMAP_ENTRY **iter;
438 TRACE("(%p %s %s %p)\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
443 for(iter = pm->m_ppAutoObjMapFirst; iter < pm->m_ppAutoObjMapLast; iter++) {
444 if(IsEqualCLSID((*iter)->pclsid, rclsid) && (*iter)->pfnGetClassObject) {
446 hres = (*iter)->pfnGetClassObject((*iter)->pfnCreateInstance, &IID_IUnknown, (void**)&(*iter)->pCF);
448 hres = IUnknown_QueryInterface((*iter)->pCF, riid, ppv);
449 TRACE("returning %p (%08x)\n", *ppv, hres);
454 WARN("Class %s not found\n", debugstr_guid(rclsid));
455 return CLASS_E_CLASSNOTAVAILABLE;
458 /***********************************************************************
459 * AtlComModuleUnregisterServer [atl100.22]
461 HRESULT WINAPI AtlComModuleUnregisterServer(_ATL_COM_MODULE *mod, BOOL bRegTypeLib, const CLSID *clsid)
463 const struct _ATL_CATMAP_ENTRY *catmap;
464 _ATL_OBJMAP_ENTRY **iter;
467 TRACE("(%p %x %s)\n", mod, bRegTypeLib, debugstr_guid(clsid));
469 for(iter = mod->m_ppAutoObjMapFirst; iter < mod->m_ppAutoObjMapLast; iter++) {
470 if(!*iter || (clsid && !IsEqualCLSID((*iter)->pclsid, clsid)))
473 TRACE("Unregistering clsid %s\n", debugstr_guid((*iter)->pclsid));
475 catmap = (*iter)->pfnGetCategoryMap();
477 hres = AtlRegisterClassCategoriesHelper((*iter)->pclsid, catmap, FALSE);
482 hres = (*iter)->pfnUpdateRegistry(FALSE);
492 hres = AtlLoadTypeLib(mod->m_hInstTypeLib, NULL, &path, &typelib);
497 hres = ITypeLib_GetLibAttr(typelib, &attr);
498 if(SUCCEEDED(hres)) {
499 hres = UnRegisterTypeLib(&attr->guid, attr->wMajorVerNum, attr->wMinorVerNum, attr->lcid, attr->syskind);
500 ITypeLib_ReleaseTLibAttr(typelib, attr);
502 ITypeLib_Release(typelib);
510 /***********************************************************************
511 * AtlRegisterClassCategoriesHelper [atl100.49]
513 HRESULT WINAPI AtlRegisterClassCategoriesHelper(REFCLSID clsid, const struct _ATL_CATMAP_ENTRY *catmap, BOOL reg)
515 const struct _ATL_CATMAP_ENTRY *iter;
518 TRACE("(%s %p %x)\n", debugstr_guid(clsid), catmap, reg);
524 ICatRegister *new_catreg;
526 hres = CoCreateInstance(&CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER,
527 &IID_ICatRegister, (void**)&new_catreg);
531 if(InterlockedCompareExchangePointer((void**)&catreg, new_catreg, NULL))
532 ICatRegister_Release(new_catreg);
535 for(iter = catmap; iter->iType != _ATL_CATMAP_ENTRY_END; iter++) {
536 CATID catid = *iter->pcatid; /* For stupid lack of const in ICatRegister declaration. */
538 if(iter->iType == _ATL_CATMAP_ENTRY_IMPLEMENTED) {
540 hres = ICatRegister_RegisterClassImplCategories(catreg, clsid, 1, &catid);
542 hres = ICatRegister_UnRegisterClassImplCategories(catreg, clsid, 1, &catid);
545 hres = ICatRegister_RegisterClassReqCategories(catreg, clsid, 1, &catid);
547 hres = ICatRegister_UnRegisterClassReqCategories(catreg, clsid, 1, &catid);
554 WCHAR reg_path[256] = {'C','L','S','I','D','\\'}, *ptr = reg_path+6;
556 static const WCHAR implemented_catW[] =
557 {'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
558 static const WCHAR required_catW[] =
559 {'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
561 ptr += StringFromGUID2(clsid, ptr, 64)-1;
564 memcpy(ptr, implemented_catW, sizeof(implemented_catW));
565 RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
567 memcpy(ptr, required_catW, sizeof(required_catW));
568 RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
574 /***********************************************************************
575 * AtlWaitWithMessageLoop [atl100.24]
577 BOOL WINAPI AtlWaitWithMessageLoop(HANDLE handle)
582 TRACE("(%p)\n", handle);
585 res = MsgWaitForMultipleObjects(1, &handle, FALSE, INFINITE, QS_ALLINPUT);
589 case WAIT_OBJECT_0+1:
590 if(GetMessageW(&msg, NULL, 0, 0) < 0)
593 TranslateMessage(&msg);
594 DispatchMessageW(&msg);
602 static HRESULT get_default_source(ITypeLib *typelib, const CLSID *clsid, IID *iid)
604 ITypeInfo *typeinfo, *src_typeinfo = NULL;
610 hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, &typeinfo);
614 hres = ITypeInfo_GetTypeAttr(typeinfo, &attr);
616 ITypeInfo_Release(typeinfo);
620 for(i=0; i < attr->cImplTypes; i++) {
621 hres = ITypeInfo_GetImplTypeFlags(typeinfo, i, &type_flags);
622 if(SUCCEEDED(hres) && type_flags == (IMPLTYPEFLAG_FSOURCE|IMPLTYPEFLAG_FDEFAULT)) {
625 hres = ITypeInfo_GetRefTypeOfImplType(typeinfo, i, &ref);
627 hres = ITypeInfo_GetRefTypeInfo(typeinfo, ref, &src_typeinfo);
632 ITypeInfo_ReleaseTypeAttr(typeinfo, attr);
633 ITypeInfo_Release(typeinfo);
642 hres = ITypeInfo_GetTypeAttr(src_typeinfo, &attr);
643 if(SUCCEEDED(hres)) {
645 ITypeInfo_ReleaseTypeAttr(src_typeinfo, attr);
647 ITypeInfo_Release(src_typeinfo);
651 /***********************************************************************
652 * AtlGetObjectSourceInterface [atl100.54]
654 HRESULT WINAPI AtlGetObjectSourceInterface(IUnknown *unk, GUID *libid, IID *iid, unsigned short *major, unsigned short *minor)
656 IProvideClassInfo2 *classinfo;
663 TRACE("(%p %p %p %p %p)\n", unk, libid, iid, major, minor);
665 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
669 hres = IDispatch_GetTypeInfo(disp, 0, 0, &typeinfo);
670 IDispatch_Release(disp);
674 hres = ITypeInfo_GetContainingTypeLib(typeinfo, &typelib, 0);
675 ITypeInfo_Release(typeinfo);
676 if(SUCCEEDED(hres)) {
679 hres = ITypeLib_GetLibAttr(typelib, &attr);
680 if(SUCCEEDED(hres)) {
682 *major = attr->wMajorVerNum;
683 *minor = attr->wMinorVerNum;
684 ITypeLib_ReleaseTLibAttr(typelib, attr);
686 ITypeLib_Release(typelib);
692 hres = IUnknown_QueryInterface(unk, &IID_IProvideClassInfo2, (void**)&classinfo);
693 if(SUCCEEDED(hres)) {
694 hres = IProvideClassInfo2_GetGUID(classinfo, GUIDKIND_DEFAULT_SOURCE_DISP_IID, iid);
695 IProvideClassInfo2_Release(classinfo);
696 ITypeLib_Release(typelib);
700 hres = IUnknown_QueryInterface(unk, &IID_IPersist, (void**)&persist);
701 if(SUCCEEDED(hres)) {
704 hres = IPersist_GetClassID(persist, &clsid);
706 hres = get_default_source(typelib, &clsid, iid);
707 IPersist_Release(persist);
713 /***********************************************************************
714 * AtlGetPerUserRegistration [atl100.68]
716 HRESULT WINAPI AtlGetPerUserRegistration(cpp_bool *pbEnabled)
718 FIXME("stub: returning false\n");
723 /***********************************************************************
724 * AtlGetVersion [atl100.@]
726 DWORD WINAPI AtlGetVersion(void *pReserved)
731 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
733 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
736 case DLL_PROCESS_ATTACH:
737 DisableThreadLibraryCalls(hinstDLL);
739 case DLL_PROCESS_DETACH:
741 ICatRegister_Release(catreg);