msxml3: Remove get_ownerDocument() forward.
[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 build_information_tree(IDxDiagContainerImpl_Container **pinfo_root);
45 static void free_information_tree(IDxDiagContainerImpl_Container *node);
46
47 /* IDxDiagProvider IUnknown parts follow: */
48 static HRESULT WINAPI IDxDiagProviderImpl_QueryInterface(PDXDIAGPROVIDER iface, REFIID riid, LPVOID *ppobj)
49 {
50     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
51
52     if (!ppobj) return E_INVALIDARG;
53
54     if (IsEqualGUID(riid, &IID_IUnknown)
55         || IsEqualGUID(riid, &IID_IDxDiagProvider)) {
56         IUnknown_AddRef(iface);
57         *ppobj = This;
58         return S_OK;
59     }
60
61     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
62     *ppobj = NULL;
63     return E_NOINTERFACE;
64 }
65
66 static ULONG WINAPI IDxDiagProviderImpl_AddRef(PDXDIAGPROVIDER iface) {
67     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
68     ULONG refCount = InterlockedIncrement(&This->ref);
69
70     TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
71
72     DXDIAGN_LockModule();
73
74     return refCount;
75 }
76
77 static ULONG WINAPI IDxDiagProviderImpl_Release(PDXDIAGPROVIDER iface) {
78     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
79     ULONG refCount = InterlockedDecrement(&This->ref);
80
81     TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
82
83     if (!refCount) {
84         free_information_tree(This->info_root);
85         HeapFree(GetProcessHeap(), 0, This);
86     }
87
88     DXDIAGN_UnlockModule();
89     
90     return refCount;
91 }
92
93 /* IDxDiagProvider Interface follow: */
94 static HRESULT WINAPI IDxDiagProviderImpl_Initialize(PDXDIAGPROVIDER iface, DXDIAG_INIT_PARAMS* pParams) {
95     IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
96     HRESULT hr;
97
98     TRACE("(%p,%p)\n", iface, pParams);
99
100     if (NULL == pParams) {
101       return E_POINTER;
102     }
103     if (pParams->dwSize != sizeof(DXDIAG_INIT_PARAMS) ||
104         pParams->dwDxDiagHeaderVersion != DXDIAG_DX9_SDK_VERSION) {
105       return E_INVALIDARG;
106     }
107
108     if (!This->info_root)
109     {
110         hr = build_information_tree(&This->info_root);
111         if (FAILED(hr))
112             return hr;
113     }
114
115     This->init = TRUE;
116     memcpy(&This->params, pParams, pParams->dwSize);
117     return S_OK;
118 }
119
120 static HRESULT WINAPI IDxDiagProviderImpl_GetRootContainer(PDXDIAGPROVIDER iface, IDxDiagContainer** ppInstance) {
121   IDxDiagProviderImpl *This = (IDxDiagProviderImpl *)iface;
122
123   TRACE("(%p,%p)\n", iface, ppInstance);
124
125   if (FALSE == This->init) {
126     return CO_E_NOTINITIALIZED;
127   }
128
129   return DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, This->info_root,
130                                       (IDxDiagProvider *)This, (void **)ppInstance);
131 }
132
133 static const IDxDiagProviderVtbl DxDiagProvider_Vtbl =
134 {
135     IDxDiagProviderImpl_QueryInterface,
136     IDxDiagProviderImpl_AddRef,
137     IDxDiagProviderImpl_Release,
138     IDxDiagProviderImpl_Initialize,
139     IDxDiagProviderImpl_GetRootContainer
140 };
141
142 HRESULT DXDiag_CreateDXDiagProvider(LPCLASSFACTORY iface, LPUNKNOWN punkOuter, REFIID riid, LPVOID *ppobj) {
143   IDxDiagProviderImpl* provider;
144
145   TRACE("(%p, %s, %p)\n", punkOuter, debugstr_guid(riid), ppobj);
146
147   *ppobj = NULL;
148   if (punkOuter) return CLASS_E_NOAGGREGATION;
149
150   provider = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagProviderImpl));
151   if (NULL == provider) return E_OUTOFMEMORY;
152   provider->lpVtbl = &DxDiagProvider_Vtbl;
153   provider->ref = 0; /* will be inited with QueryInterface */
154   return IDxDiagProviderImpl_QueryInterface ((PDXDIAGPROVIDER)provider, riid, ppobj);
155 }
156
157 static void get_display_device_id(WCHAR *szIdentifierBuffer)
158 {
159     static const WCHAR szNA[] = {'n','/','a',0};
160
161     HRESULT hr = E_FAIL;
162
163     HMODULE                 d3d9_handle;
164     IDirect3D9             *(WINAPI *pDirect3DCreate9)(UINT) = NULL;
165     IDirect3D9             *pD3d = NULL;
166     D3DADAPTER_IDENTIFIER9  adapter_ident;
167
168     /* Retrieves the display device identifier from the d3d9 implementation. */
169     d3d9_handle = LoadLibraryA("d3d9.dll");
170     if(d3d9_handle)
171         pDirect3DCreate9 = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
172     if(pDirect3DCreate9)
173         pD3d = pDirect3DCreate9(D3D_SDK_VERSION);
174     if(pD3d)
175         hr = IDirect3D9_GetAdapterIdentifier(pD3d, D3DADAPTER_DEFAULT, 0, &adapter_ident);
176     if(SUCCEEDED(hr)) {
177         StringFromGUID2(&adapter_ident.DeviceIdentifier, szIdentifierBuffer, 39);
178     } else {
179         memcpy(szIdentifierBuffer, szNA, sizeof(szNA));
180     }
181
182     if (pD3d)
183         IDirect3D9_Release(pD3d);
184     if (d3d9_handle)
185         FreeLibrary(d3d9_handle);
186 }
187
188 static void free_property_information(IDxDiagContainerImpl_Property *prop)
189 {
190     VariantClear(&prop->vProp);
191     HeapFree(GetProcessHeap(), 0, prop->propName);
192     HeapFree(GetProcessHeap(), 0, prop);
193 }
194
195 static void free_information_tree(IDxDiagContainerImpl_Container *node)
196 {
197     IDxDiagContainerImpl_Container *ptr, *cursor2;
198
199     if (!node)
200         return;
201
202     HeapFree(GetProcessHeap(), 0, node->contName);
203
204     LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &node->subContainers, IDxDiagContainerImpl_Container, entry)
205     {
206         IDxDiagContainerImpl_Property *prop, *prop_cursor2;
207
208         LIST_FOR_EACH_ENTRY_SAFE(prop, prop_cursor2, &ptr->properties, IDxDiagContainerImpl_Property, entry)
209         {
210             list_remove(&prop->entry);
211             free_property_information(prop);
212         }
213
214         list_remove(&ptr->entry);
215         free_information_tree(ptr);
216     }
217
218     HeapFree(GetProcessHeap(), 0, node);
219 }
220
221 static IDxDiagContainerImpl_Container *allocate_information_node(const WCHAR *name)
222 {
223     IDxDiagContainerImpl_Container *ret;
224
225     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
226     if (!ret)
227         return NULL;
228
229     if (name)
230     {
231         ret->contName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1) * sizeof(*name));
232         if (!ret->contName)
233         {
234             HeapFree(GetProcessHeap(), 0, ret);
235             return NULL;
236         }
237         strcpyW(ret->contName, name);
238     }
239
240     list_init(&ret->subContainers);
241     list_init(&ret->properties);
242
243     return ret;
244 }
245
246 static IDxDiagContainerImpl_Property *allocate_property_information(const WCHAR *name)
247 {
248     IDxDiagContainerImpl_Property *ret;
249
250     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
251     if (!ret)
252         return NULL;
253
254     ret->propName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1) * sizeof(*name));
255     if (!ret->propName)
256     {
257         HeapFree(GetProcessHeap(), 0, ret);
258         return NULL;
259     }
260     strcpyW(ret->propName, name);
261
262     return ret;
263 }
264
265 static inline void add_subcontainer(IDxDiagContainerImpl_Container *node, IDxDiagContainerImpl_Container *subCont)
266 {
267     list_add_tail(&node->subContainers, &subCont->entry);
268     ++node->nSubContainers;
269 }
270
271 static inline HRESULT add_bstr_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, const WCHAR *str)
272 {
273     IDxDiagContainerImpl_Property *prop;
274     BSTR bstr;
275
276     prop = allocate_property_information(propName);
277     if (!prop)
278         return E_OUTOFMEMORY;
279
280     bstr = SysAllocString(str);
281     if (!bstr)
282     {
283         free_property_information(prop);
284         return E_OUTOFMEMORY;
285     }
286
287     V_VT(&prop->vProp) = VT_BSTR;
288     V_BSTR(&prop->vProp) = bstr;
289
290     list_add_tail(&node->properties, &prop->entry);
291     ++node->nProperties;
292
293     return S_OK;
294 }
295
296 static inline HRESULT add_ui4_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, DWORD data)
297 {
298     IDxDiagContainerImpl_Property *prop;
299
300     prop = allocate_property_information(propName);
301     if (!prop)
302         return E_OUTOFMEMORY;
303
304     V_VT(&prop->vProp) = VT_UI4;
305     V_UI4(&prop->vProp) = data;
306
307     list_add_tail(&node->properties, &prop->entry);
308     ++node->nProperties;
309
310     return S_OK;
311 }
312
313 static inline HRESULT add_bool_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, BOOL data)
314 {
315     IDxDiagContainerImpl_Property *prop;
316
317     prop = allocate_property_information(propName);
318     if (!prop)
319         return E_OUTOFMEMORY;
320
321     V_VT(&prop->vProp) = VT_BOOL;
322     V_BOOL(&prop->vProp) = data;
323
324     list_add_tail(&node->properties, &prop->entry);
325     ++node->nProperties;
326
327     return S_OK;
328 }
329
330 static inline HRESULT add_ull_as_bstr_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, ULONGLONG data )
331 {
332     IDxDiagContainerImpl_Property *prop;
333
334     prop = allocate_property_information(propName);
335     if (!prop)
336         return E_OUTOFMEMORY;
337
338     V_VT(&prop->vProp) = VT_UI8;
339     V_UI8(&prop->vProp) = data;
340
341     VariantChangeType(&prop->vProp, &prop->vProp, 0, VT_BSTR);
342
343     list_add_tail(&node->properties, &prop->entry);
344     ++node->nProperties;
345
346     return S_OK;
347 }
348
349 static HRESULT build_systeminfo_tree(IDxDiagContainerImpl_Container *node)
350 {
351     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};
352     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};
353     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};
354     static const WCHAR szDirectXVersionLetter_v[] = {'c',0};
355     static const WCHAR bDebug[] = {'b','D','e','b','u','g',0};
356     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};
357     static const WCHAR szDirectXVersionEnglish_v[] = {'4','.','0','9','.','0','0','0','0','.','0','9','0','4',0};
358     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};
359     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};
360     static const WCHAR ullPhysicalMemory[] = {'u','l','l','P','h','y','s','i','c','a','l','M','e','m','o','r','y',0};
361     static const WCHAR ullUsedPageFile[]   = {'u','l','l','U','s','e','d','P','a','g','e','F','i','l','e',0};
362     static const WCHAR ullAvailPageFile[]  = {'u','l','l','A','v','a','i','l','P','a','g','e','F','i','l','e',0};
363     static const WCHAR szWindowsDir[] = {'s','z','W','i','n','d','o','w','s','D','i','r',0};
364     static const WCHAR dwOSMajorVersion[] = {'d','w','O','S','M','a','j','o','r','V','e','r','s','i','o','n',0};
365     static const WCHAR dwOSMinorVersion[] = {'d','w','O','S','M','i','n','o','r','V','e','r','s','i','o','n',0};
366     static const WCHAR dwOSBuildNumber[] = {'d','w','O','S','B','u','i','l','d','N','u','m','b','e','r',0};
367     static const WCHAR dwOSPlatformID[] = {'d','w','O','S','P','l','a','t','f','o','r','m','I','D',0};
368     static const WCHAR szCSDVersion[] = {'s','z','C','S','D','V','e','r','s','i','o','n',0};
369
370     HRESULT hr;
371     MEMORYSTATUSEX msex;
372     OSVERSIONINFOW info;
373     WCHAR buffer[MAX_PATH];
374
375     hr = add_ui4_property(node, dwDirectXVersionMajor, 9);
376     if (FAILED(hr))
377         return hr;
378
379     hr = add_ui4_property(node, dwDirectXVersionMinor, 0);
380     if (FAILED(hr))
381         return hr;
382
383     hr = add_bstr_property(node, szDirectXVersionLetter, szDirectXVersionLetter_v);
384     if (FAILED(hr))
385         return hr;
386
387     hr = add_bstr_property(node, szDirectXVersionEnglish, szDirectXVersionEnglish_v);
388     if (FAILED(hr))
389         return hr;
390
391     hr = add_bstr_property(node, szDirectXVersionLongEnglish, szDirectXVersionLongEnglish_v);
392     if (FAILED(hr))
393         return hr;
394
395     hr = add_bool_property(node, bDebug, FALSE);
396     if (FAILED(hr))
397         return hr;
398
399     msex.dwLength = sizeof(msex);
400     GlobalMemoryStatusEx(&msex);
401
402     hr = add_ull_as_bstr_property(node, ullPhysicalMemory, msex.ullTotalPhys);
403     if (FAILED(hr))
404         return hr;
405
406     hr = add_ull_as_bstr_property(node, ullUsedPageFile, msex.ullTotalPageFile - msex.ullAvailPageFile);
407     if (FAILED(hr))
408         return hr;
409
410     hr = add_ull_as_bstr_property(node, ullAvailPageFile, msex.ullAvailPageFile);
411     if (FAILED(hr))
412         return hr;
413
414     info.dwOSVersionInfoSize = sizeof(info);
415     GetVersionExW(&info);
416
417     hr = add_ui4_property(node, dwOSMajorVersion, info.dwMajorVersion);
418     if (FAILED(hr))
419         return hr;
420
421     hr = add_ui4_property(node, dwOSMinorVersion, info.dwMinorVersion);
422     if (FAILED(hr))
423         return hr;
424
425     hr = add_ui4_property(node, dwOSBuildNumber, info.dwBuildNumber);
426     if (FAILED(hr))
427         return hr;
428
429     hr = add_ui4_property(node, dwOSPlatformID, info.dwPlatformId);
430     if (FAILED(hr))
431         return hr;
432
433     hr = add_bstr_property(node, szCSDVersion, info.szCSDVersion);
434     if (FAILED(hr))
435         return hr;
436
437     GetWindowsDirectoryW(buffer, MAX_PATH);
438
439     hr = add_bstr_property(node, szWindowsDir, buffer);
440     if (FAILED(hr))
441         return hr;
442
443     return S_OK;
444 }
445
446 static HRESULT build_displaydevices_tree(IDxDiagContainerImpl_Container *node)
447 {
448     static const WCHAR szDescription[] = {'s','z','D','e','s','c','r','i','p','t','i','o','n',0};
449     static const WCHAR szDeviceName[] = {'s','z','D','e','v','i','c','e','N','a','m','e',0};
450     static const WCHAR szKeyDeviceID[] = {'s','z','K','e','y','D','e','v','i','c','e','I','D',0};
451     static const WCHAR szKeyDeviceKey[] = {'s','z','K','e','y','D','e','v','i','c','e','K','e','y',0};
452     static const WCHAR szVendorId[] = {'s','z','V','e','n','d','o','r','I','d',0};
453     static const WCHAR szDeviceId[] = {'s','z','D','e','v','i','c','e','I','d',0};
454     static const WCHAR szDeviceIdentifier[] = {'s','z','D','e','v','i','c','e','I','d','e','n','t','i','f','i','e','r',0};
455     static const WCHAR dwWidth[] = {'d','w','W','i','d','t','h',0};
456     static const WCHAR dwHeight[] = {'d','w','H','e','i','g','h','t',0};
457     static const WCHAR dwBpp[] = {'d','w','B','p','p',0};
458     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};
459     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};
460
461     static const WCHAR szAdapterID[] = {'0',0};
462     static const WCHAR szEmpty[] = {0};
463
464     IDxDiagContainerImpl_Container *display_adapter;
465     HRESULT hr;
466     IDirectDraw7 *pDirectDraw;
467     DDSCAPS2 dd_caps;
468     DISPLAY_DEVICEW disp_dev;
469     DDSURFACEDESC2 surface_descr;
470     DWORD tmp;
471     WCHAR buffer[256];
472
473     display_adapter = allocate_information_node(szAdapterID);
474     if (!display_adapter)
475         return E_OUTOFMEMORY;
476
477     add_subcontainer(node, display_adapter);
478
479     disp_dev.cb = sizeof(disp_dev);
480     if (EnumDisplayDevicesW( NULL, 0, &disp_dev, 0 ))
481     {
482         hr = add_bstr_property(display_adapter, szDeviceName, disp_dev.DeviceName);
483         if (FAILED(hr))
484             return hr;
485
486         hr = add_bstr_property(display_adapter, szDescription, disp_dev.DeviceString);
487         if (FAILED(hr))
488             return hr;
489     }
490
491     /* For now, silently ignore a failure from DirectDrawCreateEx. */
492     hr = DirectDrawCreateEx(NULL, (LPVOID *)&pDirectDraw, &IID_IDirectDraw7, NULL);
493     if (FAILED(hr))
494         return S_OK;
495
496     dd_caps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
497     dd_caps.dwCaps2 = dd_caps.dwCaps3 = dd_caps.dwCaps4 = 0;
498     hr = IDirectDraw7_GetAvailableVidMem(pDirectDraw, &dd_caps, &tmp, NULL);
499     if (SUCCEEDED(hr))
500     {
501         static const WCHAR mem_fmt[] = {'%','.','1','f',' ','M','B',0};
502
503         snprintfW(buffer, sizeof(buffer)/sizeof(buffer[0]), mem_fmt, ((float)tmp) / 1000000.0);
504
505         hr = add_bstr_property(display_adapter, szDisplayMemoryLocalized, buffer);
506         if (FAILED(hr))
507             goto cleanup;
508
509         hr = add_bstr_property(display_adapter, szDisplayMemoryEnglish, buffer);
510         if (FAILED(hr))
511             goto cleanup;
512     }
513
514     surface_descr.dwSize = sizeof(surface_descr);
515     hr = IDirectDraw7_GetDisplayMode(pDirectDraw, &surface_descr);
516     if (SUCCEEDED(hr))
517     {
518         if (surface_descr.dwFlags & DDSD_WIDTH)
519         {
520             hr = add_ui4_property(display_adapter, dwWidth, surface_descr.dwWidth);
521             if (FAILED(hr))
522                 goto cleanup;
523         }
524
525         if (surface_descr.dwFlags & DDSD_HEIGHT)
526         {
527             hr = add_ui4_property(display_adapter, dwHeight, surface_descr.dwHeight);
528             if (FAILED(hr))
529                 goto cleanup;
530         }
531
532         if (surface_descr.dwFlags & DDSD_PIXELFORMAT)
533         {
534             hr = add_ui4_property(display_adapter, dwBpp, surface_descr.u4.ddpfPixelFormat.u1.dwRGBBitCount);
535             if (FAILED(hr))
536                 goto cleanup;
537         }
538     }
539
540     get_display_device_id(buffer);
541
542     hr = add_bstr_property(display_adapter, szDeviceIdentifier, buffer);
543     if (FAILED(hr))
544         goto cleanup;
545
546     hr = add_bstr_property(display_adapter, szVendorId, szEmpty);
547     if (FAILED(hr))
548         goto cleanup;
549
550     hr = add_bstr_property(display_adapter, szDeviceId, szEmpty);
551     if (FAILED(hr))
552         goto cleanup;
553
554     hr = add_bstr_property(display_adapter, szKeyDeviceKey, szEmpty);
555     if (FAILED(hr))
556         goto cleanup;
557
558     hr = add_bstr_property(display_adapter, szKeyDeviceID, szEmpty);
559     if (FAILED(hr))
560         goto cleanup;
561
562     hr = S_OK;
563 cleanup:
564     IDirectDraw7_Release(pDirectDraw);
565     return hr;
566 }
567
568 static HRESULT build_directsound_tree(IDxDiagContainerImpl_Container *node)
569 {
570     static const WCHAR DxDiag_SoundDevices[] = {'D','x','D','i','a','g','_','S','o','u','n','d','D','e','v','i','c','e','s',0};
571     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};
572
573     IDxDiagContainerImpl_Container *cont;
574
575     cont = allocate_information_node(DxDiag_SoundDevices);
576     if (!cont)
577         return E_OUTOFMEMORY;
578
579     add_subcontainer(node, cont);
580
581     cont = allocate_information_node(DxDiag_SoundCaptureDevices);
582     if (!cont)
583         return E_OUTOFMEMORY;
584
585     add_subcontainer(node, cont);
586
587     return S_OK;
588 }
589
590 static HRESULT build_directmusic_tree(IDxDiagContainerImpl_Container *node)
591 {
592     return S_OK;
593 }
594
595 static HRESULT build_directinput_tree(IDxDiagContainerImpl_Container *node)
596 {
597     return S_OK;
598 }
599
600 static HRESULT build_directplay_tree(IDxDiagContainerImpl_Container *node)
601 {
602     return S_OK;
603 }
604
605 static HRESULT build_systemdevices_tree(IDxDiagContainerImpl_Container *node)
606 {
607     return S_OK;
608 }
609
610 static HRESULT fill_file_description(IDxDiagContainerImpl_Container *node, const WCHAR *szFilePath, const WCHAR *szFileName)
611 {
612     static const WCHAR szSlashSep[] = {'\\',0};
613     static const WCHAR szPath[] = {'s','z','P','a','t','h',0};
614     static const WCHAR szName[] = {'s','z','N','a','m','e',0};
615     static const WCHAR szVersion[] = {'s','z','V','e','r','s','i','o','n',0};
616     static const WCHAR szAttributes[] = {'s','z','A','t','t','r','i','b','u','t','e','s',0};
617     static const WCHAR szLanguageEnglish[] = {'s','z','L','a','n','g','u','a','g','e','E','n','g','l','i','s','h',0};
618     static const WCHAR dwFileTimeHigh[] = {'d','w','F','i','l','e','T','i','m','e','H','i','g','h',0};
619     static const WCHAR dwFileTimeLow[] = {'d','w','F','i','l','e','T','i','m','e','L','o','w',0};
620     static const WCHAR bBeta[] = {'b','B','e','t','a',0};
621     static const WCHAR bDebug[] = {'b','D','e','b','u','g',0};
622     static const WCHAR bExists[] = {'b','E','x','i','s','t','s',0};
623
624     /* Values */
625     static const WCHAR szFinal_Retail_v[] = {'F','i','n','a','l',' ','R','e','t','a','i','l',0};
626     static const WCHAR szEnglish_v[] = {'E','n','g','l','i','s','h',0};
627     static const WCHAR szVersionFormat[] = {'%','u','.','%','0','2','u','.','%','0','4','u','.','%','0','4','u',0};
628
629     HRESULT hr;
630     WCHAR *szFile;
631     WCHAR szVersion_v[1024];
632     DWORD retval, hdl;
633     void *pVersionInfo = NULL;
634     BOOL boolret = FALSE;
635     UINT uiLength;
636     VS_FIXEDFILEINFO *pFileInfo;
637
638     TRACE("Filling container %p for %s in %s\n", node,
639           debugstr_w(szFileName), debugstr_w(szFilePath));
640
641     szFile = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(szFilePath) +
642                                             lstrlenW(szFileName) + 2 /* slash + terminator */));
643     if (!szFile)
644         return E_OUTOFMEMORY;
645
646     lstrcpyW(szFile, szFilePath);
647     lstrcatW(szFile, szSlashSep);
648     lstrcatW(szFile, szFileName);
649
650     retval = GetFileVersionInfoSizeW(szFile, &hdl);
651     if (retval)
652     {
653         pVersionInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
654         if (!pVersionInfo)
655         {
656             hr = E_OUTOFMEMORY;
657             goto cleanup;
658         }
659
660         if (GetFileVersionInfoW(szFile, 0, retval, pVersionInfo) &&
661             VerQueryValueW(pVersionInfo, szSlashSep, (void **)&pFileInfo, &uiLength))
662             boolret = TRUE;
663     }
664
665     hr = add_bstr_property(node, szPath, szFile);
666     if (FAILED(hr))
667         goto cleanup;
668
669     hr = add_bstr_property(node, szName, szFileName);
670     if (FAILED(hr))
671         goto cleanup;
672
673     hr = add_bool_property(node, bExists, boolret);
674     if (FAILED(hr))
675         goto cleanup;
676
677     if (boolret)
678     {
679         snprintfW(szVersion_v, sizeof(szVersion_v)/sizeof(szVersion_v[0]),
680                   szVersionFormat,
681                   HIWORD(pFileInfo->dwFileVersionMS),
682                   LOWORD(pFileInfo->dwFileVersionMS),
683                   HIWORD(pFileInfo->dwFileVersionLS),
684                   LOWORD(pFileInfo->dwFileVersionLS));
685
686         TRACE("Found version as (%s)\n", debugstr_w(szVersion_v));
687
688         hr = add_bstr_property(node, szVersion, szVersion_v);
689         if (FAILED(hr))
690             goto cleanup;
691
692         hr = add_bstr_property(node, szAttributes, szFinal_Retail_v);
693         if (FAILED(hr))
694             goto cleanup;
695
696         hr = add_bstr_property(node, szLanguageEnglish, szEnglish_v);
697         if (FAILED(hr))
698             goto cleanup;
699
700         hr = add_ui4_property(node, dwFileTimeHigh, pFileInfo->dwFileDateMS);
701         if (FAILED(hr))
702             goto cleanup;
703
704         hr = add_ui4_property(node, dwFileTimeLow, pFileInfo->dwFileDateLS);
705         if (FAILED(hr))
706             goto cleanup;
707
708         hr = add_bool_property(node, bBeta, ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_PRERELEASE) != 0);
709         if (FAILED(hr))
710             goto cleanup;
711
712         hr = add_bool_property(node, bDebug, ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_DEBUG) != 0);
713         if (FAILED(hr))
714             goto cleanup;
715     }
716
717     hr = S_OK;
718 cleanup:
719     HeapFree(GetProcessHeap(), 0, pVersionInfo);
720     HeapFree(GetProcessHeap(), 0, szFile);
721
722     return hr;
723 }
724 static HRESULT build_directxfiles_tree(IDxDiagContainerImpl_Container *node)
725 {
726     static const WCHAR dlls[][15] =
727     {
728         {'d','3','d','8','.','d','l','l',0},
729         {'d','3','d','9','.','d','l','l',0},
730         {'d','d','r','a','w','.','d','l','l',0},
731         {'d','e','v','e','n','u','m','.','d','l','l',0},
732         {'d','i','n','p','u','t','8','.','d','l','l',0},
733         {'d','i','n','p','u','t','.','d','l','l',0},
734         {'d','m','b','a','n','d','.','d','l','l',0},
735         {'d','m','c','o','m','p','o','s','.','d','l','l',0},
736         {'d','m','i','m','e','.','d','l','l',0},
737         {'d','m','l','o','a','d','e','r','.','d','l','l',0},
738         {'d','m','s','c','r','i','p','t','.','d','l','l',0},
739         {'d','m','s','t','y','l','e','.','d','l','l',0},
740         {'d','m','s','y','n','t','h','.','d','l','l',0},
741         {'d','m','u','s','i','c','.','d','l','l',0},
742         {'d','p','l','a','y','x','.','d','l','l',0},
743         {'d','p','n','e','t','.','d','l','l',0},
744         {'d','s','o','u','n','d','.','d','l','l',0},
745         {'d','s','w','a','v','e','.','d','l','l',0},
746         {'d','x','d','i','a','g','n','.','d','l','l',0},
747         {'q','u','a','r','t','z','.','d','l','l',0}
748     };
749
750     HRESULT hr;
751     WCHAR szFilePath[MAX_PATH];
752     INT i;
753
754     GetSystemDirectoryW(szFilePath, MAX_PATH);
755
756     for (i = 0; i < sizeof(dlls) / sizeof(dlls[0]); i++)
757     {
758         static const WCHAR szFormat[] = {'%','d',0};
759
760         WCHAR szFileID[5];
761         IDxDiagContainerImpl_Container *file_container;
762
763         snprintfW(szFileID, sizeof(szFileID)/sizeof(szFileID[0]), szFormat, i);
764
765         file_container = allocate_information_node(szFileID);
766         if (!file_container)
767             return E_OUTOFMEMORY;
768
769         hr = fill_file_description(file_container, szFilePath, dlls[i]);
770         if (FAILED(hr))
771         {
772             free_information_tree(file_container);
773             continue;
774         }
775
776         add_subcontainer(node, file_container);
777     }
778
779     return S_OK;
780 }
781
782 static HRESULT read_property_names(IPropertyBag *pPropBag, VARIANT *friendly_name, VARIANT *clsid_name)
783 {
784     static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
785     static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
786
787     HRESULT hr;
788
789     VariantInit(friendly_name);
790     VariantInit(clsid_name);
791
792     hr = IPropertyBag_Read(pPropBag, wszFriendlyName, friendly_name, 0);
793     if (FAILED(hr))
794         return hr;
795
796     hr = IPropertyBag_Read(pPropBag, wszClsidName, clsid_name, 0);
797     if (FAILED(hr))
798     {
799         VariantClear(friendly_name);
800         return hr;
801     }
802
803     return S_OK;
804 }
805
806 static HRESULT fill_filter_data_information(IDxDiagContainerImpl_Container *subcont, BYTE *pData, ULONG cb)
807 {
808     static const WCHAR szVersionW[] = {'s','z','V','e','r','s','i','o','n',0};
809     static const WCHAR dwInputs[] = {'d','w','I','n','p','u','t','s',0};
810     static const WCHAR dwOutputs[] = {'d','w','O','u','t','p','u','t','s',0};
811     static const WCHAR dwMeritW[] = {'d','w','M','e','r','i','t',0};
812     static const WCHAR szVersionFormat[] = {'v','%','d',0};
813
814     HRESULT hr;
815     IFilterMapper2 *pFileMapper = NULL;
816     IAMFilterData *pFilterData = NULL;
817     REGFILTER2 *pRF = NULL;
818     WCHAR bufferW[10];
819     ULONG j;
820     DWORD dwNOutputs = 0;
821     DWORD dwNInputs = 0;
822
823     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC, &IID_IFilterMapper2,
824                           (void **)&pFileMapper);
825     if (FAILED(hr))
826         return hr;
827
828     hr = IFilterMapper2_QueryInterface(pFileMapper, &IID_IAMFilterData, (void **)&pFilterData);
829     if (FAILED(hr))
830         goto cleanup;
831
832     hr = IAMFilterData_ParseFilterData(pFilterData, pData, cb, (BYTE **)&pRF);
833     if (FAILED(hr))
834         goto cleanup;
835
836     snprintfW(bufferW, sizeof(bufferW)/sizeof(bufferW[0]), szVersionFormat, pRF->dwVersion);
837     hr = add_bstr_property(subcont, szVersionW, bufferW);
838     if (FAILED(hr))
839         goto cleanup;
840
841     if (pRF->dwVersion == 1)
842     {
843         for (j = 0; j < pRF->u.s.cPins; j++)
844             if (pRF->u.s.rgPins[j].bOutput)
845                 dwNOutputs++;
846             else
847                 dwNInputs++;
848     }
849     else if (pRF->dwVersion == 2)
850     {
851         for (j = 0; j < pRF->u.s1.cPins2; j++)
852             if (pRF->u.s1.rgPins2[j].dwFlags & REG_PINFLAG_B_OUTPUT)
853                 dwNOutputs++;
854             else
855                 dwNInputs++;
856     }
857
858     hr = add_ui4_property(subcont, dwInputs, dwNInputs);
859     if (FAILED(hr))
860         goto cleanup;
861
862     hr = add_ui4_property(subcont, dwOutputs, dwNOutputs);
863     if (FAILED(hr))
864         goto cleanup;
865
866     hr = add_ui4_property(subcont, dwMeritW, pRF->dwMerit);
867     if (FAILED(hr))
868         goto cleanup;
869
870     hr = S_OK;
871 cleanup:
872     CoTaskMemFree(pRF);
873     if (pFilterData) IAMFilterData_Release(pFilterData);
874     if (pFileMapper) IFilterMapper2_Release(pFileMapper);
875
876     return hr;
877 }
878
879 static HRESULT fill_filter_container(IDxDiagContainerImpl_Container *subcont, IMoniker *pMoniker)
880 {
881     static const WCHAR szName[] = {'s','z','N','a','m','e',0};
882     static const WCHAR ClsidFilterW[] = {'C','l','s','i','d','F','i','l','t','e','r',0};
883     static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
884
885     HRESULT hr;
886     IPropertyBag *pPropFilterBag = NULL;
887     BYTE *pData;
888     VARIANT friendly_name;
889     VARIANT clsid_name;
890     VARIANT v;
891
892     VariantInit(&friendly_name);
893     VariantInit(&clsid_name);
894     VariantInit(&v);
895
896     hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (void **)&pPropFilterBag);
897     if (FAILED(hr))
898         return hr;
899
900     hr = read_property_names(pPropFilterBag, &friendly_name, &clsid_name);
901     if (FAILED(hr))
902         goto cleanup;
903
904     TRACE("Name = %s\n", debugstr_w(V_BSTR(&friendly_name)));
905     TRACE("CLSID = %s\n", debugstr_w(V_BSTR(&clsid_name)));
906
907     hr = add_bstr_property(subcont, szName, V_BSTR(&friendly_name));
908     if (FAILED(hr))
909         goto cleanup;
910
911     hr = add_bstr_property(subcont, ClsidFilterW, V_BSTR(&clsid_name));
912     if (FAILED(hr))
913         goto cleanup;
914
915     hr = IPropertyBag_Read(pPropFilterBag, wszFilterDataName, &v, NULL);
916     if (FAILED(hr))
917         goto cleanup;
918
919     hr = SafeArrayAccessData(V_ARRAY(&v), (void **)&pData);
920     if (FAILED(hr))
921         goto cleanup;
922
923     hr = fill_filter_data_information(subcont, pData, V_ARRAY(&v)->rgsabound->cElements);
924     SafeArrayUnaccessData(V_ARRAY(&v));
925     if (FAILED(hr))
926         goto cleanup;
927
928     hr = S_OK;
929 cleanup:
930     VariantClear(&v);
931     VariantClear(&clsid_name);
932     VariantClear(&friendly_name);
933     if (pPropFilterBag) IPropertyBag_Release(pPropFilterBag);
934
935     return hr;
936 }
937
938 static HRESULT build_directshowfilters_tree(IDxDiagContainerImpl_Container *node)
939 {
940     static const WCHAR szCatName[] = {'s','z','C','a','t','N','a','m','e',0};
941     static const WCHAR ClsidCatW[] = {'C','l','s','i','d','C','a','t',0};
942     static const WCHAR szIdFormat[] = {'%','d',0};
943
944     HRESULT hr;
945     int i = 0;
946     ICreateDevEnum *pCreateDevEnum;
947     IEnumMoniker *pEmCat = NULL;
948     IMoniker *pMCat = NULL;
949         IEnumMoniker *pEnum = NULL;
950
951     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
952                           &IID_ICreateDevEnum, (void **)&pCreateDevEnum);
953     if (FAILED(hr))
954         return hr;
955
956     hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEmCat, 0);
957     if (FAILED(hr))
958         goto cleanup;
959
960     while (IEnumMoniker_Next(pEmCat, 1, &pMCat, NULL) == S_OK)
961     {
962         VARIANT vCatName;
963         VARIANT vCatClsid;
964         IPropertyBag *pPropBag;
965         CLSID clsidCat;
966         IMoniker *pMoniker = NULL;
967
968         hr = IMoniker_BindToStorage(pMCat, NULL, NULL, &IID_IPropertyBag, (void **)&pPropBag);
969         if (FAILED(hr))
970         {
971             IMoniker_Release(pMCat);
972             break;
973         }
974
975         hr = read_property_names(pPropBag, &vCatName, &vCatClsid);
976         IPropertyBag_Release(pPropBag);
977         if (FAILED(hr))
978         {
979             IMoniker_Release(pMCat);
980             break;
981         }
982
983         hr = CLSIDFromString(V_BSTR(&vCatClsid), &clsidCat);
984         if (FAILED(hr))
985         {
986             IMoniker_Release(pMCat);
987             VariantClear(&vCatClsid);
988             VariantClear(&vCatName);
989             break;
990         }
991
992         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
993         if (hr != S_OK)
994         {
995             IMoniker_Release(pMCat);
996             VariantClear(&vCatClsid);
997             VariantClear(&vCatName);
998             continue;
999         }
1000
1001         TRACE("Enumerating class %s\n", debugstr_guid(&clsidCat));
1002
1003         while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
1004         {
1005             WCHAR bufferW[10];
1006             IDxDiagContainerImpl_Container *subcont;
1007
1008             snprintfW(bufferW, sizeof(bufferW)/sizeof(bufferW[0]), szIdFormat, i);
1009             subcont = allocate_information_node(bufferW);
1010             if (!subcont)
1011             {
1012                 hr = E_OUTOFMEMORY;
1013                 IMoniker_Release(pMoniker);
1014                 break;
1015             }
1016
1017             hr = add_bstr_property(subcont, szCatName, V_BSTR(&vCatName));
1018             if (FAILED(hr))
1019             {
1020                 free_information_tree(subcont);
1021                 IMoniker_Release(pMoniker);
1022                 break;
1023             }
1024
1025             hr = add_bstr_property(subcont, ClsidCatW, V_BSTR(&vCatClsid));
1026             if (FAILED(hr))
1027             {
1028                 free_information_tree(subcont);
1029                 IMoniker_Release(pMoniker);
1030                 break;
1031             }
1032
1033             hr = fill_filter_container(subcont, pMoniker);
1034             if (FAILED(hr))
1035             {
1036                 free_information_tree(subcont);
1037                 IMoniker_Release(pMoniker);
1038                 break;
1039             }
1040
1041             add_subcontainer(node, subcont);
1042             i++;
1043             IMoniker_Release(pMoniker);
1044         }
1045
1046         IEnumMoniker_Release(pEnum);
1047         IMoniker_Release(pMCat);
1048         VariantClear(&vCatClsid);
1049         VariantClear(&vCatName);
1050
1051         if (FAILED(hr))
1052             break;
1053     }
1054
1055 cleanup:
1056     if (pEmCat) IEnumMoniker_Release(pEmCat);
1057     ICreateDevEnum_Release(pCreateDevEnum);
1058     return hr;
1059 }
1060
1061 static HRESULT build_logicaldisks_tree(IDxDiagContainerImpl_Container *node)
1062 {
1063     return S_OK;
1064 }
1065
1066 static HRESULT build_information_tree(IDxDiagContainerImpl_Container **pinfo_root)
1067 {
1068     static const WCHAR DxDiag_SystemInfo[] = {'D','x','D','i','a','g','_','S','y','s','t','e','m','I','n','f','o',0};
1069     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};
1070     static const WCHAR DxDiag_DirectSound[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','S','o','u','n','d',0};
1071     static const WCHAR DxDiag_DirectMusic[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','M','u','s','i','c',0};
1072     static const WCHAR DxDiag_DirectInput[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','I','n','p','u','t',0};
1073     static const WCHAR DxDiag_DirectPlay[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','P','l','a','y',0};
1074     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};
1075     static const WCHAR DxDiag_DirectXFiles[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','X','F','i','l','e','s',0};
1076     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};
1077     static const WCHAR DxDiag_LogicalDisks[] = {'D','x','D','i','a','g','_','L','o','g','i','c','a','l','D','i','s','k','s',0};
1078
1079     static const struct
1080     {
1081         const WCHAR *name;
1082         HRESULT (*initfunc)(IDxDiagContainerImpl_Container *);
1083     } root_children[] =
1084     {
1085         {DxDiag_SystemInfo, build_systeminfo_tree},
1086         {DxDiag_DisplayDevices, build_displaydevices_tree},
1087         {DxDiag_DirectSound, build_directsound_tree},
1088         {DxDiag_DirectMusic, build_directmusic_tree},
1089         {DxDiag_DirectInput, build_directinput_tree},
1090         {DxDiag_DirectPlay, build_directplay_tree},
1091         {DxDiag_SystemDevices, build_systemdevices_tree},
1092         {DxDiag_DirectXFiles, build_directxfiles_tree},
1093         {DxDiag_DirectShowFilters, build_directshowfilters_tree},
1094         {DxDiag_LogicalDisks, build_logicaldisks_tree},
1095     };
1096
1097     IDxDiagContainerImpl_Container *info_root;
1098     size_t index;
1099
1100     info_root = allocate_information_node(NULL);
1101     if (!info_root)
1102         return E_OUTOFMEMORY;
1103
1104     for (index = 0; index < sizeof(root_children)/sizeof(root_children[0]); index++)
1105     {
1106         IDxDiagContainerImpl_Container *node;
1107         HRESULT hr;
1108
1109         node = allocate_information_node(root_children[index].name);
1110         if (!node)
1111         {
1112             free_information_tree(info_root);
1113             return E_OUTOFMEMORY;
1114         }
1115
1116         hr = root_children[index].initfunc(node);
1117         if (FAILED(hr))
1118         {
1119             free_information_tree(node);
1120             free_information_tree(info_root);
1121             return hr;
1122         }
1123
1124         add_subcontainer(info_root, node);
1125     }
1126
1127     *pinfo_root = info_root;
1128     return S_OK;
1129 }