source: trunk/DirectShowSpy/SystemDeviceEnumeratorSpy.h @ 155

Last change on this file since 155 was 144, checked in by roman, 11 years ago
  • hooking and tracing device enumerator calls
  • minor updates
  • Property svn:keywords set to Id
File size: 23.0 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: SystemDeviceEnumeratorSpy.h 144 2012-11-17 11:15:58Z roman $
6
7#pragma once
8
9#include "rodshow.h"
10#include "DirectShowSpy_i.h"
11#include "Common.h"
12
13////////////////////////////////////////////////////////////
14// CSystemDeviceEnumeratorSpyT
15
16// NOTE: Original CLSID_SystemDeviceEnum does not support aggregation
17
18template <typename T, const CLSID* t_pSystemDeviceEnumeratorClassIdentifier>
19class ATL_NO_VTABLE CSystemDeviceEnumeratorSpyT :
20        public CComObjectRootEx<CComMultiThreadModel>,
21        //public CComCoClass<CSystemDeviceEnumeratorSpyT, &CLSID_SystemDeviceEnumeratorSpy>,
22        public CTransparentCoClassT<T, t_pSystemDeviceEnumeratorClassIdentifier>,
23        public IDispatchImpl<ISystemDeviceEnumeratorSpy>,
24        public ICreateDevEnum
25{
26        typedef CSystemDeviceEnumeratorSpyT<T, t_pSystemDeviceEnumeratorClassIdentifier> CSystemDeviceEnumeratorSpy;
27
28public:
29        //enum { IDR = IDR_SYSTEMDEVICEENUMERATORSPY };
30
31//DECLARE_REGISTRY_RESOURCEID(IDR)
32
33DECLARE_PROTECT_FINAL_CONSTRUCT()
34
35DECLARE_GET_CONTROLLING_UNKNOWN()
36
37DECLARE_QI_TRACE(CSystemDeviceEnumeratorSpy)
38
39BEGIN_COM_MAP(CSystemDeviceEnumeratorSpy)
40        COM_INTERFACE_ENTRY(ISystemDeviceEnumeratorSpy)
41        COM_INTERFACE_ENTRY(ICreateDevEnum)
42        COM_INTERFACE_ENTRY_FUNC(CLSID_SystemDeviceEnum, 0, QuerySystemDeviceEnumInterface)
43        //COM_INTERFACE_ENTRY(IDispatch)
44END_COM_MAP()
45
46public:
47
48        ////////////////////////////////////////////////////////
49        // CPropertyBag
50
51        class ATL_NO_VTABLE CPropertyBag :
52                public CComObjectRootEx<CComMultiThreadModelNoCS>,
53                public IPropertyBag,
54                public IPropertyBag2
55        {
56        public:
57
58        DECLARE_QI_TRACE(CPropertyBag)
59
60        BEGIN_COM_MAP(CPropertyBag)
61                COM_INTERFACE_ENTRY(IPropertyBag2)
62                COM_INTERFACE_ENTRY(IPropertyBag)
63        END_COM_MAP()
64
65        private:
66                CComPtr<IPropertyBag> m_pPropertyBag;
67                CComPtr<IPropertyBag2> m_pPropertyBag2;
68
69        public:
70        // CPropertyBag
71                CPropertyBag()
72                {
73                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
74                }
75                ~CPropertyBag()
76                {
77                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
78                }
79                VOID Initialize(IPropertyBag* pPropertyBag)
80                {
81                        _A(pPropertyBag);
82                        _A(!m_pPropertyBag);
83                        m_pPropertyBag = pPropertyBag;
84                        m_pPropertyBag2 = CComQIPtr<IPropertyBag2>(pPropertyBag);
85                }
86
87        // IPropertyBag
88                STDMETHOD(Read)(LPCOLESTR pszPropertyName, VARIANT* pvValue, IErrorLog* pErrorLog) throw()
89                {
90                        _Z4(atlTraceCOM, 4, _T("pszPropertyName %s\n"), CString(pszPropertyName));
91                        _ATLTRY
92                        {
93                                HRESULT nResult = m_pPropertyBag->Read(pszPropertyName, pvValue, pErrorLog);
94                                #pragma region DevicePath Fix for Skype 6.0.0.126
95                                //if(nResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) && pszPropertyName && _wcsicmp(pszPropertyName, L"DevicePath") == 0 && pvValue)
96                                //{
97                                //      _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
98                                //      CComVariant vValue = L"\\\\?\\fake";
99                                //      _V(vValue.Detach(pvValue));
100                                //      nResult = S_OK;
101                                //}
102                                #pragma endregion
103                                if(SUCCEEDED(nResult))
104                                {
105                                        CString sValue = _T("???");
106                                        CComVariant vStringValue;
107                                        if(SUCCEEDED(vStringValue.ChangeType(VT_BSTR, pvValue)))
108                                                sValue = CString(vStringValue.bstrVal);
109                                        _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x, pvValue->vt 0x%x, %s\n"), nResult, pvValue->vt, sValue);
110                                } else
111                                {
112                                        _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
113                                }
114                                return nResult;
115                        }
116                        _ATLCATCH(Exception)
117                        {
118                                _C(Exception);
119                        }
120                        return S_OK;
121                }
122                STDMETHOD(Write)(LPCOLESTR pszPropertyName, VARIANT* pvValue) throw()
123                {
124                        _Z4(atlTraceCOM, 4, _T("pszPropertyName %s, pvValue->vt 0x%x\n"), CString(pszPropertyName), pvValue->vt);
125                        _ATLTRY
126                        {
127                                const HRESULT nResult = m_pPropertyBag->Write(pszPropertyName, pvValue);
128                                _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
129                                return nResult;
130                        }
131                        _ATLCATCH(Exception)
132                        {
133                                _C(Exception);
134                        }
135                        return S_OK;
136                }
137
138        // IPropertyBag2
139                STDMETHOD(Read)(ULONG cProperties, PROPBAG2 *pPropBag, IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError) throw()
140                {
141                        _Z4(atlTraceCOM, 4, _T("...\n"));
142                        _ATLTRY
143                        {
144                                const HRESULT nResult = m_pPropertyBag2->Read(cProperties, pPropBag, pErrLog, pvarValue, phrError);
145                                _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
146                                return nResult;
147                        }
148                        _ATLCATCH(Exception)
149                        {
150                                _C(Exception);
151                        }
152                        return S_OK;
153                }
154                STDMETHOD(Write)(ULONG cProperties, PROPBAG2 *pPropBag, VARIANT *pvarValue) throw()
155                {
156                        _Z4(atlTraceCOM, 4, _T("...\n"));
157                        _ATLTRY
158                        {
159                                const HRESULT nResult = m_pPropertyBag2->Write(cProperties, pPropBag, pvarValue);
160                                _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
161                                return nResult;
162                        }
163                        _ATLCATCH(Exception)
164                        {
165                                _C(Exception);
166                        }
167                        return S_OK;
168                }
169                STDMETHOD(CountProperties)(ULONG *pcProperties) throw()
170                {
171                        _Z4(atlTraceCOM, 4, _T("...\n"));
172                        _ATLTRY
173                        {
174                                const HRESULT nResult = m_pPropertyBag2->CountProperties(pcProperties);
175                                _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
176                                return nResult;
177                        }
178                        _ATLCATCH(Exception)
179                        {
180                                _C(Exception);
181                        }
182                        return S_OK;
183                }
184                STDMETHOD(GetPropertyInfo)(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG *pcProperties) throw()
185                {
186                        _Z4(atlTraceCOM, 4, _T("...\n"));
187                        _ATLTRY
188                        {
189                                const HRESULT nResult = m_pPropertyBag2->GetPropertyInfo(iProperty, cProperties, pPropBag, pcProperties);
190                                _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
191                                return nResult;
192                        }
193                        _ATLCATCH(Exception)
194                        {
195                                _C(Exception);
196                        }
197                        return S_OK;
198                }
199                STDMETHOD(LoadObject)(LPCOLESTR pstrName, DWORD dwHint, IUnknown *pUnkObject, IErrorLog *pErrLog) throw()
200                {
201                        _Z4(atlTraceCOM, 4, _T("...\n"));
202                        _ATLTRY
203                        {
204                                const HRESULT nResult = m_pPropertyBag2->LoadObject(pstrName, dwHint, pUnkObject, pErrLog);
205                                _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
206                                return nResult;
207                        }
208                        _ATLCATCH(Exception)
209                        {
210                                _C(Exception);
211                        }
212                        return S_OK;
213                }
214        };
215
216        ////////////////////////////////////////////////////////
217        // CMoniker
218
219        class ATL_NO_VTABLE CMoniker :
220                public CComObjectRootEx<CComMultiThreadModelNoCS>,
221                public IMoniker
222        {
223        public:
224
225        DECLARE_QI_TRACE(CMoniker)
226
227        BEGIN_COM_MAP(CMoniker)
228                COM_INTERFACE_ENTRY(IMoniker)
229                COM_INTERFACE_ENTRY(IPersistStream)
230                COM_INTERFACE_ENTRY(IPersist)
231        END_COM_MAP()
232
233        private:
234                CComPtr<IMoniker> m_pMoniker;
235
236        public:
237        // CMoniker
238                CMoniker()
239                {
240                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
241                }
242                ~CMoniker()
243                {
244                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
245                }
246                VOID Initialize(IMoniker* pMoniker)
247                {
248                        _A(pMoniker);
249                        _A(!m_pMoniker);
250                        m_pMoniker = pMoniker;
251                }
252
253        // IPersist
254                STDMETHOD(GetClassID)(CLSID* pClassIdentifier) throw()
255                {
256                        _Z4(atlTraceCOM, 4, _T("...\n"));
257                        return m_pMoniker->GetClassID(pClassIdentifier);
258                }
259
260    // IPersistStream
261                STDMETHOD(IsDirty)() throw()
262                {
263                        _Z4(atlTraceCOM, 4, _T("...\n"));
264                        return m_pMoniker->IsDirty();
265                }
266                STDMETHOD(Load)(IStream* pStream) throw()
267                {
268                        _Z4(atlTraceCOM, 4, _T("...\n"));
269                        return m_pMoniker->Load(pStream);
270                }
271                STDMETHOD(Save)(IStream* pStream, BOOL bClearDirty) throw()
272                {
273                        _Z4(atlTraceCOM, 4, _T("...\n"));
274                        return m_pMoniker->Save(pStream, bClearDirty);
275                }
276                STDMETHOD(GetSizeMax)(ULARGE_INTEGER* pnMaximalSize) throw()
277                {
278                        _Z4(atlTraceCOM, 4, _T("...\n"));
279                        return m_pMoniker->GetSizeMax(pnMaximalSize);
280                }
281
282        // IMoniker
283                STDMETHOD(BindToObject)(IBindCtx* pBindCtx, IMoniker* pLeftMoniker, REFIID InterfaceIdentifier, VOID** ppvObject) throw()
284                {
285                        _Z4(atlTraceCOM, 4, _T("pBindCtx 0x%p, pLeftMoniker 0x%p, InterfaceIdentifier %ls\n"), pBindCtx, pLeftMoniker, _PersistHelper::StringFromInterfaceIdentifier(InterfaceIdentifier));
286                        _ATLTRY
287                        {
288                                return m_pMoniker->BindToObject(pBindCtx, pLeftMoniker, InterfaceIdentifier, ppvObject);
289                        }
290                        _ATLCATCH(Exception)
291                        {
292                                _C(Exception);
293                        }
294                        return S_OK;
295                }
296                STDMETHOD(BindToStorage)(IBindCtx* pBindCtx, IMoniker* pLeftMoniker, REFIID InterfaceIdentifier, VOID** ppvObject) throw()
297                {
298                        _Z4(atlTraceCOM, 4, _T("pBindCtx 0x%p, pLeftMoniker 0x%p, InterfaceIdentifier %ls\n"), pBindCtx, pLeftMoniker, _PersistHelper::StringFromInterfaceIdentifier(InterfaceIdentifier));
299                        _ATLTRY
300                        {
301                                const HRESULT nResult = m_pMoniker->BindToStorage(pBindCtx, pLeftMoniker, InterfaceIdentifier, ppvObject);
302                                if(SUCCEEDED(nResult) && *ppvObject)
303                                {
304                                        if(InterfaceIdentifier == __uuidof(IPropertyBag) || InterfaceIdentifier == __uuidof(IPropertyBag2))
305                                        {
306                                                CComPtr<IPropertyBag>& pPropertyBag = reinterpret_cast<CComPtr<IPropertyBag>&>(*ppvObject);
307                                                CObjectPtr<CPropertyBag> pPropertyBagWrapper;
308                                                pPropertyBagWrapper.Construct()->Initialize(pPropertyBag);
309                                                pPropertyBag = pPropertyBagWrapper;
310                                        }
311                                }
312                                return nResult;
313                        }
314                        _ATLCATCH(Exception)
315                        {
316                                _C(Exception);
317                        }
318                        return S_OK;
319                }
320                STDMETHOD(Reduce)(IBindCtx* pBindCtx, DWORD nDepth, IMoniker** ppLeftMoniker, IMoniker** ppReducedMoniker) throw()
321                {
322                        _Z4(atlTraceCOM, 4, _T("...\n"));
323                        _ATLTRY
324                        {
325                                return m_pMoniker->Reduce(pBindCtx, nDepth, ppLeftMoniker, ppReducedMoniker);
326                        }
327                        _ATLCATCH(Exception)
328                        {
329                                _C(Exception);
330                        }
331                        return S_OK;
332                }
333                STDMETHOD(ComposeWith)(IMoniker* pRightMoniker, BOOL bOnlyIfNotGeneric, IMoniker** ppCompositeMoniker) throw()
334                {
335                        _Z4(atlTraceCOM, 4, _T("...\n"));
336                        _ATLTRY
337                        {
338                                return m_pMoniker->ComposeWith(pRightMoniker, bOnlyIfNotGeneric, ppCompositeMoniker);
339                        }
340                        _ATLCATCH(Exception)
341                        {
342                                _C(Exception);
343                        }
344                        return S_OK;
345                }
346                STDMETHOD(Enum)(BOOL bForward, IEnumMoniker** ppEnumMoniker) throw()
347                {
348                        _Z4(atlTraceCOM, 4, _T("bForward %d\n"), bForward);
349                        _ATLTRY
350                        {
351                                return m_pMoniker->Enum(bForward, ppEnumMoniker);
352                        }
353                        _ATLCATCH(Exception)
354                        {
355                                _C(Exception);
356                        }
357                        return S_OK;
358                }
359                STDMETHOD(IsEqual)(IMoniker* pMoniker) throw()
360                {
361                        _Z4(atlTraceCOM, 4, _T("...\n"));
362                        _ATLTRY
363                        {
364                                return m_pMoniker->IsEqual(pMoniker);
365                        }
366                        _ATLCATCH(Exception)
367                        {
368                                _C(Exception);
369                        }
370                        return S_OK;
371                }
372                STDMETHOD(Hash)(DWORD* pnHashValue) throw()
373                {
374                        _Z4(atlTraceCOM, 4, _T("...\n"));
375                        _ATLTRY
376                        {
377                                return m_pMoniker->Hash(pnHashValue);
378                        }
379                        _ATLCATCH(Exception)
380                        {
381                                _C(Exception);
382                        }
383                        return S_OK;
384                }
385                STDMETHOD(IsRunning)(IBindCtx* pBindCtx, IMoniker* pLeftMoniker, IMoniker* pNewlyRunning) throw()
386                {
387                        _Z4(atlTraceCOM, 4, _T("...\n"));
388                        _ATLTRY
389                        {
390                                return m_pMoniker->IsRunning(pBindCtx, pLeftMoniker, pNewlyRunning);
391                        }
392                        _ATLCATCH(Exception)
393                        {
394                                _C(Exception);
395                        }
396                        return S_OK;
397                }
398                STDMETHOD(GetTimeOfLastChange)(IBindCtx* pBindCtx, IMoniker* pLeftMoniker, FILETIME* pFileTime) throw()
399                {
400                        _Z4(atlTraceCOM, 4, _T("...\n"));
401                        _ATLTRY
402                        {
403                                return m_pMoniker->GetTimeOfLastChange(pBindCtx, pLeftMoniker, pFileTime);
404                        }
405                        _ATLCATCH(Exception)
406                        {
407                                _C(Exception);
408                        }
409                        return S_OK;
410                }
411                STDMETHOD(Inverse)(IMoniker** ppMoniker) throw()
412                {
413                        _Z4(atlTraceCOM, 4, _T("...\n"));
414                        _ATLTRY
415                        {
416                                return m_pMoniker->Inverse(ppMoniker);
417                        }
418                        _ATLCATCH(Exception)
419                        {
420                                _C(Exception);
421                        }
422                        return S_OK;
423                }
424                STDMETHOD(CommonPrefixWith)(IMoniker* pMoniker, IMoniker** ppPrefixMoniker) throw()
425                {
426                        _Z4(atlTraceCOM, 4, _T("...\n"));
427                        _ATLTRY
428                        {
429                                return m_pMoniker->CommonPrefixWith(pMoniker, ppPrefixMoniker);
430                        }
431                        _ATLCATCH(Exception)
432                        {
433                                _C(Exception);
434                        }
435                        return S_OK;
436                }
437                STDMETHOD(RelativePathTo)(IMoniker* pMoniker, IMoniker** ppRelativeMoniker) throw()
438                {
439                        _Z4(atlTraceCOM, 4, _T("...\n"));
440                        _ATLTRY
441                        {
442                                return m_pMoniker->RelativePathTo(pMoniker, ppRelativeMoniker);
443                        }
444                        _ATLCATCH(Exception)
445                        {
446                                _C(Exception);
447                        }
448                        return S_OK;
449                }
450                STDMETHOD(GetDisplayName)(IBindCtx* pBindCtx, IMoniker* pLeftMoniker, LPOLESTR* ppszDisplayName) throw()
451                {
452                        _Z4(atlTraceCOM, 4, _T("pBindCtx 0x%p, pLeftMoniker 0x%p\n"), pBindCtx, pLeftMoniker);
453                        _ATLTRY
454                        {
455                                const HRESULT nResult = m_pMoniker->GetDisplayName(pBindCtx, pLeftMoniker, ppszDisplayName);
456                                if(SUCCEEDED(nResult))
457                                        _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x, *ppszDisplayName %s\n"), nResult, CString(*ppszDisplayName));
458                                else
459                                        _Z4(atlTraceGeneral, 4, _T("nResult 0x%08x\n"), nResult);
460                                return nResult;
461                        }
462                        _ATLCATCH(Exception)
463                        {
464                                _C(Exception);
465                        }
466                        return S_OK;
467                }
468                STDMETHOD(ParseDisplayName)(IBindCtx* pBindCtx, IMoniker* pLeftMoniker, LPOLESTR pszDisplayName, ULONG* pnEatenLength, IMoniker** ppOutputMoniker) throw()
469                {
470                        _Z4(atlTraceCOM, 4, _T("pBindCtx 0x%p, pLeftMoniker 0x%p, pszDisplayName %s\n"), pBindCtx, pLeftMoniker, CString(pszDisplayName));
471                        _ATLTRY
472                        {
473                                return m_pMoniker->ParseDisplayName(pBindCtx, pLeftMoniker, pszDisplayName, pnEatenLength, ppOutputMoniker);
474                        }
475                        _ATLCATCH(Exception)
476                        {
477                                _C(Exception);
478                        }
479                        return S_OK;
480                }
481                STDMETHOD(IsSystemMoniker)(DWORD* pnSystemMonikerType) throw()
482                {
483                        _Z4(atlTraceCOM, 4, _T("...\n"));
484                        _ATLTRY
485                        {
486                                return m_pMoniker->IsSystemMoniker(pnSystemMonikerType);
487                        }
488                        _ATLCATCH(Exception)
489                        {
490                                _C(Exception);
491                        }
492                        return S_OK;
493                }
494        };
495
496        ////////////////////////////////////////////////////////
497        // CEnumMoniker
498
499        class ATL_NO_VTABLE CEnumMoniker :
500                public CComObjectRootEx<CComMultiThreadModelNoCS>,
501                public IEnumMoniker
502        {
503        public:
504
505        DECLARE_QI_TRACE(CEnumMoniker)
506
507        BEGIN_COM_MAP(CEnumMoniker)
508                COM_INTERFACE_ENTRY(IEnumMoniker)
509        END_COM_MAP()
510
511        private:
512                CComPtr<IEnumMoniker> m_pEnumMoniker;
513
514        public:
515        // CEnumMoniker
516                CEnumMoniker()
517                {
518                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
519                }
520                ~CEnumMoniker()
521                {
522                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
523                }
524                VOID Initialize(IEnumMoniker* pEnumMoniker)
525                {
526                        _A(pEnumMoniker);
527                        _A(!m_pEnumMoniker);
528                        m_pEnumMoniker = pEnumMoniker;
529                }
530
531        // IEnumMoniker
532                STDMETHOD(Next)(ULONG nElementCount, IMoniker** ppMoniker, ULONG* pnFetchedElementCount) throw()
533                {
534                        _Z4(atlTraceCOM, 4, _T("nElementCount %d\n"), nElementCount);
535                        _ATLTRY
536                        {
537                                const HRESULT nNextResult = m_pEnumMoniker->Next(nElementCount, ppMoniker, pnFetchedElementCount);
538                                _Z4(atlTraceGeneral, 4, _T("nNextResult 0x%08x, *pnFetchedElementCount %s\n"), nNextResult, pnFetchedElementCount ? (LPCTSTR) AtlFormatString(_T("%d"), *pnFetchedElementCount) : _T("(null)"));
539                                if(SUCCEEDED(nNextResult))
540                                {
541                                        ULONG nFetchedElementCount = nElementCount;
542                                        if(nNextResult != S_OK && pnFetchedElementCount)
543                                                nFetchedElementCount = *pnFetchedElementCount;
544                                        for(SIZE_T nIndex = 0; nIndex < nFetchedElementCount; nIndex++)
545                                        {
546                                                CComPtr<IMoniker>& pMoniker = reinterpret_cast<CComPtr<IMoniker>&>(ppMoniker[nIndex]);
547                                                if(!pMoniker)
548                                                        break;
549                                                CObjectPtr<CMoniker> pMonikerWrapper;
550                                                pMonikerWrapper.Construct()->Initialize(pMoniker);
551                                                pMoniker = pMonikerWrapper;
552                                        }
553                                }
554                                return nNextResult;
555                        }
556                        _ATLCATCH(Exception)
557                        {
558                                _C(Exception);
559                        }
560                        return S_OK;
561                }
562                STDMETHOD(Skip)(ULONG nElementCount) throw()
563                {
564                        _Z4(atlTraceCOM, 4, _T("nElementCount %d\n"), nElementCount);
565                        _ATLTRY
566                        {
567                                return m_pEnumMoniker->Skip(nElementCount);
568                        }
569                        _ATLCATCH(Exception)
570                        {
571                                _C(Exception);
572                        }
573                        return S_OK;
574                }
575                STDMETHOD(Reset)() throw()
576                {
577                        _Z4(atlTraceCOM, 4, _T("...\n"));
578                        _ATLTRY
579                        {
580                                return m_pEnumMoniker->Reset();
581                        }
582                        _ATLCATCH(Exception)
583                        {
584                                _C(Exception);
585                        }
586                        return S_OK;
587                }
588                STDMETHOD(Clone)(IEnumMoniker** ppEnumMoniker) throw()
589                {
590                        _Z4(atlTraceCOM, 4, _T("...\n"));
591                        _ATLTRY
592                        {
593                                __C(m_pEnumMoniker->Clone(ppEnumMoniker));
594                                CComPtr<IEnumMoniker>& pEnumMoniker = reinterpret_cast<CComPtr<IEnumMoniker>&>(*ppEnumMoniker);
595                                _A(pEnumMoniker);
596                                CObjectPtr<CEnumMoniker> pEnumMonikerWrapper;
597                                pEnumMonikerWrapper.Construct()->Initialize(pEnumMoniker);
598                                pEnumMoniker = pEnumMonikerWrapper;
599                        }
600                        _ATLCATCH(Exception)
601                        {
602                                _C(Exception);
603                        }
604                        return S_OK;
605                }
606        };
607
608private:
609        HINSTANCE m_hDevEnumModule;
610        CComPtr<IUnknown> m_pSystemDeviceEnum;
611        CComPtr<ICreateDevEnum> m_pCreateDevEnum;
612
613        HRESULT QuerySystemDeviceEnumInterface(REFIID InterfaceIdentifier, VOID** ppvObject)
614        {
615                _A(InterfaceIdentifier == CLSID_SystemDeviceEnum);
616                _A(ppvObject);
617                *ppvObject = CComPtr<IUnknown>(m_pSystemDeviceEnum).Detach();
618                return S_OK;
619        }
620        static HRESULT WINAPI QuerySystemDeviceEnumInterface(VOID* pvInstance, REFIID InterfaceIdentifier, VOID** ppvObject, DWORD)
621        {
622                return ((CSystemDeviceEnumeratorSpy*) pvInstance)->QuerySystemDeviceEnumInterface(InterfaceIdentifier, ppvObject);
623        }
624
625public:
626// CSystemDeviceEnumeratorSpyT
627        static LPCTSTR GetOriginalLibraryName() throw()
628        {
629                return _T("devenum.dll");
630        }
631        static CString GetObjectFriendlyName()
632        {
633                return _StringHelper::GetLine(T::IDR, 2);
634        }
635        static HRESULT WINAPI UpdateRegistry(BOOL bRegister) throw()
636        {
637                _Z2(atlTraceRegistrar, 2, _T("bRegister %d\n"), bRegister);
638                _ATLTRY
639                {
640                        TreatAsUpdateRegistryFromResource<T>(*t_pSystemDeviceEnumeratorClassIdentifier, bRegister);
641                }
642                _ATLCATCH(Exception)
643                {
644                        _C(Exception);
645                }
646                return S_OK;
647        }
648        CSystemDeviceEnumeratorSpyT() throw() :
649                m_hDevEnumModule(NULL)
650        {
651                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
652        }
653        ~CSystemDeviceEnumeratorSpyT() throw()
654        {
655                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
656        }
657        HRESULT FinalConstruct() throw()
658        {
659                _ATLTRY
660                {
661                        TCHAR pszPath[MAX_PATH] = { 0 };
662                        _W(GetModuleFileName(NULL, pszPath, DIM(pszPath)));
663                        _Z4(atlTraceRefcount, 4, _T("pszPath \"%s\", this 0x%08x, m_dwRef %d\n"), pszPath, this, m_dwRef);
664                        const HINSTANCE hModule = CoLoadLibrary(const_cast<LPOLESTR>((LPCOLESTR) CT2COLE(GetOriginalLibraryName())), TRUE);
665                        _ATLTRY
666                        {
667                                typedef HRESULT (WINAPI *DLLGETCLASSOBJECT)(REFCLSID, REFIID, VOID**);
668                                DLLGETCLASSOBJECT DllGetClassObject = (DLLGETCLASSOBJECT) GetProcAddress(hModule, "DllGetClassObject");
669                                __E(DllGetClassObject);
670                                CComPtr<IClassFactory> pClassFactory;
671                                __C(DllGetClassObject(*t_pSystemDeviceEnumeratorClassIdentifier, __uuidof(IClassFactory), (VOID**) &pClassFactory));
672                                _A(pClassFactory);
673                                CComPtr<IUnknown> pUnknown;
674                                __C(pClassFactory->CreateInstance(NULL, __uuidof(IUnknown), (VOID**) &pUnknown));
675                                __D(pUnknown, E_NOINTERFACE);
676                                CComPtr<IUnknown> pSystemDeviceEnum;
677                                __C(pUnknown->QueryInterface(CLSID_SystemDeviceEnum, (VOID**) &pSystemDeviceEnum));
678                                const CComQIPtr<ICreateDevEnum> pCreateDevEnum = pUnknown;
679                                __D(pCreateDevEnum, E_NOINTERFACE);
680                                m_pSystemDeviceEnum = pSystemDeviceEnum;
681                                m_pCreateDevEnum = pCreateDevEnum;
682                        }
683                        _ATLCATCHALL()
684                        {
685                                CoFreeLibrary(hModule);
686                                _ATLRETHROW;
687                        }
688                        _A(!m_hDevEnumModule);
689                        m_hDevEnumModule = hModule;
690                }
691                _ATLCATCH(Exception)
692                {
693                        _C(Exception);
694                }
695                return S_OK;
696        }
697        VOID FinalRelease() throw()
698        {
699                _Z5(atlTraceRefcount, 5, _T("m_dwRef 0x%x\n"), m_dwRef);
700                m_pSystemDeviceEnum = NULL;
701                m_pCreateDevEnum = NULL;
702                if(m_hDevEnumModule)
703                {
704                        CoFreeLibrary(m_hDevEnumModule);
705                        m_hDevEnumModule = NULL;
706                }
707        }
708        static CString FormatDeviceCategory(const GUID& DeviceCategory)
709        {
710                static const struct { const GUID* pIdentifier; LPCSTR pszName; } g_pMap[] = 
711                {
712                        #define A(x) { &x, #x },
713                        A(CLSID_VideoInputDeviceCategory)
714                        A(CLSID_LegacyAmFilterCategory)
715                        A(CLSID_VideoCompressorCategory)
716                        A(CLSID_AudioCompressorCategory)
717                        A(CLSID_AudioInputDeviceCategory)
718                        A(CLSID_AudioRendererCategory)
719                        A(CLSID_MidiRendererCategory)
720                        A(CLSID_TransmitCategory)
721                        A(CLSID_DeviceControlCategory)
722                        A(CLSID_ActiveMovieCategories)
723                        A(CLSID_DVDHWDecodersCategory)
724                        A(CLSID_MediaEncoderCategory)
725                        A(CLSID_MediaMultiplexerCategory)
726                        #undef A
727                };
728                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
729                        if(*g_pMap[nIndex].pIdentifier == DeviceCategory)
730                                return CString(g_pMap[nIndex].pszName);
731                return CString(_PersistHelper::StringFromIdentifier(DeviceCategory));
732        }
733
734// ISystemDeviceEnumeratorSpy
735
736// ICreateDevEnum
737        STDMETHOD(CreateClassEnumerator)(REFCLSID DeviceCategory, IEnumMoniker** ppEnumMoniker, DWORD nFlags) throw()
738        {
739                _Z4(atlTraceCOM, 4, _T("DeviceCategory %s, nFlags 0x%x\n"), FormatDeviceCategory(DeviceCategory), nFlags);
740                _ATLTRY
741                {
742                        __C(m_pCreateDevEnum->CreateClassEnumerator(DeviceCategory, ppEnumMoniker, nFlags));
743                        CComPtr<IEnumMoniker>& pEnumMoniker = reinterpret_cast<CComPtr<IEnumMoniker>&>(*ppEnumMoniker);
744                        if(pEnumMoniker)
745                        {
746                                #pragma region Trace Monikers
747                                __C(pEnumMoniker->Reset());
748                                CComPtr<IMoniker> pMoniker;
749                                while(pEnumMoniker->Next(1, &pMoniker, NULL) == S_OK)
750                                {
751                                        _Z4(atlTraceCOM, 4, _T("pMoniker %ls\n"), _FilterGraphHelper::GetMonikerDisplayName(pMoniker));
752                                        CComPtr<IBindCtx> pBindCtx;
753                                        __C(CreateBindCtx(0, &pBindCtx));
754                                        CComPtr<IPropertyBag> pPropertyBag;
755                                        __C(pMoniker->BindToStorage(pBindCtx, NULL, __uuidof(IPropertyBag), (VOID**) &pPropertyBag));
756                                        const CStringW sFriendlyName = _FilterGraphHelper::ReadPropertyBagString(pPropertyBag, OLESTR("FriendlyName"));
757                                        const CStringW sDescription = _FilterGraphHelper::ReadPropertyBagString(pPropertyBag, OLESTR("Description"));
758                                        const CStringW sDevicePath = _FilterGraphHelper::ReadPropertyBagString(pPropertyBag, OLESTR("DevicePath"));
759                                        _Z4(atlTraceCOM, 4, _T("sFriendlyName \"%ls\", sDescription \"%ls\", sDevicePath \"%ls\"\n"), sFriendlyName, sDescription, sDevicePath);
760                                        pMoniker.Release();
761                                }
762                                __C(pEnumMoniker->Reset());
763                                #pragma endregion
764                                CObjectPtr<CEnumMoniker> pEnumMonikerWrapper;
765                                pEnumMonikerWrapper.Construct()->Initialize(pEnumMoniker);
766                                pEnumMoniker = pEnumMonikerWrapper;
767                        }
768                }
769                _ATLCATCH(Exception)
770                {
771                        _C(Exception);
772                }
773                return S_OK;
774        }
775};
776
777////////////////////////////////////////////////////////////
778// CSystemDeviceEnumeratorSpy
779
780class ATL_NO_VTABLE CSystemDeviceEnumeratorSpy :
781        public CSystemDeviceEnumeratorSpyT<CSystemDeviceEnumeratorSpy, &CLSID_SystemDeviceEnum>,
782        public CComCoClass<CSystemDeviceEnumeratorSpy, &CLSID_SystemDeviceEnumeratorSpy>
783{
784public:
785        enum { IDR = IDR_SYSTEMDEVICEENUMERATORSPY };
786
787private:
788        static LPCTSTR g_pszClassName;
789
790public:
791        //typedef CBlackListAwareComCreatorT<CComObjectCached<CSystemDeviceEnumeratorSpy>, CSystemDeviceEnumeratorSpy, &g_pszClassName> _ClassFactoryCreatorClass; // DECLARE_CLASSFACTORY override
792        typedef CComCreator2<CBlackListAwareComCreatorT<CComObject<CSystemDeviceEnumeratorSpy>, CSystemDeviceEnumeratorSpy, &g_pszClassName>, CBlackListAwareComCreatorT<CComAggObject<CSystemDeviceEnumeratorSpy>, CSystemDeviceEnumeratorSpy, &g_pszClassName> > _CreatorClass; // DECLARE_AGGREGATABLE override
793
794public:
795// CSystemDeviceEnumeratorSpy
796};
797
798__declspec(selectany) LPCTSTR CSystemDeviceEnumeratorSpy::g_pszClassName = _T("CSystemDeviceEnumeratorSpy");
799
800OBJECT_ENTRY_AUTO(__uuidof(SystemDeviceEnumeratorSpy), CSystemDeviceEnumeratorSpy)
Note: See TracBrowser for help on using the repository browser.