msi: Fix a leaking record handle.
[wine] / dlls / dxdiagn / provider.c
1 /* 
2  * IDxDiagProvider Implementation
3  * 
4  * Copyright 2004-2005 Raphael Junqueira
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include "config.h"
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "dxdiag_private.h"
28 #include "wine/unicode.h"
29 #include "winver.h"
30 #include "objidl.h"
31 #include "dshow.h"
32 #include "vfw.h"
33 #include "mmddk.h"
34 #include "ddraw.h"
35 #include "d3d9.h"
36 #include "strmif.h"
37 #include "initguid.h"
38 #include "fil_data.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
43
44 static HRESULT DXDiag_InitRootDXDiagContainer(IDxDiagContainer* pRootCont);
45
46 /* IDxDiagProvider IUnknown parts follow: */
47 static HRESULT WINAPI IDxDiagProviderImpl_QueryInterface(PDXDIAGPROVIDER iface, REFIID riid, LPVOID *ppobj)
48 {
49     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
50
51     if (IsEqualGUID(riid, &IID_IUnknown)
52         || IsEqualGUID(riid, &IID_IDxDiagProvider)) {
53         IUnknown_AddRef(iface);
54         *ppobj = This;
55         return S_OK;
56     }
57
58     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
59     return E_NOINTERFACE;
60 }
61
62 static ULONG WINAPI IDxDiagProviderImpl_AddRef(PDXDIAGPROVIDER iface) {
63     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
64     ULONG refCount = InterlockedIncrement(&This->ref);
65
66     TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
67
68     DXDIAGN_LockModule();
69
70     return refCount;
71 }
72
73 static ULONG WINAPI IDxDiagProviderImpl_Release(PDXDIAGPROVIDER iface) {
74     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
75     ULONG refCount = InterlockedDecrement(&This->ref);
76
77     TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
78
79     if (!refCount) {
80         HeapFree(GetProcessHeap(), 0, This);
81     }
82
83     DXDIAGN_UnlockModule();
84     
85     return refCount;
86 }
87
88 /* IDxDiagProvider Interface follow: */
89 static HRESULT WINAPI IDxDiagProviderImpl_Initialize(PDXDIAGPROVIDER iface, DXDIAG_INIT_PARAMS* pParams) {
90     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
91     TRACE("(%p,%p)\n", iface, pParams);
92
93     if (NULL == pParams) {
94       return E_POINTER;
95     }
96     if (pParams->dwSize != sizeof(DXDIAG_INIT_PARAMS)) {
97       return E_INVALIDARG;
98     }
99
100     This->init = TRUE;
101     memcpy(&This->params, pParams, pParams->dwSize);
102     return S_OK;
103 }
104
105 static HRESULT WINAPI IDxDiagProviderImpl_GetRootContainer(PDXDIAGPROVIDER iface, IDxDiagContainer** ppInstance) {
106   HRESULT hr = S_OK;
107   IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
108   TRACE("(%p,%p)\n", iface, ppInstance);
109
110   if (NULL == ppInstance) {
111     return E_INVALIDARG;
112   }
113   if (FALSE == This->init) {
114     return E_INVALIDARG; /* should be E_CO_UNINITIALIZED */
115   }
116   if (NULL == This->pRootContainer) {
117     hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &This->pRootContainer);
118     if (FAILED(hr)) {
119       return hr;
120     }
121     hr = DXDiag_InitRootDXDiagContainer(This->pRootContainer);
122   }
123   return IDxDiagContainerImpl_QueryInterface((PDXDIAGCONTAINER)This->pRootContainer, &IID_IDxDiagContainer, (void**) ppInstance);
124 }
125
126 static const IDxDiagProviderVtbl DxDiagProvider_Vtbl =
127 {
128     IDxDiagProviderImpl_QueryInterface,
129     IDxDiagProviderImpl_AddRef,
130     IDxDiagProviderImpl_Release,
131     IDxDiagProviderImpl_Initialize,
132     IDxDiagProviderImpl_GetRootContainer
133 };
134
135 HRESULT DXDiag_CreateDXDiagProvider(LPCLASSFACTORY iface, LPUNKNOWN punkOuter, REFIID riid, LPVOID *ppobj) {
136   IDxDiagProviderImpl* provider;
137
138   TRACE("(%p, %s, %p)\n", punkOuter, debugstr_guid(riid), ppobj);
139   
140   provider = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagProviderImpl));
141   if (NULL == provider) {
142     *ppobj = NULL;
143     return E_OUTOFMEMORY;
144   }
145   provider->lpVtbl = &DxDiagProvider_Vtbl;
146   provider->ref = 0; /* will be inited with QueryInterface */
147   return IDxDiagProviderImpl_QueryInterface ((PDXDIAGPROVIDER)provider, riid, ppobj);
148
149
150 static inline HRESULT add_prop_str( IDxDiagContainer* cont, LPCWSTR prop, LPCWSTR str )
151 {
152     HRESULT hr;
153     VARIANT var;
154
155     V_VT( &var ) = VT_BSTR;
156     V_BSTR( &var ) = SysAllocString( str );
157     hr = IDxDiagContainerImpl_AddProp( cont, prop, &var );
158     VariantClear( &var );
159
160     return hr;
161 }
162
163 static inline HRESULT add_prop_ui4( IDxDiagContainer* cont, LPCWSTR prop, DWORD data )
164 {
165     VARIANT var;
166
167     V_VT( &var ) = VT_UI4;
168     V_UI4( &var ) = data;
169     return IDxDiagContainerImpl_AddProp( cont, prop, &var );
170 }
171
172 static inline HRESULT add_prop_bool( IDxDiagContainer* cont, LPCWSTR prop, BOOL data )
173 {
174     VARIANT var;
175
176     V_VT( &var ) = VT_BOOL;
177     V_BOOL( &var ) = data;
178     return IDxDiagContainerImpl_AddProp( cont, prop, &var );
179 }
180
181 static inline HRESULT add_prop_ull_as_str( IDxDiagContainer* cont, LPCWSTR prop, ULONGLONG data )
182 {
183     HRESULT hr;
184     VARIANT var;
185
186     V_VT( &var ) = VT_UI8;
187     V_UI8( &var ) = data;
188     VariantChangeType( &var, &var, 0, VT_BSTR );
189     hr = IDxDiagContainerImpl_AddProp( cont, prop, &var );
190     VariantClear( &var );
191
192     return hr;
193 }
194
195 static void get_display_device_id(WCHAR *szIdentifierBuffer)
196 {
197     static const WCHAR szNA[] = {'n','/','a',0};
198
199     HRESULT hr = E_FAIL;
200
201     HMODULE                 d3d9_handle;
202     IDirect3D9             *(WINAPI *pDirect3DCreate9)(UINT) = NULL;
203     IDirect3D9             *pD3d = NULL;
204     D3DADAPTER_IDENTIFIER9  adapter_ident;
205
206     /* Retrieves the display device identifier from the d3d9 implementation. */
207     d3d9_handle = LoadLibraryA("d3d9.dll");
208     if(d3d9_handle)
209         pDirect3DCreate9 = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
210     if(pDirect3DCreate9)
211         pD3d = pDirect3DCreate9(D3D_SDK_VERSION);
212     if(pD3d)
213         hr = IDirect3D9_GetAdapterIdentifier(pD3d, D3DADAPTER_DEFAULT, 0, &adapter_ident);
214     if(SUCCEEDED(hr)) {
215         StringFromGUID2(&adapter_ident.DeviceIdentifier, szIdentifierBuffer, 39);
216     } else {
217         memcpy(szIdentifierBuffer, szNA, sizeof(szNA));
218     }
219
220     if (pD3d)
221         IDirect3D9_Release(pD3d);
222     if (d3d9_handle)
223         FreeLibrary(d3d9_handle);
224 }
225
226 /**
227  * @param szFilePath: usually GetSystemDirectoryW
228  * @param szFileName: name of the dll without path
229  */
230 static HRESULT DXDiag_AddFileDescContainer(IDxDiagContainer* pSubCont, const WCHAR* szFilePath, const WCHAR* szFileName) {
231   HRESULT hr = S_OK;
232   /**/
233   static const WCHAR szSlashSep[] = {'\\',0};
234   static const WCHAR szPath[] = {'s','z','P','a','t','h',0};
235   static const WCHAR szName[] = {'s','z','N','a','m','e',0};
236   static const WCHAR szVersion[] = {'s','z','V','e','r','s','i','o','n',0};
237   static const WCHAR szAttributes[] = {'s','z','A','t','t','r','i','b','u','t','e','s',0};
238   static const WCHAR szLanguageEnglish[] = {'s','z','L','a','n','g','u','a','g','e','E','n','g','l','i','s','h',0};
239   static const WCHAR dwFileTimeHigh[] = {'d','w','F','i','l','e','T','i','m','e','H','i','g','h',0};
240   static const WCHAR dwFileTimeLow[] = {'d','w','F','i','l','e','T','i','m','e','L','o','w',0};
241   static const WCHAR bBeta[] = {'b','B','e','t','a',0};
242   static const WCHAR bDebug[] = {'b','D','e','b','u','g',0};
243   static const WCHAR bExists[] = {'b','E','x','i','s','t','s',0};
244   /** values */
245   static const WCHAR szFinal_Retail_v[] = {'F','i','n','a','l',' ','R','e','t','a','i','l',0};
246   static const WCHAR szEnglish_v[] = {'E','n','g','l','i','s','h',0};
247   static const WCHAR szVersionFormat[] = {'%','u','.','%','0','2','u','.','%','0','4','u','.','%','0','4','u',0};
248
249   WCHAR szFile[512];
250   WCHAR szVersion_v[1024];
251   DWORD retval, hdl;
252   LPVOID pVersionInfo;
253   BOOL boolret;
254   UINT uiLength;
255   VS_FIXEDFILEINFO* pFileInfo;
256
257   TRACE("(%p,%s)\n", pSubCont, debugstr_w(szFileName));
258
259   lstrcpyW(szFile, szFilePath);
260   lstrcatW(szFile, szSlashSep);
261   lstrcatW(szFile, szFileName);
262
263   retval = GetFileVersionInfoSizeW(szFile, &hdl);
264   pVersionInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
265   boolret = GetFileVersionInfoW(szFile, 0, retval, pVersionInfo);
266   boolret = VerQueryValueW(pVersionInfo, szSlashSep, (LPVOID) &pFileInfo, &uiLength);
267
268   add_prop_str(pSubCont, szPath, szFile);
269   add_prop_str(pSubCont, szName, szFileName);
270   add_prop_bool(pSubCont, bExists, boolret);
271
272   if (boolret) {
273     snprintfW(szVersion_v, sizeof(szVersion_v)/sizeof(szVersion_v[0]),
274               szVersionFormat,
275               HIWORD(pFileInfo->dwFileVersionMS), 
276               LOWORD(pFileInfo->dwFileVersionMS),
277               HIWORD(pFileInfo->dwFileVersionLS),
278               LOWORD(pFileInfo->dwFileVersionLS));
279
280     TRACE("Found version as (%s)\n", debugstr_w(szVersion_v));
281
282     add_prop_str(pSubCont, szVersion,         szVersion_v);
283     add_prop_str(pSubCont, szAttributes,      szFinal_Retail_v);
284     add_prop_str(pSubCont, szLanguageEnglish, szEnglish_v);
285     add_prop_ui4(pSubCont, dwFileTimeHigh,    pFileInfo->dwFileDateMS);
286     add_prop_ui4(pSubCont, dwFileTimeLow,     pFileInfo->dwFileDateLS);
287     add_prop_bool(pSubCont, bBeta,  0 != ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_PRERELEASE));
288     add_prop_bool(pSubCont, bDebug, 0 != ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_DEBUG));
289   }
290
291   HeapFree(GetProcessHeap(), 0, pVersionInfo);
292
293   return hr;
294 }
295
296 static HRESULT DXDiag_InitDXDiagSystemInfoContainer(IDxDiagContainer* pSubCont) {
297   static const WCHAR dwDirectXVersionMajor[] = {'d','w','D','i','r','e','c','t','X','V','e','r','s','i','o','n','M','a','j','o','r',0};
298   static const WCHAR dwDirectXVersionMinor[] = {'d','w','D','i','r','e','c','t','X','V','e','r','s','i','o','n','M','i','n','o','r',0};
299   static const WCHAR szDirectXVersionLetter[] = {'s','z','D','i','r','e','c','t','X','V','e','r','s','i','o','n','L','e','t','t','e','r',0};
300   static const WCHAR szDirectXVersionLetter_v[] = {'c',0};
301   static const WCHAR bDebug[] = {'b','D','e','b','u','g',0};
302   static const WCHAR szDirectXVersionEnglish[] = {'s','z','D','i','r','e','c','t','X','V','e','r','s','i','o','n','E','n','g','l','i','s','h',0};
303   static const WCHAR szDirectXVersionEnglish_v[] = {'4','.','0','9','.','0','0','0','0','.','0','9','0','4',0};
304   static const WCHAR szDirectXVersionLongEnglish[] = {'s','z','D','i','r','e','c','t','X','V','e','r','s','i','o','n','L','o','n','g','E','n','g','l','i','s','h',0};
305   static const WCHAR szDirectXVersionLongEnglish_v[] = {'=',' ','"','D','i','r','e','c','t','X',' ','9','.','0','c',' ','(','4','.','0','9','.','0','0','0','0','.','0','9','0','4',')',0};
306   static const WCHAR ullPhysicalMemory[] = {'u','l','l','P','h','y','s','i','c','a','l','M','e','m','o','r','y',0};
307   static const WCHAR ullUsedPageFile[]   = {'u','l','l','U','s','e','d','P','a','g','e','F','i','l','e',0};
308   static const WCHAR ullAvailPageFile[]  = {'u','l','l','A','v','a','i','l','P','a','g','e','F','i','l','e',0};
309   /*static const WCHAR szDxDiagVersion[] = {'s','z','D','x','D','i','a','g','V','e','r','s','i','o','n',0};*/
310   static const WCHAR szWindowsDir[] = {'s','z','W','i','n','d','o','w','s','D','i','r',0};
311   static const WCHAR dwOSMajorVersion[] = {'d','w','O','S','M','a','j','o','r','V','e','r','s','i','o','n',0};
312   static const WCHAR dwOSMinorVersion[] = {'d','w','O','S','M','i','n','o','r','V','e','r','s','i','o','n',0};
313   static const WCHAR dwOSBuildNumber[] = {'d','w','O','S','B','u','i','l','d','N','u','m','b','e','r',0};
314   static const WCHAR dwOSPlatformID[] = {'d','w','O','S','P','l','a','t','f','o','r','m','I','D',0};
315   static const WCHAR szCSDVersion[] = {'s','z','C','S','D','V','e','r','s','i','o','n',0};
316   MEMORYSTATUSEX msex;
317   OSVERSIONINFOW info;
318   WCHAR buffer[MAX_PATH];
319
320   add_prop_ui4(pSubCont, dwDirectXVersionMajor, 9);
321   add_prop_ui4(pSubCont, dwDirectXVersionMinor, 0);
322   add_prop_str(pSubCont, szDirectXVersionLetter, szDirectXVersionLetter_v);
323   add_prop_str(pSubCont, szDirectXVersionEnglish, szDirectXVersionEnglish_v);
324   add_prop_str(pSubCont, szDirectXVersionLongEnglish, szDirectXVersionLongEnglish_v);
325   add_prop_bool(pSubCont, bDebug, FALSE);
326
327   msex.dwLength = sizeof(msex);
328   GlobalMemoryStatusEx( &msex );
329   add_prop_ull_as_str(pSubCont, ullPhysicalMemory, msex.ullTotalPhys);
330   add_prop_ull_as_str(pSubCont, ullUsedPageFile, msex.ullTotalPageFile - msex.ullAvailPageFile);
331   add_prop_ull_as_str(pSubCont, ullAvailPageFile, msex.ullAvailPageFile);
332
333   info.dwOSVersionInfoSize = sizeof(info);
334   GetVersionExW( &info );
335   add_prop_ui4(pSubCont, dwOSMajorVersion, info.dwMajorVersion);
336   add_prop_ui4(pSubCont, dwOSMinorVersion, info.dwMinorVersion);
337   add_prop_ui4(pSubCont, dwOSBuildNumber,  info.dwBuildNumber);
338   add_prop_ui4(pSubCont, dwOSPlatformID,   info.dwPlatformId);
339   add_prop_str(pSubCont, szCSDVersion,     info.szCSDVersion);
340
341   GetWindowsDirectoryW(buffer, MAX_PATH);
342   add_prop_str(pSubCont, szWindowsDir, buffer);
343
344   return S_OK;
345 }
346
347 static HRESULT DXDiag_InitDXDiagSystemDevicesContainer(IDxDiagContainer* pSubCont) {
348   HRESULT hr = S_OK;
349   /*
350   static const WCHAR szDescription[] = {'s','z','D','e','s','c','r','i','p','t','i','o','n',0};
351   static const WCHAR szDeviceID[] = {'s','z','D','e','v','i','c','e','I','D',0};
352
353   static const WCHAR szDrivers[] = {'s','z','D','r','i','v','e','r','s',0};
354
355   VARIANT v;
356   IDxDiagContainer* pDeviceSubCont = NULL;
357   IDxDiagContainer* pDriversCont = NULL;
358
359   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, &pDeviceSubCont);
360   if (FAILED(hr)) { return hr; }
361   V_VT(pvarProp) = VT_BSTR; V_BSTR(pvarProp) = SysAllocString(property->psz);
362   hr = IDxDiagContainerImpl_AddProp(pDeviceSubCont, szDescription, &v);
363   VariantClear(&v);
364   V_VT(pvarProp) = VT_BSTR; V_BSTR(pvarProp) = SysAllocString(property->psz);
365   hr = IDxDiagContainerImpl_AddProp(pDeviceSubCont, szDeviceID, &v);
366   VariantClear(&v);
367
368   hr = IDxDiagContainerImpl_AddChildContainer(pSubCont, "", pDeviceSubCont);
369   */
370
371   /*
372    * Drivers Cont contains Files Desc Containers
373    */
374   /*
375   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, &pDriversCont);
376   if (FAILED(hr)) { return hr; }
377   hr = IDxDiagContainerImpl_AddChildContainer(pDeviceSubCont, szDrivers, pDriversCont);
378
379   */
380   return hr;
381 }
382
383 static HRESULT DXDiag_InitDXDiagLogicalDisksContainer(IDxDiagContainer* pSubCont) {
384   HRESULT hr = S_OK;
385   /*
386   static const WCHAR szDriveLetter[] = {'s','z','D','r','i','v','e','L','e','t','t','e','r',0};
387   static const WCHAR szFreeSpace[] = {'s','z','F','r','e','e','S','p','a','c','e',0};
388   static const WCHAR szMaxSpace[] = {'s','z','M','a','x','S','p','a','c','e',0};
389   static const WCHAR szFileSystem[] = {'s','z','F','i','l','e','S','y','s','t','e','m',0};
390   static const WCHAR szModel[] = {'s','z','M','o','d','e','l',0};
391   static const WCHAR szPNPDeviceID[] = {'s','z','P','N','P','D','e','v','i','c','e','I','D',0};
392   static const WCHAR dwHardDriveIndex[] = {'d','w','H','a','r','d','D','r','i','v','e','I','n','d','e','x',0};
393
394   static const WCHAR szDrivers[] = {'s','z','D','r','i','v','e','r','s',0};
395  
396   VARIANT v;
397   IDxDiagContainer* pDiskSubCont = NULL;
398   IDxDiagContainer* pDriversCont = NULL;
399
400   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, &pDiskSubCont);
401   if (FAILED(hr)) { return hr; }
402   hr = IDxDiagContainerImpl_AddChildContainer(pSubCont, "" , pDiskSubCont);
403   */
404   
405   /*
406    * Drivers Cont contains Files Desc Containers
407    */
408   /*
409   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, &pDriversCont);
410   if (FAILED(hr)) { return hr; }
411   hr = IDxDiagContainerImpl_AddChildContainer(pDeviceSubCont, szDrivers, pDriversCont);
412   */
413   return hr;
414 }
415
416 static HRESULT DXDiag_InitDXDiagDirectXFilesContainer(IDxDiagContainer* pSubCont)
417 {
418     HRESULT hr = S_OK;
419     static const WCHAR dlls[][15] =
420     {
421         {'d','3','d','8','.','d','l','l',0},
422         {'d','3','d','9','.','d','l','l',0},
423         {'d','d','r','a','w','.','d','l','l',0},
424         {'d','e','v','e','n','u','m','.','d','l','l',0},
425         {'d','i','n','p','u','t','8','.','d','l','l',0},
426         {'d','i','n','p','u','t','.','d','l','l',0},
427         {'d','m','b','a','n','d','.','d','l','l',0},
428         {'d','m','c','o','m','p','o','s','.','d','l','l',0},
429         {'d','m','i','m','e','.','d','l','l',0},
430         {'d','m','l','o','a','d','e','r','.','d','l','l',0},
431         {'d','m','s','c','r','i','p','t','.','d','l','l',0},
432         {'d','m','s','t','y','l','e','.','d','l','l',0},
433         {'d','m','s','y','n','t','h','.','d','l','l',0},
434         {'d','m','u','s','i','c','.','d','l','l',0},
435         {'d','p','l','a','y','x','.','d','l','l',0},
436         {'d','p','n','e','t','.','d','l','l',0},
437         {'d','s','o','u','n','d','.','d','l','l',0},
438         {'d','s','w','a','v','e','.','d','l','l',0},
439         {'d','x','d','i','a','g','n','.','d','l','l',0},
440         {'q','u','a','r','t','z','.','d','l','l',0}
441     };
442     WCHAR szFilePath[MAX_PATH];
443     INT i;
444
445     GetSystemDirectoryW(szFilePath, MAX_PATH);
446
447     for (i = 0; i < sizeof(dlls) / sizeof(dlls[0]); i++)
448     {
449         static const WCHAR szFormat[] = {'%','d',0};
450         WCHAR szFileID[5];
451         IDxDiagContainer *pDXFileSubCont;
452
453         snprintfW(szFileID, sizeof(szFileID)/sizeof(szFileID[0]), szFormat, i);
454
455         hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pDXFileSubCont);
456         if (FAILED(hr)) continue;
457
458         if (FAILED(DXDiag_AddFileDescContainer(pDXFileSubCont, szFilePath, dlls[i])) ||
459             FAILED(IDxDiagContainerImpl_AddChildContainer(pSubCont, szFileID, pDXFileSubCont)))
460         {
461             IUnknown_Release(pDXFileSubCont);
462             continue;
463         }
464     }
465     return hr;
466 }
467
468 static HRESULT DXDiag_InitDXDiagDisplayContainer(IDxDiagContainer* pSubCont)
469 {
470     static const WCHAR szDescription[] = {'s','z','D','e','s','c','r','i','p','t','i','o','n',0};
471     static const WCHAR szDeviceName[] = {'s','z','D','e','v','i','c','e','N','a','m','e',0};
472     static const WCHAR szKeyDeviceID[] = {'s','z','K','e','y','D','e','v','i','c','e','I','D',0};
473     static const WCHAR szKeyDeviceKey[] = {'s','z','K','e','y','D','e','v','i','c','e','K','e','y',0};
474     static const WCHAR szVendorId[] = {'s','z','V','e','n','d','o','r','I','d',0};
475     static const WCHAR szDeviceId[] = {'s','z','D','e','v','i','c','e','I','d',0};
476     static const WCHAR szDeviceIdentifier[] = {'s','z','D','e','v','i','c','e','I','d','e','n','t','i','f','i','e','r',0};
477     static const WCHAR dwWidth[] = {'d','w','W','i','d','t','h',0};
478     static const WCHAR dwHeight[] = {'d','w','H','e','i','g','h','t',0};
479     static const WCHAR dwBpp[] = {'d','w','B','p','p',0};
480     static const WCHAR szDisplayMemoryLocalized[] = {'s','z','D','i','s','p','l','a','y','M','e','m','o','r','y','L','o','c','a','l','i','z','e','d',0};
481     static const WCHAR szDisplayMemoryEnglish[] = {'s','z','D','i','s','p','l','a','y','M','e','m','o','r','y','E','n','g','l','i','s','h',0};
482
483     static const WCHAR szAdapterID[] = {'0',0};
484     static const WCHAR szEmpty[] = {0};
485
486     HRESULT                 hr;
487     IDxDiagContainer       *pDisplayAdapterSubCont = NULL;
488
489     IDirectDraw7           *pDirectDraw;
490     DDSCAPS2                dd_caps;
491     DISPLAY_DEVICEW         disp_dev;
492     DDSURFACEDESC2          surface_descr;
493     DWORD                   tmp;
494     WCHAR                   buffer[256];
495
496     hr = DXDiag_CreateDXDiagContainer( &IID_IDxDiagContainer, (void**) &pDisplayAdapterSubCont );
497     if (FAILED( hr )) return hr;
498     hr = IDxDiagContainerImpl_AddChildContainer( pSubCont, szAdapterID, pDisplayAdapterSubCont );
499     if (FAILED( hr )) return hr;
500
501     disp_dev.cb = sizeof(disp_dev);
502     if (EnumDisplayDevicesW( NULL, 0, &disp_dev, 0 ))
503     {
504         add_prop_str( pDisplayAdapterSubCont, szDeviceName, disp_dev.DeviceName );
505         add_prop_str( pDisplayAdapterSubCont, szDescription, disp_dev.DeviceString );
506     }
507
508     hr = DirectDrawCreateEx( NULL, (LPVOID *)&pDirectDraw, &IID_IDirectDraw7, NULL);
509     if (FAILED( hr )) return hr;
510
511     dd_caps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
512     dd_caps.dwCaps2 = dd_caps.dwCaps3 = dd_caps.dwCaps4 = 0;
513     hr = IDirectDraw7_GetAvailableVidMem( pDirectDraw, &dd_caps, &tmp, NULL );
514     if (SUCCEEDED(hr))
515     {
516         static const WCHAR mem_fmt[] = {'%','.','1','f',' ','M','B',0};
517
518         snprintfW( buffer, sizeof(buffer)/sizeof(buffer[0]), mem_fmt, ((float)tmp) / 1000000.0 );
519         add_prop_str( pDisplayAdapterSubCont, szDisplayMemoryLocalized, buffer );
520         add_prop_str( pDisplayAdapterSubCont, szDisplayMemoryEnglish, buffer );
521     }
522
523     surface_descr.dwSize = sizeof(surface_descr);
524     hr = IDirectDraw7_GetDisplayMode( pDirectDraw, &surface_descr );
525     if (SUCCEEDED(hr))
526     {
527         if (surface_descr.dwFlags & DDSD_WIDTH)
528             add_prop_ui4( pDisplayAdapterSubCont, dwWidth, surface_descr.dwWidth );
529         if (surface_descr.dwFlags & DDSD_HEIGHT)
530             add_prop_ui4( pDisplayAdapterSubCont, dwHeight, surface_descr.dwHeight );
531         if (surface_descr.dwFlags & DDSD_PIXELFORMAT)
532             add_prop_ui4( pDisplayAdapterSubCont, dwBpp, surface_descr.u4.ddpfPixelFormat.u1.dwRGBBitCount );
533     }
534
535     get_display_device_id( buffer );
536     add_prop_str( pDisplayAdapterSubCont, szDeviceIdentifier, buffer );
537
538     add_prop_str( pDisplayAdapterSubCont, szVendorId, szEmpty );
539     add_prop_str( pDisplayAdapterSubCont, szDeviceId, szEmpty );
540     add_prop_str( pDisplayAdapterSubCont, szKeyDeviceKey, szEmpty );
541     add_prop_str( pDisplayAdapterSubCont, szKeyDeviceID, szEmpty );
542
543     IUnknown_Release( pDirectDraw );
544     return hr;
545 }
546
547 static HRESULT DXDiag_InitDXDiagDirectSoundContainer(IDxDiagContainer* pSubCont) {
548   HRESULT hr = S_OK;
549   static const WCHAR DxDiag_SoundDevices[] = {'D','x','D','i','a','g','_','S','o','u','n','d','D','e','v','i','c','e','s',0};
550   static const WCHAR DxDiag_SoundCaptureDevices[] = {'D','x','D','i','a','g','_','S','o','u','n','d','C','a','p','t','u','r','e','D','e','v','i','c','e','s',0};
551   IDxDiagContainer* pSubSubCont = NULL;
552
553   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubSubCont);
554   if (FAILED(hr)) { return hr; }
555   hr = IDxDiagContainerImpl_AddChildContainer(pSubCont, DxDiag_SoundDevices, pSubSubCont);
556
557   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubSubCont);
558   if (FAILED(hr)) { return hr; }
559   hr = IDxDiagContainerImpl_AddChildContainer(pSubCont, DxDiag_SoundCaptureDevices, pSubSubCont);
560
561   return hr;
562 }
563
564 static HRESULT DXDiag_InitDXDiagDirectMusicContainer(IDxDiagContainer* pSubCont) {
565   HRESULT hr = S_OK;
566   return hr;
567 }
568
569 static HRESULT DXDiag_InitDXDiagDirectInputContainer(IDxDiagContainer* pSubCont) {
570   HRESULT hr = S_OK;
571   return hr;
572 }
573
574 static HRESULT DXDiag_InitDXDiagDirectPlayContainer(IDxDiagContainer* pSubCont) {
575   HRESULT hr = S_OK;
576   return hr;
577 }
578
579 static HRESULT DXDiag_InitDXDiagDirectShowFiltersContainer(IDxDiagContainer* pSubCont) {
580   HRESULT hr = S_OK;
581   static const WCHAR szName[] = {'s','z','N','a','m','e',0};
582   static const WCHAR szVersionW[] = {'s','z','V','e','r','s','i','o','n',0};
583   static const WCHAR szCatName[] = {'s','z','C','a','t','N','a','m','e',0};
584   static const WCHAR ClsidCatW[] = {'C','l','s','i','d','C','a','t',0};
585   static const WCHAR ClsidFilterW[] = {'C','l','s','i','d','F','i','l','t','e','r',0};
586   static const WCHAR dwInputs[] = {'d','w','I','n','p','u','t','s',0};
587   static const WCHAR dwOutputs[] = {'d','w','O','u','t','p','u','t','s',0};
588   static const WCHAR dwMeritW[] = {'d','w','M','e','r','i','t',0};
589   /*
590   static const WCHAR szFileName[] = {'s','z','F','i','l','e','N','a','m','e',0};
591   static const WCHAR szFileVersion[] = {'s','z','F','i','l','e','V','e','r','s','i','o','n',0};
592   */
593   VARIANT v;
594
595   static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
596   static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};  
597   static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
598
599   static const WCHAR szVersionFormat[] = {'v','%','d',0};
600   static const WCHAR szIdFormat[] = {'%','d',0};
601   int i = 0;
602
603
604   ICreateDevEnum* pCreateDevEnum = NULL;
605   IEnumMoniker* pEmCat = NULL;
606   IMoniker* pMCat = NULL;
607   /** */
608   hr = CoCreateInstance(&CLSID_SystemDeviceEnum, 
609                         NULL, 
610                         CLSCTX_INPROC_SERVER,
611                         &IID_ICreateDevEnum, 
612                         (void**) &pCreateDevEnum);
613   if (FAILED(hr)) return hr; 
614   
615   hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEmCat, 0);
616   if (FAILED(hr)) goto out_show_filters; 
617
618   VariantInit(&v);
619
620   while (S_OK == IEnumMoniker_Next(pEmCat, 1, &pMCat, NULL)) {
621     IPropertyBag* pPropBag = NULL;
622     CLSID clsidCat; 
623     hr = IMoniker_BindToStorage(pMCat, NULL, NULL, &IID_IPropertyBag, (void**) &pPropBag);
624     if (SUCCEEDED(hr)) {
625       WCHAR* wszCatName = NULL;
626       WCHAR* wszCatClsid = NULL;
627
628       hr = IPropertyBag_Read(pPropBag, wszFriendlyName, &v, 0);
629       wszCatName = SysAllocString(V_BSTR(&v));
630       VariantClear(&v);
631
632       hr = IPropertyBag_Read(pPropBag, wszClsidName, &v, 0);
633       wszCatClsid = SysAllocString(V_BSTR(&v));
634       hr = CLSIDFromString(V_UNION(&v, bstrVal), &clsidCat);
635       VariantClear(&v);
636
637       /*
638       hr = IPropertyBag_Read(pPropBag, wszMeritName, &v, 0);
639       hr = IDxDiagContainerImpl_AddProp(pSubCont, dwMerit, &v);
640       VariantClear(&v);
641       */
642
643       if (SUCCEEDED(hr)) {
644         IEnumMoniker* pEnum = NULL;
645         IMoniker* pMoniker = NULL;
646         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);        
647         TRACE("\tClassEnumerator for clsid(%s) pEnum(%p)\n", debugstr_guid(&clsidCat), pEnum);
648         if (FAILED(hr) || pEnum == NULL) {
649           goto class_enum_failed;
650         }
651         while (NULL != pEnum && S_OK == IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL)) {          
652           IPropertyBag* pPropFilterBag = NULL;
653           TRACE("\tIEnumMoniker_Next(%p, 1, %p)\n", pEnum, pMoniker);
654           hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (void**) &pPropFilterBag);
655           if (SUCCEEDED(hr)) {
656             LPBYTE pData = NULL;
657             DWORD dwNOutputs = 0;
658             DWORD dwNInputs = 0;
659             DWORD dwMerit = 0;
660             WCHAR bufferW[10];
661             IDxDiagContainer *pDShowSubCont = NULL;
662             IFilterMapper2 *pFileMapper = NULL;
663             IAMFilterData *pFilterData = NULL;
664
665             snprintfW(bufferW, sizeof(bufferW)/sizeof(bufferW[0]), szIdFormat, i);
666             if (FAILED(DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pDShowSubCont)) ||
667                 FAILED(IDxDiagContainerImpl_AddChildContainer(pSubCont, bufferW, pDShowSubCont)))
668             {
669               IPropertyBag_Release(pPropFilterBag);
670               if (pDShowSubCont) IUnknown_Release(pDShowSubCont);
671               continue;
672             }
673
674             bufferW[0] = 0;
675             hr = IPropertyBag_Read(pPropFilterBag, wszFriendlyName, &v, 0);
676             hr = IDxDiagContainerImpl_AddProp(pDShowSubCont, szName, &v);
677             TRACE("\tName:%s\n", debugstr_w(V_BSTR(&v)));
678             VariantClear(&v);
679
680             hr = IPropertyBag_Read(pPropFilterBag, wszClsidName, &v, 0);
681             TRACE("\tClsid:%s\n", debugstr_w(V_BSTR(&v)));
682             hr = IDxDiagContainerImpl_AddProp(pDShowSubCont, ClsidFilterW, &v);
683             VariantClear(&v);
684
685             hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC, &IID_IFilterMapper2,
686                                   (LPVOID*)&pFileMapper);
687             if (SUCCEEDED(hr) &&
688                 SUCCEEDED(IFilterMapper2_QueryInterface(pFileMapper, &IID_IAMFilterData, (void **)&pFilterData)))
689             {
690               DWORD array_size;
691               BYTE *tmp;
692               REGFILTER2 *pRF = NULL;
693
694               if (SUCCEEDED(IPropertyBag_Read(pPropFilterBag, wszFilterDataName, &v, NULL)) &&
695                   SUCCEEDED(SafeArrayAccessData(V_UNION(&v, parray), (LPVOID*) &pData)))
696               {
697                 ULONG j;
698                 array_size = V_UNION(&v, parray)->rgsabound->cElements;
699
700                 if (SUCCEEDED(IAMFilterData_ParseFilterData(pFilterData, pData, array_size, &tmp)))
701                 {
702                   pRF = ((REGFILTER2 **)tmp)[0];
703
704                   snprintfW(bufferW, sizeof(bufferW)/sizeof(bufferW[0]), szVersionFormat, pRF->dwVersion);
705                   if (pRF->dwVersion == 1)
706                   {
707                     for (j = 0; j < pRF->u.s.cPins; j++)
708                       if (pRF->u.s.rgPins[j].bOutput)
709                         dwNOutputs++;
710                       else
711                         dwNInputs++;
712                   }
713                   else if (pRF->dwVersion == 2)
714                   {
715                     for (j = 0; j < pRF->u.s1.cPins2; j++)
716                       if (pRF->u.s1.rgPins2[j].dwFlags & REG_PINFLAG_B_OUTPUT)
717                         dwNOutputs++;
718                       else
719                         dwNInputs++;
720                   }
721
722                   dwMerit = pRF->dwMerit;
723                   CoTaskMemFree(tmp);
724                 }
725
726                 SafeArrayUnaccessData(V_UNION(&v, parray));
727                 VariantClear(&v);
728               }
729               IFilterMapper2_Release(pFilterData);
730             }
731             if (pFileMapper) IFilterMapper2_Release(pFileMapper);
732
733             add_prop_str(pDShowSubCont, szVersionW, bufferW);
734             add_prop_str(pDShowSubCont, szCatName, wszCatName);
735             add_prop_str(pDShowSubCont, ClsidCatW, wszCatClsid);
736             add_prop_ui4(pDShowSubCont, dwInputs,  dwNInputs);
737             add_prop_ui4(pDShowSubCont, dwOutputs, dwNOutputs);
738             add_prop_ui4(pDShowSubCont, dwMeritW, dwMerit);
739
740             i++;
741           }
742           IPropertyBag_Release(pPropFilterBag); pPropFilterBag = NULL;
743         }
744         IEnumMoniker_Release(pEnum); pEnum = NULL;
745       }
746 class_enum_failed:      
747       SysFreeString(wszCatName);
748       SysFreeString(wszCatClsid);
749       IPropertyBag_Release(pPropBag); pPropBag = NULL;
750     }
751     IEnumMoniker_Release(pMCat); pMCat = NULL;
752   }
753
754 out_show_filters:
755   if (NULL != pEmCat) { IEnumMoniker_Release(pEmCat); pEmCat = NULL; }
756   if (NULL != pCreateDevEnum) { ICreateDevEnum_Release(pCreateDevEnum); pCreateDevEnum = NULL; }
757   return hr;
758 }
759
760 static HRESULT DXDiag_InitRootDXDiagContainer(IDxDiagContainer* pRootCont) {
761   HRESULT hr = S_OK;
762   static const WCHAR DxDiag_SystemInfo[] = {'D','x','D','i','a','g','_','S','y','s','t','e','m','I','n','f','o',0};
763   static const WCHAR DxDiag_SystemDevices[] = {'D','x','D','i','a','g','_','S','y','s','t','e','m','D','e','v','i','c','e','s',0};
764   static const WCHAR DxDiag_LogicalDisks[] = {'D','x','D','i','a','g','_','L','o','g','i','c','a','l','D','i','s','k','s',0};
765   static const WCHAR DxDiag_DirectXFiles[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','X','F','i','l','e','s',0};
766   static const WCHAR DxDiag_DisplayDevices[] = {'D','x','D','i','a','g','_','D','i','s','p','l','a','y','D','e','v','i','c','e','s',0};
767   static const WCHAR DxDiag_DirectSound[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','S','o','u','n','d',0};
768   static const WCHAR DxDiag_DirectMusic[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','M','u','s','i','c',0};
769   static const WCHAR DxDiag_DirectInput[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','I','n','p','u','t',0};
770   static const WCHAR DxDiag_DirectPlay[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','P','l','a','y',0};
771   static const WCHAR DxDiag_DirectShowFilters[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','S','h','o','w','F','i','l','t','e','r','s',0};
772   IDxDiagContainer* pSubCont = NULL;
773   
774   TRACE("(%p)\n", pRootCont);
775
776   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
777   if (FAILED(hr)) { return hr; }
778   hr = DXDiag_InitDXDiagSystemInfoContainer(pSubCont);
779   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_SystemInfo, pSubCont);
780
781   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
782   if (FAILED(hr)) { return hr; }
783   hr = DXDiag_InitDXDiagSystemDevicesContainer(pSubCont);
784   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_SystemDevices, pSubCont);
785
786   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
787   if (FAILED(hr)) { return hr; }
788   hr = DXDiag_InitDXDiagLogicalDisksContainer(pSubCont);
789   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_LogicalDisks, pSubCont);
790
791   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
792   if (FAILED(hr)) { return hr; }
793   hr = DXDiag_InitDXDiagDirectXFilesContainer(pSubCont);
794   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DirectXFiles, pSubCont);
795
796   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
797   if (FAILED(hr)) { return hr; }
798   hr = DXDiag_InitDXDiagDisplayContainer(pSubCont);
799   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DisplayDevices, pSubCont);
800
801   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
802   if (FAILED(hr)) { return hr; }
803   hr = DXDiag_InitDXDiagDirectSoundContainer(pSubCont);
804   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DirectSound, pSubCont);
805
806   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
807   if (FAILED(hr)) { return hr; }
808   hr = DXDiag_InitDXDiagDirectMusicContainer(pSubCont);
809   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DirectMusic, pSubCont);
810
811   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
812   if (FAILED(hr)) { return hr; }
813   hr = DXDiag_InitDXDiagDirectInputContainer(pSubCont);
814   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DirectInput, pSubCont);
815
816   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
817   if (FAILED(hr)) { return hr; }
818   hr = DXDiag_InitDXDiagDirectPlayContainer(pSubCont);
819   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DirectPlay, pSubCont);
820
821   hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, (void**) &pSubCont);
822   if (FAILED(hr)) { return hr; }
823   hr = DXDiag_InitDXDiagDirectShowFiltersContainer(pSubCont);
824   hr = IDxDiagContainerImpl_AddChildContainer(pRootCont, DxDiag_DirectShowFilters, pSubCont);
825
826   return hr;
827 }