xref: /reactos/dll/win32/atl/atl.c (revision 84ccccab)
1 /*
2  * Copyright 2012 Stefan Leichter
3  * Copyright 2012 Jacek Caban for CodeWeavers
4  *
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.
9  *
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.
14  *
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
18  */
19 
20 #include <precomp.h>
21 
22 #include <wine/atlcom.h>
23 #include <wingdi.h>
24 
25 #define ATLVer1Size FIELD_OFFSET(_ATL_MODULEW, dwAtlBuildVer)
26 
27 HINSTANCE atl_instance;
28 
29 typedef unsigned char cpp_bool;
30 
31 static inline void* __WINE_ALLOC_SIZE(1) heap_alloc(size_t size)
32 {
33     return HeapAlloc(GetProcessHeap(), 0, size);
34 }
35 
36 static inline BOOL heap_free(void *mem)
37 {
38     return HeapFree(GetProcessHeap(), 0, mem);
39 }
40 
41 static ICatRegister *catreg;
42 
43 /***********************************************************************
44  *           AtlAdvise         [atl100.@]
45  */
46 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, DWORD *pdw)
47 {
48     IConnectionPointContainer *container;
49     IConnectionPoint *cp;
50     HRESULT hres;
51 
52     TRACE("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
53 
54     if(!pUnkCP)
55         return E_INVALIDARG;
56 
57     hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
58     if(FAILED(hres))
59         return hres;
60 
61     hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
62     IConnectionPointContainer_Release(container);
63     if(FAILED(hres))
64         return hres;
65 
66     hres = IConnectionPoint_Advise(cp, pUnk, pdw);
67     IConnectionPoint_Release(cp);
68     return hres;
69 }
70 
71 /***********************************************************************
72  *           AtlUnadvise         [atl100.@]
73  */
74 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
75 {
76     IConnectionPointContainer *container;
77     IConnectionPoint *cp;
78     HRESULT hres;
79 
80     TRACE("%p %p %d\n", pUnkCP, iid, dw);
81 
82     if(!pUnkCP)
83         return E_INVALIDARG;
84 
85     hres = IUnknown_QueryInterface(pUnkCP, &IID_IConnectionPointContainer, (void**)&container);
86     if(FAILED(hres))
87         return hres;
88 
89     hres = IConnectionPointContainer_FindConnectionPoint(container, iid, &cp);
90     IConnectionPointContainer_Release(container);
91     if(FAILED(hres))
92         return hres;
93 
94     hres = IConnectionPoint_Unadvise(cp, dw);
95     IConnectionPoint_Release(cp);
96     return hres;
97 }
98 
99 /***********************************************************************
100  *           AtlFreeMarshalStream         [atl100.@]
101  */
102 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
103 {
104     FIXME("%p\n", stm);
105     return S_OK;
106 }
107 
108 /***********************************************************************
109  *           AtlMarshalPtrInProc         [atl100.@]
110  */
111 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
112 {
113     FIXME("%p %p %p\n", pUnk, iid, pstm);
114     return E_FAIL;
115 }
116 
117 /***********************************************************************
118  *           AtlUnmarshalPtr              [atl100.@]
119  */
120 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
121 {
122     FIXME("%p %p %p\n", stm, iid, ppUnk);
123     return E_FAIL;
124 }
125 
126 /***********************************************************************
127  *           AtlCreateTargetDC         [atl100.@]
128  */
129 HDC WINAPI AtlCreateTargetDC( HDC hdc, DVTARGETDEVICE *dv )
130 {
131     static const WCHAR displayW[] = {'d','i','s','p','l','a','y',0};
132     const WCHAR *driver = NULL, *device = NULL, *port = NULL;
133     DEVMODEW *devmode = NULL;
134 
135     TRACE( "(%p, %p)\n", hdc, dv );
136 
137     if (dv)
138     {
139         if (dv->tdDriverNameOffset) driver  = (WCHAR *)((char *)dv + dv->tdDriverNameOffset);
140         if (dv->tdDeviceNameOffset) device  = (WCHAR *)((char *)dv + dv->tdDeviceNameOffset);
141         if (dv->tdPortNameOffset)   port    = (WCHAR *)((char *)dv + dv->tdPortNameOffset);
142         if (dv->tdExtDevmodeOffset) devmode = (DEVMODEW *)((char *)dv + dv->tdExtDevmodeOffset);
143     }
144     else
145     {
146         if (hdc) return hdc;
147         driver = displayW;
148     }
149     return CreateDCW( driver, device, port, devmode );
150 }
151 
152 /***********************************************************************
153  *           AtlHiMetricToPixel              [atl100.@]
154  */
155 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
156 {
157     HDC dc = GetDC(NULL);
158     lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
159     lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
160     ReleaseDC( NULL, dc );
161 }
162 
163 /***********************************************************************
164  *           AtlPixelToHiMetric              [atl100.@]
165  */
166 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
167 {
168     HDC dc = GetDC(NULL);
169     lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
170     lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
171     ReleaseDC( NULL, dc );
172 }
173 
174 /***********************************************************************
175  *           AtlComPtrAssign              [atl100.@]
176  */
177 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
178 {
179     TRACE("(%p %p)\n", pp, p);
180 
181     if (p) IUnknown_AddRef(p);
182     if (*pp) IUnknown_Release(*pp);
183     *pp = p;
184     return p;
185 }
186 
187 /***********************************************************************
188  *           AtlComQIPtrAssign              [atl100.@]
189  */
190 IUnknown* WINAPI AtlComQIPtrAssign(IUnknown** pp, IUnknown *p, REFIID riid)
191 {
192     IUnknown *new_p = NULL;
193 
194     TRACE("(%p %p %s)\n", pp, p, debugstr_guid(riid));
195 
196     if (p) IUnknown_QueryInterface(p, riid, (void **)&new_p);
197     if (*pp) IUnknown_Release(*pp);
198     *pp = new_p;
199     return new_p;
200 }
201 
202 /***********************************************************************
203  *           AtlInternalQueryInterface     [atl100.@]
204  */
205 HRESULT WINAPI AtlInternalQueryInterface(void* this, const _ATL_INTMAP_ENTRY* pEntries,  REFIID iid, void** ppvObject)
206 {
207     int i = 0;
208     HRESULT rc = E_NOINTERFACE;
209     TRACE("(%p, %p, %s, %p)\n",this, pEntries, debugstr_guid(iid), ppvObject);
210 
211     if (IsEqualGUID(iid,&IID_IUnknown))
212     {
213         TRACE("Returning IUnknown\n");
214         *ppvObject = ((LPSTR)this+pEntries[0].dw);
215         IUnknown_AddRef((IUnknown*)*ppvObject);
216         return S_OK;
217     }
218 
219     while (pEntries[i].pFunc != 0)
220     {
221         TRACE("Trying entry %i (%s %i %p)\n",i,debugstr_guid(pEntries[i].piid),
222               pEntries[i].dw, pEntries[i].pFunc);
223 
224         if (!pEntries[i].piid || IsEqualGUID(iid,pEntries[i].piid))
225         {
226             TRACE("MATCH\n");
227             if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
228             {
229                 TRACE("Offset\n");
230                 *ppvObject = ((LPSTR)this+pEntries[i].dw);
231                 IUnknown_AddRef((IUnknown*)*ppvObject);
232                 return S_OK;
233             }
234             else
235             {
236                 TRACE("Function\n");
237                 rc = pEntries[i].pFunc(this, iid, ppvObject, pEntries[i].dw);
238                 if(rc==S_OK || pEntries[i].piid)
239                     return rc;
240             }
241         }
242         i++;
243     }
244     TRACE("Done returning (0x%x)\n",rc);
245     return rc;
246 }
247 
248 /***********************************************************************
249  *           AtlIPersistStreamInit_Load      [atl100.@]
250  */
251 HRESULT WINAPI AtlIPersistStreamInit_Load( LPSTREAM pStm, ATL_PROPMAP_ENTRY *pMap,
252                                            void *pThis, IUnknown *pUnk)
253 {
254     FIXME("(%p, %p, %p, %p)\n", pStm, pMap, pThis, pUnk);
255 
256     return S_OK;
257 }
258 
259 /***********************************************************************
260  *           AtlIPersistStreamInit_Save      [atl100.@]
261  */
262 HRESULT WINAPI AtlIPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty,
263                                           ATL_PROPMAP_ENTRY *pMap, void *pThis,
264                                           IUnknown *pUnk)
265 {
266     FIXME("(%p, %d, %p, %p, %p)\n", pStm, fClearDirty, pMap, pThis, pUnk);
267 
268     return S_OK;
269 }
270 
271 /***********************************************************************
272  *           AtlIPersistPropertyBag_Load      [atl100.@]
273  */
274 HRESULT WINAPI AtlIPersistPropertyBag_Load(LPPROPERTYBAG pPropBag, LPERRORLOG pErrorLog,
275                                            ATL_PROPMAP_ENTRY *pMap, void *pThis,
276                                            IUnknown *pUnk)
277 {
278     FIXME("(%p, %p, %p, %p, %p)\n", pPropBag, pErrorLog, pMap, pThis, pUnk);
279 
280     return S_OK;
281 }
282 
283 /***********************************************************************
284  *           AtlIPersistPropertyBag_Save     [atl100.@]
285  */
286 HRESULT WINAPI AtlIPersistPropertyBag_Save(LPPROPERTYBAG pPropBag, BOOL fClearDirty,
287                                            BOOL fSaveAll, ATL_PROPMAP_ENTRY *pMap,
288                                            void *pThis, IUnknown *pUnk)
289 {
290     FIXME("(%p, %d, %d, %p, %p, %p)\n", pPropBag, fClearDirty, fSaveAll, pMap, pThis, pUnk);
291 
292     return S_OK;
293 }
294 
295 /***********************************************************************
296  *           AtlModuleAddTermFunc            [atl100.@]
297  */
298 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULE *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
299 {
300     _ATL_TERMFUNC_ELEM *termfunc_elem;
301 
302     TRACE("version %04x (%p %p %ld)\n", _ATL_VER, pM, pFunc, dw);
303 
304     if (_ATL_VER > _ATL_VER_30 || pM->cbSize > ATLVer1Size) {
305         termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
306         termfunc_elem->pFunc = pFunc;
307         termfunc_elem->dw = dw;
308         termfunc_elem->pNext = pM->m_pTermFuncs;
309 
310         pM->m_pTermFuncs = termfunc_elem;
311     }
312 
313     return S_OK;
314 }
315 
316 #if _ATL_VER > _ATL_VER_30
317 
318 /***********************************************************************
319  *           AtlCallTermFunc              [atl100.@]
320  */
321 void WINAPI AtlCallTermFunc(_ATL_MODULE *pM)
322 {
323     _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
324 
325     TRACE("(%p)\n", pM);
326 
327     while(iter) {
328         iter->pFunc(iter->dw);
329         tmp = iter;
330         iter = iter->pNext;
331         HeapFree(GetProcessHeap(), 0, tmp);
332     }
333 
334     pM->m_pTermFuncs = NULL;
335 }
336 
337 #endif
338 
339 /***********************************************************************
340  *           AtlLoadTypeLib             [atl100.56]
341  */
342 HRESULT WINAPI AtlLoadTypeLib(HINSTANCE inst, LPCOLESTR lpszIndex,
343         BSTR *pbstrPath, ITypeLib **ppTypeLib)
344 {
345     size_t path_len, index_len;
346     ITypeLib *typelib = NULL;
347     WCHAR *path;
348     HRESULT hres;
349 
350     static const WCHAR tlb_extW[] = {'.','t','l','b',0};
351 
352     TRACE("(%p %s %p %p)\n", inst, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
353 
354     index_len = lpszIndex ? strlenW(lpszIndex) : 0;
355     path = heap_alloc((MAX_PATH+index_len)*sizeof(WCHAR) + sizeof(tlb_extW));
356     if(!path)
357         return E_OUTOFMEMORY;
358 
359     path_len = GetModuleFileNameW(inst, path, MAX_PATH);
360     if(!path_len) {
361         heap_free(path);
362         return HRESULT_FROM_WIN32(GetLastError());
363     }
364 
365     if(index_len)
366         memcpy(path+path_len, lpszIndex, (index_len+1)*sizeof(WCHAR));
367 
368     hres = LoadTypeLib(path, &typelib);
369     if(FAILED(hres)) {
370         WCHAR *ptr;
371 
372         for(ptr = path+path_len-1; ptr > path && *ptr != '\\' && *ptr != '.'; ptr--);
373         if(*ptr != '.')
374             ptr = path+path_len;
375         memcpy(ptr, tlb_extW, sizeof(tlb_extW));
376         hres = LoadTypeLib(path, &typelib);
377     }
378 
379     if(SUCCEEDED(hres)) {
380         *pbstrPath = SysAllocString(path);
381         if(!*pbstrPath) {
382             ITypeLib_Release(typelib);
383             hres = E_OUTOFMEMORY;
384         }
385     }
386 
387     heap_free(path);
388     if(FAILED(hres))
389         return hres;
390 
391     *ppTypeLib = typelib;
392     return S_OK;
393 }
394 
395 #if _ATL_VER <= _ATL_VER_80
396 
397 /***********************************************************************
398  *           AtlRegisterTypeLib         [atl80.19]
399  */
400 HRESULT WINAPI AtlRegisterTypeLib(HINSTANCE inst, const WCHAR *index)
401 {
402     ITypeLib *typelib;
403     BSTR path;
404     HRESULT hres;
405 
406     TRACE("(%p %s)\n", inst, debugstr_w(index));
407 
408     hres = AtlLoadTypeLib(inst, index, &path, &typelib);
409     if(FAILED(hres))
410         return hres;
411 
412     hres = RegisterTypeLib(typelib, path, NULL); /* FIXME: pass help directory */
413     ITypeLib_Release(typelib);
414     SysFreeString(path);
415     return hres;
416 }
417 
418 #endif
419 
420 #if _ATL_VER > _ATL_VER_30
421 
422 /***********************************************************************
423  *           AtlWinModuleInit                          [atl100.65]
424  */
425 HRESULT WINAPI AtlWinModuleInit(_ATL_WIN_MODULE *winmod)
426 {
427     TRACE("(%p)\n", winmod);
428 
429     if(winmod->cbSize != sizeof(*winmod))
430         return E_INVALIDARG;
431 
432     InitializeCriticalSection(&winmod->m_csWindowCreate);
433     winmod->m_pCreateWndList = NULL;
434     return S_OK;
435 }
436 
437 /***********************************************************************
438  *           AtlWinModuleAddCreateWndData              [atl100.43]
439  */
440 void WINAPI AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE *pM, _AtlCreateWndData *pData, void *pvObject)
441 {
442     TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
443 
444     pData->m_pThis = pvObject;
445     pData->m_dwThreadID = GetCurrentThreadId();
446 
447     EnterCriticalSection(&pM->m_csWindowCreate);
448     pData->m_pNext = pM->m_pCreateWndList;
449     pM->m_pCreateWndList = pData;
450     LeaveCriticalSection(&pM->m_csWindowCreate);
451 }
452 
453 /***********************************************************************
454  *           AtlWinModuleExtractCreateWndData          [atl100.44]
455  */
456 void* WINAPI AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE *winmod)
457 {
458     _AtlCreateWndData *iter, *prev = NULL;
459     DWORD thread_id;
460 
461     TRACE("(%p)\n", winmod);
462 
463     thread_id = GetCurrentThreadId();
464 
465     EnterCriticalSection(&winmod->m_csWindowCreate);
466 
467     for(iter = winmod->m_pCreateWndList; iter && iter->m_dwThreadID != thread_id; iter = iter->m_pNext)
468         prev = iter;
469     if(iter) {
470         if(prev)
471             prev->m_pNext = iter->m_pNext;
472         else
473             winmod->m_pCreateWndList = iter->m_pNext;
474     }
475 
476     LeaveCriticalSection(&winmod->m_csWindowCreate);
477 
478     return iter ? iter->m_pThis : NULL;
479 }
480 
481 /***********************************************************************
482  *           AtlComModuleGetClassObject                [atl100.15]
483  */
484 HRESULT WINAPI AtlComModuleGetClassObject(_ATL_COM_MODULE *pm, REFCLSID rclsid, REFIID riid, void **ppv)
485 {
486     _ATL_OBJMAP_ENTRY **iter;
487     HRESULT hres;
488 
489     TRACE("(%p %s %s %p)\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
490 
491     if(!pm)
492         return E_INVALIDARG;
493 
494     for(iter = pm->m_ppAutoObjMapFirst; iter < pm->m_ppAutoObjMapLast; iter++) {
495         if(IsEqualCLSID((*iter)->pclsid, rclsid) && (*iter)->pfnGetClassObject) {
496             if(!(*iter)->pCF)
497                 hres = (*iter)->pfnGetClassObject((*iter)->pfnCreateInstance, &IID_IUnknown, (void**)&(*iter)->pCF);
498             if((*iter)->pCF)
499                 hres = IUnknown_QueryInterface((*iter)->pCF, riid, ppv);
500             TRACE("returning %p (%08x)\n", *ppv, hres);
501             return hres;
502         }
503     }
504 
505     WARN("Class %s not found\n", debugstr_guid(rclsid));
506     return CLASS_E_CLASSNOTAVAILABLE;
507 }
508 
509 /***********************************************************************
510  *           AtlComModuleRegisterClassObjects   [atl100.17]
511  */
512 HRESULT WINAPI AtlComModuleRegisterClassObjects(_ATL_COM_MODULE *module, DWORD context, DWORD flags)
513 {
514     _ATL_OBJMAP_ENTRY **iter;
515     IUnknown *unk;
516     HRESULT hres;
517 
518     TRACE("(%p %x %x)\n", module, context, flags);
519 
520     if(!module)
521         return E_INVALIDARG;
522 
523     for(iter = module->m_ppAutoObjMapFirst; iter < module->m_ppAutoObjMapLast; iter++) {
524         if(!(*iter)->pfnGetClassObject)
525             continue;
526 
527         hres = (*iter)->pfnGetClassObject((*iter)->pfnCreateInstance, &IID_IUnknown, (void**)&unk);
528         if(FAILED(hres))
529             return hres;
530 
531         hres = CoRegisterClassObject((*iter)->pclsid, unk, context, flags, &(*iter)->dwRegister);
532         IUnknown_Release(unk);
533         if(FAILED(hres))
534             return hres;
535     }
536 
537    return S_OK;
538 }
539 
540 /***********************************************************************
541  *           AtlComModuleRevokeClassObjects   [atl100.20]
542  */
543 HRESULT WINAPI AtlComModuleRevokeClassObjects(_ATL_COM_MODULE *module)
544 {
545     _ATL_OBJMAP_ENTRY **iter;
546     HRESULT hres;
547 
548     TRACE("(%p)\n", module);
549 
550     if(!module)
551         return E_INVALIDARG;
552 
553     for(iter = module->m_ppAutoObjMapFirst; iter < module->m_ppAutoObjMapLast; iter++) {
554         hres = CoRevokeClassObject((*iter)->dwRegister);
555         if(FAILED(hres))
556             return hres;
557     }
558 
559     return S_OK;
560 }
561 
562 /***********************************************************************
563  *           AtlComModuleUnregisterServer       [atl100.22]
564  */
565 HRESULT WINAPI AtlComModuleUnregisterServer(_ATL_COM_MODULE *mod, BOOL bRegTypeLib, const CLSID *clsid)
566 {
567     const struct _ATL_CATMAP_ENTRY *catmap;
568     _ATL_OBJMAP_ENTRY **iter;
569     HRESULT hres;
570 
571     TRACE("(%p %x %s)\n", mod, bRegTypeLib, debugstr_guid(clsid));
572 
573     for(iter = mod->m_ppAutoObjMapFirst; iter < mod->m_ppAutoObjMapLast; iter++) {
574         if(!*iter || (clsid && !IsEqualCLSID((*iter)->pclsid, clsid)))
575             continue;
576 
577         TRACE("Unregistering clsid %s\n", debugstr_guid((*iter)->pclsid));
578 
579         catmap = (*iter)->pfnGetCategoryMap();
580         if(catmap) {
581             hres = AtlRegisterClassCategoriesHelper((*iter)->pclsid, catmap, FALSE);
582             if(FAILED(hres))
583                 return hres;
584         }
585 
586         hres = (*iter)->pfnUpdateRegistry(FALSE);
587         if(FAILED(hres))
588             return hres;
589     }
590 
591     if(bRegTypeLib) {
592         ITypeLib *typelib;
593         TLIBATTR *attr;
594         BSTR path;
595 
596         hres = AtlLoadTypeLib(mod->m_hInstTypeLib, NULL, &path, &typelib);
597         if(FAILED(hres))
598             return hres;
599 
600         SysFreeString(path);
601         hres = ITypeLib_GetLibAttr(typelib, &attr);
602         if(SUCCEEDED(hres)) {
603             hres = UnRegisterTypeLib(&attr->guid, attr->wMajorVerNum, attr->wMinorVerNum, attr->lcid, attr->syskind);
604             ITypeLib_ReleaseTLibAttr(typelib, attr);
605         }
606         ITypeLib_Release(typelib);
607         if(FAILED(hres))
608             return hres;
609     }
610 
611     return S_OK;
612 }
613 
614 #endif
615 
616 /***********************************************************************
617  *           AtlRegisterClassCategoriesHelper          [atl100.49]
618  */
619 HRESULT WINAPI AtlRegisterClassCategoriesHelper(REFCLSID clsid, const struct _ATL_CATMAP_ENTRY *catmap, BOOL reg)
620 {
621     const struct _ATL_CATMAP_ENTRY *iter;
622     HRESULT hres;
623 
624     TRACE("(%s %p %x)\n", debugstr_guid(clsid), catmap, reg);
625 
626     if(!catmap)
627         return S_OK;
628 
629     if(!catreg) {
630         ICatRegister *new_catreg;
631 
632         hres = CoCreateInstance(&CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER,
633                 &IID_ICatRegister, (void**)&new_catreg);
634         if(FAILED(hres))
635             return hres;
636 
637         if(InterlockedCompareExchangePointer((void**)&catreg, new_catreg, NULL))
638             ICatRegister_Release(new_catreg);
639     }
640 
641     for(iter = catmap; iter->iType != _ATL_CATMAP_ENTRY_END; iter++) {
642         CATID catid = *iter->pcatid; /* For stupid lack of const in ICatRegister declaration. */
643 
644         if(iter->iType == _ATL_CATMAP_ENTRY_IMPLEMENTED) {
645             if(reg)
646                 hres = ICatRegister_RegisterClassImplCategories(catreg, clsid, 1, &catid);
647             else
648                 hres = ICatRegister_UnRegisterClassImplCategories(catreg, clsid, 1, &catid);
649         }else {
650             if(reg)
651                 hres = ICatRegister_RegisterClassReqCategories(catreg, clsid, 1, &catid);
652             else
653                 hres = ICatRegister_UnRegisterClassReqCategories(catreg, clsid, 1, &catid);
654         }
655         if(FAILED(hres))
656             return hres;
657     }
658 
659     if(!reg) {
660         WCHAR reg_path[256] = {'C','L','S','I','D','\\'}, *ptr = reg_path+6;
661 
662         static const WCHAR implemented_catW[] =
663             {'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
664         static const WCHAR required_catW[] =
665             {'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0};
666 
667         ptr += StringFromGUID2(clsid, ptr, 64)-1;
668         *ptr++ = '\\';
669 
670         memcpy(ptr, implemented_catW, sizeof(implemented_catW));
671         RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
672 
673         memcpy(ptr, required_catW, sizeof(required_catW));
674         RegDeleteKeyW(HKEY_CLASSES_ROOT, reg_path);
675     }
676 
677     return S_OK;
678 }
679 
680 /***********************************************************************
681  *           AtlWaitWithMessageLoop     [atl100.24]
682  */
683 BOOL WINAPI AtlWaitWithMessageLoop(HANDLE handle)
684 {
685     MSG msg;
686     DWORD res;
687 
688     TRACE("(%p)\n", handle);
689 
690     while(1) {
691         res = MsgWaitForMultipleObjects(1, &handle, FALSE, INFINITE, QS_ALLINPUT);
692         switch(res) {
693         case WAIT_OBJECT_0:
694             return TRUE;
695         case WAIT_OBJECT_0+1:
696             if(GetMessageW(&msg, NULL, 0, 0) < 0)
697                 return FALSE;
698 
699             TranslateMessage(&msg);
700             DispatchMessageW(&msg);
701             break;
702         default:
703             return FALSE;
704         }
705     }
706 }
707 
708 static HRESULT get_default_source(ITypeLib *typelib, const CLSID *clsid, IID *iid)
709 {
710     ITypeInfo *typeinfo, *src_typeinfo = NULL;
711     TYPEATTR *attr;
712     int type_flags;
713     unsigned i;
714     HRESULT hres;
715 
716     hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, &typeinfo);
717     if(FAILED(hres))
718         return hres;
719 
720     hres = ITypeInfo_GetTypeAttr(typeinfo, &attr);
721     if(FAILED(hres)) {
722         ITypeInfo_Release(typeinfo);
723         return hres;
724     }
725 
726     for(i=0; i < attr->cImplTypes; i++) {
727         hres = ITypeInfo_GetImplTypeFlags(typeinfo, i, &type_flags);
728         if(SUCCEEDED(hres) && type_flags == (IMPLTYPEFLAG_FSOURCE|IMPLTYPEFLAG_FDEFAULT)) {
729             HREFTYPE ref;
730 
731             hres = ITypeInfo_GetRefTypeOfImplType(typeinfo, i, &ref);
732             if(SUCCEEDED(hres))
733                 hres = ITypeInfo_GetRefTypeInfo(typeinfo, ref, &src_typeinfo);
734             break;
735         }
736     }
737 
738     ITypeInfo_ReleaseTypeAttr(typeinfo, attr);
739     ITypeInfo_Release(typeinfo);
740     if(FAILED(hres))
741         return hres;
742 
743     if(!src_typeinfo) {
744         *iid = IID_NULL;
745         return S_OK;
746     }
747 
748     hres = ITypeInfo_GetTypeAttr(src_typeinfo, &attr);
749     if(SUCCEEDED(hres)) {
750         *iid = attr->guid;
751         ITypeInfo_ReleaseTypeAttr(src_typeinfo, attr);
752     }
753     ITypeInfo_Release(src_typeinfo);
754     return hres;
755 }
756 
757 /***********************************************************************
758  *           AtlGetObjectSourceInterface    [atl100.54]
759  */
760 HRESULT WINAPI AtlGetObjectSourceInterface(IUnknown *unk, GUID *libid, IID *iid, unsigned short *major, unsigned short *minor)
761 {
762     IProvideClassInfo2 *classinfo;
763     ITypeInfo *typeinfo;
764     ITypeLib *typelib;
765     IPersist *persist;
766     IDispatch *disp;
767     HRESULT hres;
768 
769     TRACE("(%p %p %p %p %p)\n", unk, libid, iid, major, minor);
770 
771     hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
772     if(FAILED(hres))
773         return hres;
774 
775     hres = IDispatch_GetTypeInfo(disp, 0, 0, &typeinfo);
776     IDispatch_Release(disp);
777     if(FAILED(hres))
778         return hres;
779 
780     hres = ITypeInfo_GetContainingTypeLib(typeinfo, &typelib, 0);
781     ITypeInfo_Release(typeinfo);
782     if(SUCCEEDED(hres)) {
783         TLIBATTR *attr;
784 
785         hres = ITypeLib_GetLibAttr(typelib, &attr);
786         if(SUCCEEDED(hres)) {
787             *libid = attr->guid;
788             *major = attr->wMajorVerNum;
789             *minor = attr->wMinorVerNum;
790             ITypeLib_ReleaseTLibAttr(typelib, attr);
791         }else {
792             ITypeLib_Release(typelib);
793         }
794     }
795     if(FAILED(hres))
796         return hres;
797 
798     hres = IUnknown_QueryInterface(unk, &IID_IProvideClassInfo2, (void**)&classinfo);
799     if(SUCCEEDED(hres)) {
800         hres = IProvideClassInfo2_GetGUID(classinfo, GUIDKIND_DEFAULT_SOURCE_DISP_IID, iid);
801         IProvideClassInfo2_Release(classinfo);
802         ITypeLib_Release(typelib);
803         return hres;
804     }
805 
806     hres = IUnknown_QueryInterface(unk, &IID_IPersist, (void**)&persist);
807     if(SUCCEEDED(hres)) {
808         CLSID clsid;
809 
810         hres = IPersist_GetClassID(persist, &clsid);
811         if(SUCCEEDED(hres))
812             hres = get_default_source(typelib, &clsid, iid);
813         IPersist_Release(persist);
814     }
815 
816     return hres;
817 }
818 
819 #if _ATL_VER >= _ATL_VER90
820 
821 /***********************************************************************
822  *           AtlSetPerUserRegistration [atl100.67]
823  */
824 HRESULT WINAPI AtlSetPerUserRegistration(cpp_bool bEnable)
825 {
826     FIXME("stub: bEnable: %d\n", bEnable);
827     return E_NOTIMPL;
828 }
829 
830 /***********************************************************************
831  *           AtlGetPerUserRegistration  [atl100.68]
832  */
833 HRESULT WINAPI AtlGetPerUserRegistration(cpp_bool *pbEnabled)
834 {
835     FIXME("stub: returning false\n");
836     *pbEnabled = 0;
837     return S_OK;
838 }
839 
840 #endif
841 
842 /***********************************************************************
843  *           AtlGetVersion              [atl100.@]
844  */
845 DWORD WINAPI AtlGetVersion(void *pReserved)
846 {
847     TRACE("version %04x (%p)\n", _ATL_VER, pReserved);
848     return _ATL_VER;
849 }
850 
851 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
852 {
853     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
854 
855     switch(fdwReason) {
856     case DLL_PROCESS_ATTACH:
857         atl_instance = hinstDLL;
858         DisableThreadLibraryCalls(hinstDLL);
859         break;
860     case DLL_PROCESS_DETACH:
861         if (lpvReserved) break;
862         if(catreg)
863             ICatRegister_Release(catreg);
864     }
865 
866     return TRUE;
867 }
868