msvcp90: Fix comment typo.
[wine/multimedia.git] / dlls / atl / atl.c
blob795ca9b1d65a878170d96e02305adc6fe10e1584
1 /*
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
20 #define COBJMACROS
22 #include "atlbase.h"
23 #include "atlcom.h"
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(atl);
30 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
32 HINSTANCE atl_instance;
34 typedef unsigned char cpp_bool;
36 static inline void *heap_alloc(size_t len)
38 return HeapAlloc(GetProcessHeap(), 0, len);
41 static inline BOOL heap_free(void *mem)
43 return HeapFree(GetProcessHeap(), 0, mem);
46 static ICatRegister *catreg;
48 /***********************************************************************
49 * AtlAdvise [atl100.@]
51 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, DWORD *pdw)
53 IConnectionPointContainer *container;
54 IConnectionPoint *cp;
55 HRESULT hres;
57 TRACE("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
59 if(!pUnkCP)
60 return E_INVALIDARG;
62 hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
63 if(FAILED(hres))
64 return hres;
66 hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
67 IConnectionPointContainer_Release(container);
68 if(FAILED(hres))
69 return hres;
71 hres = IConnectionPoint_Advise(cp, pUnk, pdw);
72 IConnectionPoint_Release(cp);
73 return hres;
76 /***********************************************************************
77 * AtlUnadvise [atl100.@]
79 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
81 IConnectionPointContainer *container;
82 IConnectionPoint *cp;
83 HRESULT hres;
85 TRACE("%p %p %d\n", pUnkCP, iid, dw);
87 if(!pUnkCP)
88 return E_INVALIDARG;
90 hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
91 if(FAILED(hres))
92 return hres;
94 hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
95 IConnectionPointContainer_Release(container);
96 if(FAILED(hres))
97 return hres;
99 hres = IConnectionPoint_Unadvise(cp, dw);
100 IConnectionPoint_Release(cp);
101 return hres;
104 /***********************************************************************
105 * AtlFreeMarshalStream [atl100.@]
107 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
109 FIXME("%p\n", stm);
110 return S_OK;
113 /***********************************************************************
114 * AtlMarshalPtrInProc [atl100.@]
116 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
118 FIXME("%p %p %p\n", pUnk, iid, pstm);
119 return E_FAIL;
122 /***********************************************************************
123 * AtlUnmarshalPtr [atl100.@]
125 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
127 FIXME("%p %p %p\n", stm, iid, ppUnk);
128 return E_FAIL;
131 /***********************************************************************
132 * AtlCreateTargetDC [atl100.@]
134 HDC WINAPI AtlCreateTargetDC( HDC hdc, DVTARGETDEVICE *dv )
136 static const WCHAR displayW[] = {'d','i','s','p','l','a','y',0};
137 const WCHAR *driver = NULL, *device = NULL, *port = NULL;
138 DEVMODEW *devmode = NULL;
140 TRACE( "(%p, %p)\n", hdc, dv );
142 if (dv)
144 if (dv->tdDriverNameOffset) driver = (WCHAR *)((char *)dv + dv->tdDriverNameOffset);
145 if (dv->tdDeviceNameOffset) device = (WCHAR *)((char *)dv + dv->tdDeviceNameOffset);
146 if (dv->tdPortNameOffset) port = (WCHAR *)((char *)dv + dv->tdPortNameOffset);
147 if (dv->tdExtDevmodeOffset) devmode = (DEVMODEW *)((char *)dv + dv->tdExtDevmodeOffset);
149 else
151 if (hdc) return hdc;
152 driver = displayW;
154 return CreateDCW( driver, device, port, devmode );
157 /***********************************************************************
158 * AtlHiMetricToPixel [atl100.@]
160 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
162 HDC dc = GetDC(NULL);
163 lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
164 lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
165 ReleaseDC( NULL, dc );
168 /***********************************************************************
169 * AtlPixelToHiMetric [atl100.@]
171 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
173 HDC dc = GetDC(NULL);
174 lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
175 lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
176 ReleaseDC( NULL, dc );
179 /***********************************************************************
180 * AtlComPtrAssign [atl100.@]
182 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
184 TRACE("(%p %p)\n", pp, p);
186 if (p) IUnknown_AddRef(p);
187 if (*pp) IUnknown_Release(*pp);
188 *pp = p;
189 return p;
192 /***********************************************************************
193 * AtlComQIPtrAssign [atl100.@]
195 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
197 IUnknown *new_p = NULL;
199 TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
201 if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
202 if (*pp) IUnknown_Release(*pp);
203 *pp = new_p;
204 return new_p;
207 /***********************************************************************
208 * AtlInternalQueryInterface [atl100.@]
210 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
212 int i = 0;
213 HRESULT rc = E_NOINTERFACE;
214 TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
216 if (IsEqualGUID(iid,&IID_IUnknown))
218 TRACE("Returning IUnknown\n");
219 *ppvObject = ((LPSTR)this+pEntries[0].dw);
220 IUnknown_AddRef((IUnknown*)*ppvObject);
221 return S_OK;
224 while (pEntries[i].pFunc != 0)
226 TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
227 pEntries[i].dw, pEntries[i].pFunc);
229 if (!pEntries[i].piid || IsEqualGUID(iid,pEntries[i].piid))
231 TRACE("MATCH\n");
232 if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
234 TRACE("Offset\n");
235 *ppvObject = ((LPSTR)this+pEntries[i].dw);
236 IUnknown_AddRef((IUnknown*)*ppvObject);
237 return S_OK;
239 else
241 TRACE("Function\n");
242 rc = pEntries[i].pFunc(this, iid, ppvObject, pEntries[i].dw);
243 if(rc==S_OK || pEntries[i].piid)
244 return rc;
247 i++;
249 TRACE("Done returning (0x%x)\n",rc);
250 return rc;
253 /***********************************************************************
254 * AtlIPersistStreamInit_Load [atl100.@]
256 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
257 void *pThis, IUnknown *pUnk)
259 FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
261 return S_OK;
264 /***********************************************************************
265 * AtlIPersistStreamInit_Save [atl100.@]
267 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
268 ATL_PROPMAP_ENTRY *pMap, void *pThis,
269 IUnknown *pUnk)
271 FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
273 return S_OK;
276 /***********************************************************************
277 * AtlIPersistPropertyBag_Load [atl100.@]
279 HRESULT WINAPI AtlIPersistPropertyBag_Load(LPPROPERTYBAG pPropBag, LPERRORLOG pErrorLog,
280 ATL_PROPMAP_ENTRY *pMap, void *pThis,
281 IUnknown *pUnk)
283 FIXME("(%p, %p, %p, %p, %p)\n", pPropBag, pErrorLog, pMap, pThis, pUnk);
285 return S_OK;
288 /***********************************************************************
289 * AtlModuleAddTermFunc [atl100.@]
291 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULE *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
293 _ATL_TERMFUNC_ELEM *termfunc_elem;
295 TRACE("version %04x (%p %p %ld)\n", _ATL_VER, pM, pFunc, dw);
297 if (_ATL_VER > _ATL_VER_30 || pM->cbSize > ATLVer1Size) {
298 termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
299 termfunc_elem->pFunc = pFunc;
300 termfunc_elem->dw = dw;
301 termfunc_elem->pNext = pM->m_pTermFuncs;
303 pM->m_pTermFuncs = termfunc_elem;
306 return S_OK;
309 #if _ATL_VER > _ATL_VER_30
311 /***********************************************************************
312 * AtlCallTermFunc [atl100.@]
314 void WINAPI AtlCallTermFunc(_ATL_MODULE *pM)
316 _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
318 TRACE("(%p)\n", pM);
320 while(iter) {
321 iter->pFunc(iter->dw);
322 tmp = iter;
323 iter = iter->pNext;
324 HeapFree(GetProcessHeap(), 0, tmp);
327 pM->m_pTermFuncs = NULL;
330 #endif
332 /***********************************************************************
333 * AtlLoadTypeLib [atl100.56]
335 HRESULT WINAPI AtlLoadTypeLib(HINSTANCE inst, LPCOLESTR lpszIndex,
336 BSTR *pbstrPath, ITypeLib **ppTypeLib)
338 size_t path_len, index_len;
339 ITypeLib *typelib = NULL;
340 WCHAR *path;
341 HRESULT hres;
343 static const WCHAR tlb_extW[] = {'.','t','l','b',0};
345 TRACE("(%p %s %p %p)\n", inst, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
347 index_len = lpszIndex ? strlenW(lpszIndex) : 0;
348 path = heap_alloc((MAX_PATH+index_len)*sizeof(WCHAR) + sizeof(tlb_extW));
349 if(!path)
350 return E_OUTOFMEMORY;
352 path_len = GetModuleFileNameW(inst, path, MAX_PATH);
353 if(!path_len) {
354 heap_free(path);
355 return HRESULT_FROM_WIN32(GetLastError());
358 if(index_len)
359 memcpy(path+path_len, lpszIndex, (index_len+1)*sizeof(WCHAR));
361 hres = LoadTypeLib(path, &typelib);
362 if(FAILED(hres)) {
363 WCHAR *ptr;
365 for(ptr = path+path_len-1; ptr > path && *ptr != '\\' && *ptr != '.'; ptr--);
366 if(*ptr != '.')
367 ptr = path+path_len;
368 memcpy(ptr, tlb_extW, sizeof(tlb_extW));
369 hres = LoadTypeLib(path, &typelib);
372 if(SUCCEEDED(hres)) {
373 *pbstrPath = SysAllocString(path);
374 if(!*pbstrPath) {
375 ITypeLib_Release(typelib);
376 hres = E_OUTOFMEMORY;
380 heap_free(path);
381 if(FAILED(hres))
382 return hres;
384 *ppTypeLib = typelib;
385 return S_OK;
388 #if _ATL_VER <= _ATL_VER_80
390 /***********************************************************************
391 * AtlRegisterTypeLib [atl80.19]
393 HRESULT WINAPI AtlRegisterTypeLib(HINSTANCE inst, const WCHAR *index)
395 ITypeLib *typelib;
396 BSTR path;
397 HRESULT hres;
399 TRACE("(%p %s)\n", inst, debugstr_w(index));
401 hres = AtlLoadTypeLib(inst, index, &path, &typelib);
402 if(FAILED(hres))
403 return hres;
405 hres = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
406 ITypeLib_Release(typelib);
407 SysFreeString(path);
408 return hres;
411 #endif
413 #if _ATL_VER > _ATL_VER_30
415 /***********************************************************************
416 * AtlWinModuleInit [atl100.65]
418 HRESULT WINAPI AtlWinModuleInit(_ATL_WIN_MODULE *winmod)
420 TRACE("(%p)\n", winmod);
422 if(winmod->cbSize != sizeof(*winmod))
423 return E_INVALIDARG;
425 InitializeCriticalSection(&winmod->m_csWindowCreate);
426 winmod->m_pCreateWndList = NULL;
427 return S_OK;
430 /***********************************************************************
431 * AtlWinModuleAddCreateWndData [atl100.43]
433 void WINAPI AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE *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 * AtlWinModuleExtractCreateWndData [atl100.44]
449 void* WINAPI AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE *winmod)
451 _AtlCreateWndData *iter, *prev = NULL;
452 DWORD thread_id;
454 TRACE("(%p)\n", winmod);
456 thread_id = GetCurrentThreadId();
458 EnterCriticalSection(&winmod->m_csWindowCreate);
460 for(iter = winmod->m_pCreateWndList; iter && iter->m_dwThreadID != thread_id; iter = iter->m_pNext)
461 prev = iter;
462 if(iter) {
463 if(prev)
464 prev->m_pNext = iter->m_pNext;
465 else
466 winmod->m_pCreateWndList = iter->m_pNext;
469 LeaveCriticalSection(&winmod->m_csWindowCreate);
471 return iter ? iter->m_pThis : NULL;
474 /***********************************************************************
475 * AtlComModuleGetClassObject [atl100.15]
477 HRESULT WINAPI AtlComModuleGetClassObject(_ATL_COM_MODULE *pm, REFCLSID rclsid, REFIID riid, void **ppv)
479 _ATL_OBJMAP_ENTRY **iter;
480 HRESULT hres;
482 TRACE("(%p %s %s %p)\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
484 if(!pm)
485 return E_INVALIDARG;
487 for(iter = pm->m_ppAutoObjMapFirst; iter < pm->m_ppAutoObjMapLast; iter++) {
488 if(IsEqualCLSID((*iter)->pclsid, rclsid) && (*iter)->pfnGetClassObject) {
489 if(!(*iter)->pCF)
490 hres = (*iter)->pfnGetClassObject((*iter)->pfnCreateInstance, &IID_IUnknown, (void**)&(*iter)->pCF);
491 if((*iter)->pCF)
492 hres = IUnknown_QueryInterface((*iter)->pCF, riid, ppv);
493 TRACE("returning %p (%08x)\n", *ppv, hres);
494 return hres;
498 WARN("Class %s not found\n", debugstr_guid(rclsid));
499 return CLASS_E_CLASSNOTAVAILABLE;
502 /***********************************************************************
503 * AtlComModuleRegisterClassObjects [atl100.17]
505 HRESULT WINAPI AtlComModuleRegisterClassObjects(_ATL_COM_MODULE *module, DWORD context, DWORD flags)
507 _ATL_OBJMAP_ENTRY **iter;
508 IUnknown *unk;
509 HRESULT hres;
511 TRACE("(%p %x %x)\n", module, context, flags);
513 if(!module)
514 return E_INVALIDARG;
516 for(iter = module->m_ppAutoObjMapFirst; iter < module->m_ppAutoObjMapLast; iter++) {
517 if(!(*iter)->pfnGetClassObject)
518 continue;
520 hres = (*iter)->pfnGetClassObject((*iter)->pfnCreateInstance, &IID_IUnknown, (void**)&unk);
521 if(FAILED(hres))
522 return hres;
524 hres = CoRegisterClassObject((*iter)->pclsid, unk, context, flags, &(*iter)->dwRegister);
525 IUnknown_Release(unk);
526 if(FAILED(hres))
527 return hres;
530 return S_OK;
533 /***********************************************************************
534 * AtlComModuleRevokeClassObjects [atl100.20]
536 HRESULT WINAPI AtlComModuleRevokeClassObjects(_ATL_COM_MODULE *module)
538 _ATL_OBJMAP_ENTRY **iter;
539 HRESULT hres;
541 TRACE("(%p)\n", module);
543 if(!module)
544 return E_INVALIDARG;
546 for(iter = module->m_ppAutoObjMapFirst; iter < module->m_ppAutoObjMapLast; iter++) {
547 hres = CoRevokeClassObject((*iter)->dwRegister);
548 if(FAILED(hres))
549 return hres;
552 return S_OK;
555 /***********************************************************************
556 * AtlComModuleUnregisterServer [atl100.22]
558 HRESULT WINAPI AtlComModuleUnregisterServer(_ATL_COM_MODULE *mod, BOOL bRegTypeLib, const CLSID *clsid)
560 const struct _ATL_CATMAP_ENTRY *catmap;
561 _ATL_OBJMAP_ENTRY **iter;
562 HRESULT hres;
564 TRACE("(%p %x %s)\n", mod, bRegTypeLib, debugstr_guid(clsid));
566 for(iter = mod->m_ppAutoObjMapFirst; iter < mod->m_ppAutoObjMapLast; iter++) {
567 if(!*iter || (clsid && !IsEqualCLSID((*iter)->pclsid, clsid)))
568 continue;
570 TRACE("Unregistering clsid %s\n", debugstr_guid((*iter)->pclsid));
572 catmap = (*iter)->pfnGetCategoryMap();
573 if(catmap) {
574 hres = AtlRegisterClassCategoriesHelper((*iter)->pclsid, catmap, FALSE);
575 if(FAILED(hres))
576 return hres;
579 hres = (*iter)->pfnUpdateRegistry(FALSE);
580 if(FAILED(hres))
581 return hres;
584 if(bRegTypeLib) {
585 ITypeLib *typelib;
586 TLIBATTR *attr;
587 BSTR path;
589 hres = AtlLoadTypeLib(mod->m_hInstTypeLib, NULL, &path, &typelib);
590 if(FAILED(hres))
591 return hres;
593 SysFreeString(path);
594 hres = ITypeLib_GetLibAttr(typelib, &attr);
595 if(SUCCEEDED(hres)) {
596 hres = UnRegisterTypeLib(&attr->guid, attr->wMajorVerNum, attr->wMinorVerNum, attr->lcid, attr->syskind);
597 ITypeLib_ReleaseTLibAttr(typelib, attr);
599 ITypeLib_Release(typelib);
600 if(FAILED(hres))
601 return hres;
604 return S_OK;
607 #endif
609 /***********************************************************************
610 * AtlRegisterClassCategoriesHelper [atl100.49]
612 HRESULT WINAPI AtlRegisterClassCategoriesHelper(REFCLSID clsid, const struct _ATL_CATMAP_ENTRY *catmap, BOOL reg)
614 const struct _ATL_CATMAP_ENTRY *iter;
615 HRESULT hres;
617 TRACE("(%s %p %x)\n", debugstr_guid(clsid), catmap, reg);
619 if(!catmap)
620 return S_OK;
622 if(!catreg) {
623 ICatRegister *new_catreg;
625 hres = CoCreateInstance(&CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER,
626 &IID_ICatRegister, (void**)&new_catreg);
627 if(FAILED(hres))
628 return hres;
630 if(InterlockedCompareExchangePointer((void**)&catreg, new_catreg, NULL))
631 ICatRegister_Release(new_catreg);
634 for(iter = catmap; iter->iType != _ATL_CATMAP_ENTRY_END; iter++) {
635 CATID catid = *iter->pcatid; /* For stupid lack of const in ICatRegister declaration. */
637 if(iter->iType == _ATL_CATMAP_ENTRY_IMPLEMENTED) {
638 if(reg)
639 hres = ICatRegister_RegisterClassImplCategories(catreg, clsid, 1, &catid);
640 else
641 hres = ICatRegister_UnRegisterClassImplCategories(catreg, clsid, 1, &catid);
642 }else {
643 if(reg)
644 hres = ICatRegister_RegisterClassReqCategories(catreg, clsid, 1, &catid);
645 else
646 hres = ICatRegister_UnRegisterClassReqCategories(catreg, clsid, 1, &catid);
648 if(FAILED(hres))
649 return hres;
652 if(!reg) {
653 WCHAR reg_path[256] = {'C','L','S','I','D','\\'}, *ptr = reg_path+6;
655 static const WCHAR implemented_catW[] =
656 {'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
657 static const WCHAR required_catW[] =
658 {'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
660 ptr += StringFromGUID2(clsid, ptr, 64)-1;
661 *ptr++ = '\\';
663 memcpy(ptr, implemented_catW, sizeof(implemented_catW));
664 RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
666 memcpy(ptr, required_catW, sizeof(required_catW));
667 RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
670 return S_OK;
673 /***********************************************************************
674 * AtlWaitWithMessageLoop [atl100.24]
676 BOOL WINAPI AtlWaitWithMessageLoop(HANDLE handle)
678 MSG msg;
679 DWORD res;
681 TRACE("(%p)\n", handle);
683 while(1) {
684 res = MsgWaitForMultipleObjects(1, &handle, FALSE, INFINITE, QS_ALLINPUT);
685 switch(res) {
686 case WAIT_OBJECT_0:
687 return TRUE;
688 case WAIT_OBJECT_0+1:
689 if(GetMessageW(&msg, NULL, 0, 0) < 0)
690 return FALSE;
692 TranslateMessage(&msg);
693 DispatchMessageW(&msg);
694 break;
695 default:
696 return FALSE;
701 static HRESULT get_default_source(ITypeLib *typelib, const CLSID *clsid, IID *iid)
703 ITypeInfo *typeinfo, *src_typeinfo = NULL;
704 TYPEATTR *attr;
705 int type_flags;
706 unsigned i;
707 HRESULT hres;
709 hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, &typeinfo);
710 if(FAILED(hres))
711 return hres;
713 hres = ITypeInfo_GetTypeAttr(typeinfo, &attr);
714 if(FAILED(hres)) {
715 ITypeInfo_Release(typeinfo);
716 return hres;
719 for(i=0; i < attr->cImplTypes; i++) {
720 hres = ITypeInfo_GetImplTypeFlags(typeinfo, i, &type_flags);
721 if(SUCCEEDED(hres) && type_flags == (IMPLTYPEFLAG_FSOURCE|IMPLTYPEFLAG_FDEFAULT)) {
722 HREFTYPE ref;
724 hres = ITypeInfo_GetRefTypeOfImplType(typeinfo, i, &ref);
725 if(SUCCEEDED(hres))
726 hres = ITypeInfo_GetRefTypeInfo(typeinfo, ref, &src_typeinfo);
727 break;
731 ITypeInfo_ReleaseTypeAttr(typeinfo, attr);
732 ITypeInfo_Release(typeinfo);
733 if(FAILED(hres))
734 return hres;
736 if(!src_typeinfo) {
737 *iid = IID_NULL;
738 return S_OK;
741 hres = ITypeInfo_GetTypeAttr(src_typeinfo, &attr);
742 if(SUCCEEDED(hres)) {
743 *iid = attr->guid;
744 ITypeInfo_ReleaseTypeAttr(src_typeinfo, attr);
746 ITypeInfo_Release(src_typeinfo);
747 return hres;
750 /***********************************************************************
751 * AtlGetObjectSourceInterface [atl100.54]
753 HRESULT WINAPI AtlGetObjectSourceInterface(IUnknown *unk, GUID *libid, IID *iid, unsigned short *major, unsigned short *minor)
755 IProvideClassInfo2 *classinfo;
756 ITypeInfo *typeinfo;
757 ITypeLib *typelib;
758 IPersist *persist;
759 IDispatch *disp;
760 HRESULT hres;
762 TRACE("(%p %p %p %p %p)\n", unk, libid, iid, major, minor);
764 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
765 if(FAILED(hres))
766 return hres;
768 hres = IDispatch_GetTypeInfo(disp, 0, 0, &typeinfo);
769 IDispatch_Release(disp);
770 if(FAILED(hres))
771 return hres;
773 hres = ITypeInfo_GetContainingTypeLib(typeinfo, &typelib, 0);
774 ITypeInfo_Release(typeinfo);
775 if(SUCCEEDED(hres)) {
776 TLIBATTR *attr;
778 hres = ITypeLib_GetLibAttr(typelib, &attr);
779 if(SUCCEEDED(hres)) {
780 *libid = attr->guid;
781 *major = attr->wMajorVerNum;
782 *minor = attr->wMinorVerNum;
783 ITypeLib_ReleaseTLibAttr(typelib, attr);
784 }else {
785 ITypeLib_Release(typelib);
788 if(FAILED(hres))
789 return hres;
791 hres = IUnknown_QueryInterface(unk, &IID_IProvideClassInfo2, (void**)&classinfo);
792 if(SUCCEEDED(hres)) {
793 hres = IProvideClassInfo2_GetGUID(classinfo, GUIDKIND_DEFAULT_SOURCE_DISP_IID, iid);
794 IProvideClassInfo2_Release(classinfo);
795 ITypeLib_Release(typelib);
796 return hres;
799 hres = IUnknown_QueryInterface(unk, &IID_IPersist, (void**)&persist);
800 if(SUCCEEDED(hres)) {
801 CLSID clsid;
803 hres = IPersist_GetClassID(persist, &clsid);
804 if(SUCCEEDED(hres))
805 hres = get_default_source(typelib, &clsid, iid);
806 IPersist_Release(persist);
809 return hres;
812 #if _ATL_VER >= _ATL_VER90
814 /***********************************************************************
815 * AtlSetPerUserRegistration [atl100.67]
817 HRESULT WINAPI AtlSetPerUserRegistration(cpp_bool bEnable)
819 FIXME("stub: bEnable: %d\n", bEnable);
820 return E_NOTIMPL;
823 /***********************************************************************
824 * AtlGetPerUserRegistration [atl100.68]
826 HRESULT WINAPI AtlGetPerUserRegistration(cpp_bool *pbEnabled)
828 FIXME("stub: returning false\n");
829 *pbEnabled = 0;
830 return S_OK;
833 #endif
835 /***********************************************************************
836 * AtlGetVersion [atl100.@]
838 DWORD WINAPI AtlGetVersion(void *pReserved)
840 TRACE("version %04x (%p)\n", _ATL_VER, pReserved);
841 return _ATL_VER;
844 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
846 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
848 switch(fdwReason) {
849 case DLL_PROCESS_ATTACH:
850 atl_instance = hinstDLL;
851 DisableThreadLibraryCalls(hinstDLL);
852 break;
853 case DLL_PROCESS_DETACH:
854 if (lpvReserved) break;
855 if(catreg)
856 ICatRegister_Release(catreg);
859 return TRUE;