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 static inline void *heap_alloc(size_t len)
31 return HeapAlloc(GetProcessHeap(), 0, len);
34 static inline BOOL heap_free(void *mem)
36 return HeapFree(GetProcessHeap(), 0, mem);
39 static ICatRegister *catreg;
41 /***********************************************************************
42 * AtlAdvise [atl100.@]
44 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, DWORD *pdw)
46 IConnectionPointContainer *container;
50 TRACE("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
52 hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
56 hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
57 IConnectionPointContainer_Release(container);
61 hres = IConnectionPoint_Advise(cp, pUnk, pdw);
62 IConnectionPoint_Release(cp);
66 /***********************************************************************
67 * AtlUnadvise [atl100.@]
69 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
71 IConnectionPointContainer *container;
75 TRACE("%p %p %d\n", pUnkCP, iid, dw);
77 hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
81 hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
82 IConnectionPointContainer_Release(container);
86 hres = IConnectionPoint_Unadvise(cp, dw);
87 IConnectionPoint_Release(cp);
91 /***********************************************************************
92 * AtlFreeMarshalStream [atl100.@]
94 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
100 /***********************************************************************
101 * AtlMarshalPtrInProc [atl100.@]
103 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
105 FIXME("%p %p %p\n", pUnk, iid, pstm);
109 /***********************************************************************
110 * AtlUnmarshalPtr [atl100.@]
112 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
114 FIXME("%p %p %p\n", stm, iid, ppUnk);
118 /***********************************************************************
119 * AtlCreateTargetDC [atl100.@]
121 HDC WINAPI AtlCreateTargetDC( HDC hdc, DVTARGETDEVICE *dv )
123 static const WCHAR displayW[] = {'d','i','s','p','l','a','y',0};
124 const WCHAR *driver = NULL, *device = NULL, *port = NULL;
125 DEVMODEW *devmode = NULL;
127 TRACE( "(%p, %p)\n", hdc, dv );
131 if (dv->tdDriverNameOffset) driver = (WCHAR *)((char *)dv + dv->tdDriverNameOffset);
132 if (dv->tdDeviceNameOffset) device = (WCHAR *)((char *)dv + dv->tdDeviceNameOffset);
133 if (dv->tdPortNameOffset) port = (WCHAR *)((char *)dv + dv->tdPortNameOffset);
134 if (dv->tdExtDevmodeOffset) devmode = (DEVMODEW *)((char *)dv + dv->tdExtDevmodeOffset);
141 return CreateDCW( driver, device, port, devmode );
144 /***********************************************************************
145 * AtlHiMetricToPixel [atl100.@]
147 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
149 HDC dc = GetDC(NULL);
150 lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
151 lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
152 ReleaseDC( NULL, dc );
155 /***********************************************************************
156 * AtlPixelToHiMetric [atl100.@]
158 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
160 HDC dc = GetDC(NULL);
161 lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
162 lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
163 ReleaseDC( NULL, dc );
166 /***********************************************************************
167 * AtlComPtrAssign [atl100.@]
169 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
171 TRACE("(%p %p)\n", pp, p);
173 if (p) IUnknown_AddRef(p);
174 if (*pp) IUnknown_Release(*pp);
179 /***********************************************************************
180 * AtlComQIPtrAssign [atl100.@]
182 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
184 IUnknown *new_p = NULL;
186 TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
188 if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
189 if (*pp) IUnknown_Release(*pp);
194 /***********************************************************************
195 * AtlInternalQueryInterface [atl100.@]
197 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
200 HRESULT rc = E_NOINTERFACE;
201 TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
203 if (IsEqualGUID(iid,&IID_IUnknown))
205 TRACE("Returning IUnknown\n");
206 *ppvObject = ((LPSTR)this+pEntries[0].dw);
207 IUnknown_AddRef((IUnknown*)*ppvObject);
211 while (pEntries[i].pFunc != 0)
213 TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
214 pEntries[i].dw, pEntries[i].pFunc);
216 if (!pEntries[i].piid || IsEqualGUID(iid,pEntries[i].piid))
219 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
222 *ppvObject = ((LPSTR)this+pEntries[i].dw);
223 IUnknown_AddRef((IUnknown*)*ppvObject);
229 rc = pEntries[i].pFunc(this, iid, ppvObject, pEntries[i].dw);
230 if(rc==S_OK || pEntries[i].piid)
236 TRACE("Done returning (0x%x)\n",rc);
240 /* FIXME: should be in a header file */
241 typedef struct ATL_PROPMAP_ENTRY
245 const CLSID* pclsidPropPage;
246 const IID* piidDispatch;
252 /***********************************************************************
253 * AtlIPersistStreamInit_Load [atl100.@]
255 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
256 void *pThis, IUnknown *pUnk)
258 FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
263 /***********************************************************************
264 * AtlIPersistStreamInit_Save [atl100.@]
266 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
267 ATL_PROPMAP_ENTRY *pMap, void *pThis,
270 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
275 /***********************************************************************
276 * AtlModuleAddTermFunc [atl100.@]
278 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULE *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
280 _ATL_TERMFUNC_ELEM *termfunc_elem;
282 TRACE("(%p %p %ld)\n", pM, pFunc, dw);
284 termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
285 termfunc_elem->pFunc = pFunc;
286 termfunc_elem->dw = dw;
287 termfunc_elem->pNext = pM->m_pTermFuncs;
289 pM->m_pTermFuncs = termfunc_elem;
294 /***********************************************************************
295 * AtlCallTermFunc [atl100.@]
297 void WINAPI AtlCallTermFunc(_ATL_MODULE *pM)
299 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
304 iter->pFunc(iter->dw);
307 HeapFree(GetProcessHeap(), 0, tmp);
310 pM->m_pTermFuncs = NULL;
313 /***********************************************************************
314 * AtlLoadTypeLib [atl100.56]
316 HRESULT WINAPI AtlLoadTypeLib(HINSTANCE inst, LPCOLESTR lpszIndex,
317 BSTR *pbstrPath, ITypeLib **ppTypeLib)
319 size_t path_len, index_len;
320 ITypeLib *typelib = NULL;
324 static const WCHAR tlb_extW[] = {'.','t','l','b',0};
326 TRACE("(%p %s %p %p)\n", inst, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
328 index_len = lpszIndex ? strlenW(lpszIndex) : 0;
329 path = heap_alloc((MAX_PATH+index_len)*sizeof(WCHAR) + sizeof(tlb_extW));
331 return E_OUTOFMEMORY;
333 path_len = GetModuleFileNameW(inst, path, MAX_PATH);
336 return HRESULT_FROM_WIN32(GetLastError());
340 memcpy(path+path_len, lpszIndex, (index_len+1)*sizeof(WCHAR));
342 hres = LoadTypeLib(path, &typelib);
346 for(ptr = path+path_len-1; ptr > path && *ptr != '\\' && *ptr != '.'; ptr--);
349 memcpy(ptr, tlb_extW, sizeof(tlb_extW));
350 hres = LoadTypeLib(path, &typelib);
353 if(SUCCEEDED(hres)) {
354 *pbstrPath = SysAllocString(path);
356 ITypeLib_Release(typelib);
357 hres = E_OUTOFMEMORY;
365 *ppTypeLib = typelib;
369 /***********************************************************************
370 * AtlWinModuleInit [atl100.65]
372 HRESULT WINAPI AtlWinModuleInit(_ATL_WIN_MODULE *winmod)
374 TRACE("(%p\n", winmod);
376 if(winmod->cbSize != sizeof(*winmod))
379 InitializeCriticalSection(&winmod->m_csWindowCreate);
380 winmod->m_pCreateWndList = NULL;
384 /***********************************************************************
385 * AtlWinModuleAddCreateWndData [atl100.43]
387 void WINAPI AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE *pM, _AtlCreateWndData *pData, void *pvObject)
389 TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
391 pData->m_pThis = pvObject;
392 pData->m_dwThreadID = GetCurrentThreadId();
394 EnterCriticalSection(&pM->m_csWindowCreate);
395 pData->m_pNext = pM->m_pCreateWndList;
396 pM->m_pCreateWndList = pData;
397 LeaveCriticalSection(&pM->m_csWindowCreate);
400 /***********************************************************************
401 * AtlWinModuleExtractCreateWndData [atl100.44]
403 void* WINAPI AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE *winmod)
405 _AtlCreateWndData *iter, *prev = NULL;
408 TRACE("(%p)\n", winmod);
410 thread_id = GetCurrentThreadId();
412 EnterCriticalSection(&winmod->m_csWindowCreate);
414 for(iter = winmod->m_pCreateWndList; iter && iter->m_dwThreadID != thread_id; iter = iter->m_pNext)
418 prev->m_pNext = iter->m_pNext;
420 winmod->m_pCreateWndList = iter->m_pNext;
423 LeaveCriticalSection(&winmod->m_csWindowCreate);
425 return iter ? iter->m_pThis : NULL;
428 /***********************************************************************
429 * AtlComModuleGetClassObject [atl100.15]
431 HRESULT WINAPI AtlComModuleGetClassObject(_ATL_COM_MODULE *pm, REFCLSID rclsid, REFIID riid, void **ppv)
433 _ATL_OBJMAP_ENTRY **iter;
436 TRACE("(%p %s %s %p)\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
441 for(iter = pm->m_ppAutoObjMapFirst; iter < pm->m_ppAutoObjMapLast; iter++) {
442 if(IsEqualCLSID((*iter)->pclsid, rclsid) && (*iter)->pfnGetClassObject) {
444 hres = (*iter)->pfnGetClassObject((*iter)->pfnCreateInstance, &IID_IUnknown, (void**)&(*iter)->pCF);
446 hres = IUnknown_QueryInterface((*iter)->pCF, riid, ppv);
447 TRACE("returning %p (%08x)\n", *ppv, hres);
452 WARN("Class %s not found\n", debugstr_guid(rclsid));
453 return CLASS_E_CLASSNOTAVAILABLE;
456 /***********************************************************************
457 * AtlComModuleUnregisterServer [atl100.22]
459 HRESULT WINAPI AtlComModuleUnregisterServer(_ATL_COM_MODULE *mod, BOOL bRegTypeLib, const CLSID *clsid)
461 const struct _ATL_CATMAP_ENTRY *catmap;
462 _ATL_OBJMAP_ENTRY **iter;
465 TRACE("(%p %x %s)\n", mod, bRegTypeLib, debugstr_guid(clsid));
467 for(iter = mod->m_ppAutoObjMapFirst; iter < mod->m_ppAutoObjMapLast; iter++) {
468 if(!*iter || (clsid && !IsEqualCLSID((*iter)->pclsid, clsid)))
471 TRACE("Unregistering clsid %s\n", debugstr_guid((*iter)->pclsid));
473 catmap = (*iter)->pfnGetCategoryMap();
475 hres = AtlRegisterClassCategoriesHelper((*iter)->pclsid, catmap, FALSE);
480 hres = (*iter)->pfnUpdateRegistry(FALSE);
490 hres = AtlLoadTypeLib(mod->m_hInstTypeLib, NULL, &path, &typelib);
495 hres = ITypeLib_GetLibAttr(typelib, &attr);
496 if(SUCCEEDED(hres)) {
497 hres = UnRegisterTypeLib(&attr->guid, attr->wMajorVerNum, attr->wMinorVerNum, attr->lcid, attr->syskind);
498 ITypeLib_ReleaseTLibAttr(typelib, attr);
500 ITypeLib_Release(typelib);
508 /***********************************************************************
509 * AtlRegisterClassCategoriesHelper [atl100.49]
511 HRESULT WINAPI AtlRegisterClassCategoriesHelper(REFCLSID clsid, const struct _ATL_CATMAP_ENTRY *catmap, BOOL reg)
513 const struct _ATL_CATMAP_ENTRY *iter;
516 TRACE("(%s %p %x)\n", debugstr_guid(clsid), catmap, reg);
522 ICatRegister *new_catreg;
524 hres = CoCreateInstance(&CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER,
525 &IID_ICatRegister, (void**)&new_catreg);
529 if(InterlockedCompareExchangePointer((void**)&catreg, new_catreg, NULL))
530 ICatRegister_Release(new_catreg);
533 for(iter = catmap; iter->iType != _ATL_CATMAP_ENTRY_END; iter++) {
534 CATID catid = *iter->pcatid; /* For stupid lack of const in ICatRegister declaration. */
536 if(iter->iType == _ATL_CATMAP_ENTRY_IMPLEMENTED) {
538 hres = ICatRegister_RegisterClassImplCategories(catreg, clsid, 1, &catid);
540 hres = ICatRegister_UnRegisterClassImplCategories(catreg, clsid, 1, &catid);
543 hres = ICatRegister_RegisterClassReqCategories(catreg, clsid, 1, &catid);
545 hres = ICatRegister_UnRegisterClassReqCategories(catreg, clsid, 1, &catid);
552 WCHAR reg_path[256] = {'C','L','S','I','D','\\'}, *ptr = reg_path+6;
554 static const WCHAR implemented_catW[] =
555 {'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
556 static const WCHAR required_catW[] =
557 {'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
559 ptr += StringFromGUID2(clsid, ptr, 64)-1;
562 memcpy(ptr, implemented_catW, sizeof(implemented_catW));
563 RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
565 memcpy(ptr, required_catW, sizeof(required_catW));
566 RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
572 /***********************************************************************
573 * AtlWaitWithMessageLoop [atl100.24]
575 BOOL WINAPI AtlWaitWithMessageLoop(HANDLE handle)
580 TRACE("(%p)\n", handle);
583 res = MsgWaitForMultipleObjects(1, &handle, FALSE, INFINITE, QS_ALLINPUT);
587 case WAIT_OBJECT_0+1:
588 if(GetMessageW(&msg, NULL, 0, 0) < 0)
591 TranslateMessage(&msg);
592 DispatchMessageW(&msg);
600 static HRESULT get_default_source(ITypeLib *typelib, const CLSID *clsid, IID *iid)
602 ITypeInfo *typeinfo, *src_typeinfo = NULL;
608 hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, &typeinfo);
612 hres = ITypeInfo_GetTypeAttr(typeinfo, &attr);
614 ITypeInfo_Release(typeinfo);
618 for(i=0; i < attr->cImplTypes; i++) {
619 hres = ITypeInfo_GetImplTypeFlags(typeinfo, i, &type_flags);
620 if(SUCCEEDED(hres) && type_flags == (IMPLTYPEFLAG_FSOURCE|IMPLTYPEFLAG_FDEFAULT)) {
623 hres = ITypeInfo_GetRefTypeOfImplType(typeinfo, i, &ref);
625 hres = ITypeInfo_GetRefTypeInfo(typeinfo, ref, &src_typeinfo);
630 ITypeInfo_ReleaseTypeAttr(typeinfo, attr);
631 ITypeInfo_Release(typeinfo);
640 hres = ITypeInfo_GetTypeAttr(src_typeinfo, &attr);
641 if(SUCCEEDED(hres)) {
643 ITypeInfo_ReleaseTypeAttr(src_typeinfo, attr);
645 ITypeInfo_Release(src_typeinfo);
649 /***********************************************************************
650 * AtlGetObjectSourceInterface [atl100.54]
652 HRESULT WINAPI AtlGetObjectSourceInterface(IUnknown *unk, GUID *libid, IID *iid, unsigned short *major, unsigned short *minor)
654 IProvideClassInfo2 *classinfo;
661 TRACE("(%p %p %p %p %p)\n", unk, libid, iid, major, minor);
663 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
667 hres = IDispatch_GetTypeInfo(disp, 0, 0, &typeinfo);
668 IDispatch_Release(disp);
672 hres = ITypeInfo_GetContainingTypeLib(typeinfo, &typelib, 0);
673 ITypeInfo_Release(typeinfo);
674 if(SUCCEEDED(hres)) {
677 hres = ITypeLib_GetLibAttr(typelib, &attr);
678 if(SUCCEEDED(hres)) {
680 *major = attr->wMajorVerNum;
681 *minor = attr->wMinorVerNum;
682 ITypeLib_ReleaseTLibAttr(typelib, attr);
684 ITypeLib_Release(typelib);
690 hres = IUnknown_QueryInterface(unk, &IID_IProvideClassInfo2, (void**)&classinfo);
691 if(SUCCEEDED(hres)) {
692 hres = IProvideClassInfo2_GetGUID(classinfo, GUIDKIND_DEFAULT_SOURCE_DISP_IID, iid);
693 IProvideClassInfo2_Release(classinfo);
694 ITypeLib_Release(typelib);
698 hres = IUnknown_QueryInterface(unk, &IID_IPersist, (void**)&persist);
699 if(SUCCEEDED(hres)) {
702 hres = IPersist_GetClassID(persist, &clsid);
704 hres = get_default_source(typelib, &clsid, iid);
705 IPersist_Release(persist);
711 /***********************************************************************
712 * AtlGetVersion [atl100.@]
714 DWORD WINAPI AtlGetVersion(void *pReserved)
719 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
721 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
724 case DLL_PROCESS_ATTACH:
725 DisableThreadLibraryCalls(hinstDLL);
727 case DLL_PROCESS_DETACH:
729 ICatRegister_Release(catreg);