dsound: Added missing LeaveCriticalSection (Coverity).
[wine] / dlls / dxdiagn / provider.c
1 /* 
2  * IDxDiagProvider Implementation
3  * 
4  * Copyright 2004-2005 Raphael Junqueira
5  * Copyright 2010 Andrew Nguyen
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 #include "config.h"
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "dxdiag_private.h"
29 #include "wine/unicode.h"
30 #include "winver.h"
31 #include "objidl.h"
32 #include "dshow.h"
33 #include "vfw.h"
34 #include "mmddk.h"
35 #include "ddraw.h"
36 #include "d3d9.h"
37 #include "strmif.h"
38 #include "initguid.h"
39 #include "fil_data.h"
40 #include "psapi.h"
41
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
45
46 static const WCHAR szEmpty[] = {0};
47
48 static HRESULT build_information_tree(IDxDiagContainerImpl_Container **pinfo_root);
49 static void free_information_tree(IDxDiagContainerImpl_Container *node);
50
51 static const WCHAR szDescription[] = {'s','z','D','e','s','c','r','i','p','t','i','o','n',0};
52 static const WCHAR szDeviceName[] = {'s','z','D','e','v','i','c','e','N','a','m','e',0};
53 static const WCHAR szKeyDeviceID[] = {'s','z','K','e','y','D','e','v','i','c','e','I','D',0};
54 static const WCHAR szKeyDeviceKey[] = {'s','z','K','e','y','D','e','v','i','c','e','K','e','y',0};
55 static const WCHAR szVendorId[] = {'s','z','V','e','n','d','o','r','I','d',0};
56 static const WCHAR szDeviceId[] = {'s','z','D','e','v','i','c','e','I','d',0};
57 static const WCHAR szDeviceIdentifier[] = {'s','z','D','e','v','i','c','e','I','d','e','n','t','i','f','i','e','r',0};
58 static const WCHAR dwWidth[] = {'d','w','W','i','d','t','h',0};
59 static const WCHAR dwHeight[] = {'d','w','H','e','i','g','h','t',0};
60 static const WCHAR dwBpp[] = {'d','w','B','p','p',0};
61 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};
62 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};
63 static const WCHAR szDriverName[] = {'s','z','D','r','i','v','e','r','N','a','m','e',0};
64 static const WCHAR szDriverVersion[] = {'s','z','D','r','i','v','e','r','V','e','r','s','i','o','n',0};
65 static const WCHAR szSubSysId[] = {'s','z','S','u','b','S','y','s','I','d',0};
66 static const WCHAR szRevisionId[] = {'s','z','R','e','v','i','s','i','o','n','I','d',0};
67 static const WCHAR dwRefreshRate[] = {'d','w','R','e','f','r','e','s','h','R','a','t','e',0};
68 static const WCHAR szManufacturer[] = {'s','z','M','a','n','u','f','a','c','t','u','r','e','r',0};
69
70 struct IDxDiagProviderImpl
71 {
72   IDxDiagProvider IDxDiagProvider_iface;
73   LONG ref;
74   BOOL init;
75   DXDIAG_INIT_PARAMS params;
76   IDxDiagContainerImpl_Container *info_root;
77 };
78
79 static inline IDxDiagProviderImpl *impl_from_IDxDiagProvider(IDxDiagProvider *iface)
80 {
81      return CONTAINING_RECORD(iface, IDxDiagProviderImpl, IDxDiagProvider_iface);
82 }
83
84 /* IDxDiagProvider IUnknown parts follow: */
85 static HRESULT WINAPI IDxDiagProviderImpl_QueryInterface(IDxDiagProvider *iface, REFIID riid,
86         void **ppobj)
87 {
88     IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
89
90     if (!ppobj) return E_INVALIDARG;
91
92     if (IsEqualGUID(riid, &IID_IUnknown)
93         || IsEqualGUID(riid, &IID_IDxDiagProvider)) {
94         IUnknown_AddRef(iface);
95         *ppobj = This;
96         return S_OK;
97     }
98
99     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
100     *ppobj = NULL;
101     return E_NOINTERFACE;
102 }
103
104 static ULONG WINAPI IDxDiagProviderImpl_AddRef(IDxDiagProvider *iface)
105 {
106     IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
107     ULONG refCount = InterlockedIncrement(&This->ref);
108
109     TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
110
111     DXDIAGN_LockModule();
112
113     return refCount;
114 }
115
116 static ULONG WINAPI IDxDiagProviderImpl_Release(IDxDiagProvider *iface)
117 {
118     IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
119     ULONG refCount = InterlockedDecrement(&This->ref);
120
121     TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
122
123     if (!refCount) {
124         free_information_tree(This->info_root);
125         HeapFree(GetProcessHeap(), 0, This);
126     }
127
128     DXDIAGN_UnlockModule();
129     
130     return refCount;
131 }
132
133 /* IDxDiagProvider Interface follow: */
134 static HRESULT WINAPI IDxDiagProviderImpl_Initialize(IDxDiagProvider *iface,
135         DXDIAG_INIT_PARAMS *pParams)
136 {
137     IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
138     HRESULT hr;
139
140     TRACE("(%p,%p)\n", iface, pParams);
141
142     if (NULL == pParams) {
143       return E_POINTER;
144     }
145     if (pParams->dwSize != sizeof(DXDIAG_INIT_PARAMS) ||
146         pParams->dwDxDiagHeaderVersion != DXDIAG_DX9_SDK_VERSION) {
147       return E_INVALIDARG;
148     }
149
150     if (!This->info_root)
151     {
152         hr = build_information_tree(&This->info_root);
153         if (FAILED(hr))
154             return hr;
155     }
156
157     This->init = TRUE;
158     memcpy(&This->params, pParams, pParams->dwSize);
159     return S_OK;
160 }
161
162 static HRESULT WINAPI IDxDiagProviderImpl_GetRootContainer(IDxDiagProvider *iface,
163         IDxDiagContainer **ppInstance)
164 {
165   IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
166
167   TRACE("(%p,%p)\n", iface, ppInstance);
168
169   if (FALSE == This->init) {
170     return CO_E_NOTINITIALIZED;
171   }
172
173   return DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, This->info_root,
174           &This->IDxDiagProvider_iface, (void **)ppInstance);
175 }
176
177 static const IDxDiagProviderVtbl DxDiagProvider_Vtbl =
178 {
179     IDxDiagProviderImpl_QueryInterface,
180     IDxDiagProviderImpl_AddRef,
181     IDxDiagProviderImpl_Release,
182     IDxDiagProviderImpl_Initialize,
183     IDxDiagProviderImpl_GetRootContainer
184 };
185
186 HRESULT DXDiag_CreateDXDiagProvider(LPCLASSFACTORY iface, LPUNKNOWN punkOuter, REFIID riid, LPVOID *ppobj) {
187   IDxDiagProviderImpl* provider;
188
189   TRACE("(%p, %s, %p)\n", punkOuter, debugstr_guid(riid), ppobj);
190
191   *ppobj = NULL;
192   if (punkOuter) return CLASS_E_NOAGGREGATION;
193
194   provider = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagProviderImpl));
195   if (NULL == provider) return E_OUTOFMEMORY;
196   provider->IDxDiagProvider_iface.lpVtbl = &DxDiagProvider_Vtbl;
197   provider->ref = 0; /* will be inited with QueryInterface */
198   return IDxDiagProviderImpl_QueryInterface(&provider->IDxDiagProvider_iface, riid, ppobj);
199 }
200
201 static void free_property_information(IDxDiagContainerImpl_Property *prop)
202 {
203     VariantClear(&prop->vProp);
204     HeapFree(GetProcessHeap(), 0, prop->propName);
205     HeapFree(GetProcessHeap(), 0, prop);
206 }
207
208 static void free_information_tree(IDxDiagContainerImpl_Container *node)
209 {
210     IDxDiagContainerImpl_Container *ptr, *cursor2;
211
212     if (!node)
213         return;
214
215     HeapFree(GetProcessHeap(), 0, node->contName);
216
217     LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &node->subContainers, IDxDiagContainerImpl_Container, entry)
218     {
219         IDxDiagContainerImpl_Property *prop, *prop_cursor2;
220
221         LIST_FOR_EACH_ENTRY_SAFE(prop, prop_cursor2, &ptr->properties, IDxDiagContainerImpl_Property, entry)
222         {
223             list_remove(&prop->entry);
224             free_property_information(prop);
225         }
226
227         list_remove(&ptr->entry);
228         free_information_tree(ptr);
229     }
230
231     HeapFree(GetProcessHeap(), 0, node);
232 }
233
234 static IDxDiagContainerImpl_Container *allocate_information_node(const WCHAR *name)
235 {
236     IDxDiagContainerImpl_Container *ret;
237
238     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
239     if (!ret)
240         return NULL;
241
242     if (name)
243     {
244         ret->contName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1) * sizeof(*name));
245         if (!ret->contName)
246         {
247             HeapFree(GetProcessHeap(), 0, ret);
248             return NULL;
249         }
250         strcpyW(ret->contName, name);
251     }
252
253     list_init(&ret->subContainers);
254     list_init(&ret->properties);
255
256     return ret;
257 }
258
259 static IDxDiagContainerImpl_Property *allocate_property_information(const WCHAR *name)
260 {
261     IDxDiagContainerImpl_Property *ret;
262
263     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
264     if (!ret)
265         return NULL;
266
267     ret->propName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1) * sizeof(*name));
268     if (!ret->propName)
269     {
270         HeapFree(GetProcessHeap(), 0, ret);
271         return NULL;
272     }
273     strcpyW(ret->propName, name);
274
275     return ret;
276 }
277
278 static inline void add_subcontainer(IDxDiagContainerImpl_Container *node, IDxDiagContainerImpl_Container *subCont)
279 {
280     list_add_tail(&node->subContainers, &subCont->entry);
281     ++node->nSubContainers;
282 }
283
284 static inline HRESULT add_bstr_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, const WCHAR *str)
285 {
286     IDxDiagContainerImpl_Property *prop;
287     BSTR bstr;
288
289     prop = allocate_property_information(propName);
290     if (!prop)
291         return E_OUTOFMEMORY;
292
293     bstr = SysAllocString(str);
294     if (!bstr)
295     {
296         free_property_information(prop);
297         return E_OUTOFMEMORY;
298     }
299
300     V_VT(&prop->vProp) = VT_BSTR;
301     V_BSTR(&prop->vProp) = bstr;
302
303     list_add_tail(&node->properties, &prop->entry);
304     ++node->nProperties;
305
306     return S_OK;
307 }
308
309 static inline HRESULT add_ui4_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, DWORD data)
310 {
311     IDxDiagContainerImpl_Property *prop;
312
313     prop = allocate_property_information(propName);
314     if (!prop)
315         return E_OUTOFMEMORY;
316
317     V_VT(&prop->vProp) = VT_UI4;
318     V_UI4(&prop->vProp) = data;
319
320     list_add_tail(&node->properties, &prop->entry);
321     ++node->nProperties;
322
323     return S_OK;
324 }
325
326 static inline HRESULT add_bool_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, BOOL data)
327 {
328     IDxDiagContainerImpl_Property *prop;
329
330     prop = allocate_property_information(propName);
331     if (!prop)
332         return E_OUTOFMEMORY;
333
334     V_VT(&prop->vProp) = VT_BOOL;
335     V_BOOL(&prop->vProp) = data;
336
337     list_add_tail(&node->properties, &prop->entry);
338     ++node->nProperties;
339
340     return S_OK;
341 }
342
343 static inline HRESULT add_ull_as_bstr_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, ULONGLONG data )
344 {
345     IDxDiagContainerImpl_Property *prop;
346
347     prop = allocate_property_information(propName);
348     if (!prop)
349         return E_OUTOFMEMORY;
350
351     V_VT(&prop->vProp) = VT_UI8;
352     V_UI8(&prop->vProp) = data;
353
354     VariantChangeType(&prop->vProp, &prop->vProp, 0, VT_BSTR);
355
356     list_add_tail(&node->properties, &prop->entry);
357     ++node->nProperties;
358
359     return S_OK;
360 }
361
362 /* Copied from programs/taskkill/taskkill.c. */
363 static DWORD *enumerate_processes(DWORD *list_count)
364 {
365     DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
366
367     pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
368     if (!pid_list)
369         return NULL;
370
371     for (;;)
372     {
373         DWORD *realloc_list;
374
375         if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
376         {
377             HeapFree(GetProcessHeap(), 0, pid_list);
378             return NULL;
379         }
380
381         /* EnumProcesses can't signal an insufficient buffer condition, so the
382          * only way to possibly determine whether a larger buffer is required
383          * is to see whether the written number of bytes is the same as the
384          * buffer size. If so, the buffer will be reallocated to twice the
385          * size. */
386         if (alloc_bytes != needed_bytes)
387             break;
388
389         alloc_bytes *= 2;
390         realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
391         if (!realloc_list)
392         {
393             HeapFree(GetProcessHeap(), 0, pid_list);
394             return NULL;
395         }
396         pid_list = realloc_list;
397     }
398
399     *list_count = needed_bytes / sizeof(*pid_list);
400     return pid_list;
401 }
402
403 /* Copied from programs/taskkill/taskkill.c. */
404 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
405 {
406     HANDLE process;
407     HMODULE module;
408     DWORD required_size;
409
410     process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
411     if (!process)
412         return FALSE;
413
414     if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
415     {
416         CloseHandle(process);
417         return FALSE;
418     }
419
420     if (!GetModuleBaseNameW(process, module, buf, chars))
421     {
422         CloseHandle(process);
423         return FALSE;
424     }
425
426     CloseHandle(process);
427     return TRUE;
428 }
429
430 /* dxdiagn's detection scheme is simply to look for a process called conf.exe. */
431 static BOOL is_netmeeting_running(void)
432 {
433     static const WCHAR conf_exe[] = {'c','o','n','f','.','e','x','e',0};
434
435     DWORD list_count;
436     DWORD *pid_list = enumerate_processes(&list_count);
437
438     if (pid_list)
439     {
440         DWORD i;
441         WCHAR process_name[MAX_PATH];
442
443         for (i = 0; i < list_count; i++)
444         {
445             if (get_process_name_from_pid(pid_list[i], process_name, sizeof(process_name)/sizeof(WCHAR)) &&
446                 !lstrcmpW(conf_exe, process_name))
447             {
448                 HeapFree(GetProcessHeap(), 0, pid_list);
449                 return TRUE;
450             }
451         }
452         HeapFree(GetProcessHeap(), 0, pid_list);
453     }
454
455     return FALSE;
456 }
457
458 static HRESULT fill_language_information(IDxDiagContainerImpl_Container *node)
459 {
460     static const WCHAR regional_setting_engW[] = {'R','e','g','i','o','n','a','l',' ','S','e','t','t','i','n','g',0};
461     static const WCHAR languages_fmtW[] = {'%','s',' ','(','%','s',':',' ','%','s',')',0};
462     static const WCHAR szLanguagesLocalized[] = {'s','z','L','a','n','g','u','a','g','e','s','L','o','c','a','l','i','z','e','d',0};
463     static const WCHAR szLanguagesEnglish[] = {'s','z','L','a','n','g','u','a','g','e','s','E','n','g','l','i','s','h',0};
464
465     WCHAR system_lang[80], regional_setting[100], user_lang[80], language_str[300];
466     HRESULT hr;
467
468     /* szLanguagesLocalized */
469     GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SNATIVELANGNAME, system_lang, sizeof(system_lang)/sizeof(WCHAR));
470     LoadStringW(dxdiagn_instance, IDS_REGIONAL_SETTING, regional_setting, sizeof(regional_setting)/sizeof(WCHAR));
471     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SNATIVELANGNAME, user_lang, sizeof(user_lang)/sizeof(WCHAR));
472
473     snprintfW(language_str, sizeof(language_str)/sizeof(WCHAR), languages_fmtW, system_lang, regional_setting, user_lang);
474
475     hr = add_bstr_property(node, szLanguagesLocalized, language_str);
476     if (FAILED(hr))
477         return hr;
478
479     /* szLanguagesEnglish */
480     GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, system_lang, sizeof(system_lang)/sizeof(WCHAR));
481     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, user_lang, sizeof(user_lang)/sizeof(WCHAR));
482
483     snprintfW(language_str, sizeof(language_str)/sizeof(WCHAR), languages_fmtW, system_lang, regional_setting_engW, user_lang);
484
485     hr = add_bstr_property(node, szLanguagesEnglish, language_str);
486     if (FAILED(hr))
487         return hr;
488
489     return S_OK;
490 }
491
492 static HRESULT fill_datetime_information(IDxDiagContainerImpl_Container *node)
493 {
494     static const WCHAR date_fmtW[] = {'M','\'','/','\'','d','\'','/','\'','y','y','y','y',0};
495     static const WCHAR time_fmtW[] = {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
496     static const WCHAR datetime_fmtW[] = {'%','s',',',' ','%','s',0};
497     static const WCHAR szTimeLocalized[] = {'s','z','T','i','m','e','L','o','c','a','l','i','z','e','d',0};
498     static const WCHAR szTimeEnglish[] = {'s','z','T','i','m','e','E','n','g','l','i','s','h',0};
499
500     SYSTEMTIME curtime;
501     WCHAR date_str[80], time_str[80], datetime_str[200];
502     HRESULT hr;
503
504     GetLocalTime(&curtime);
505
506     GetTimeFormatW(LOCALE_NEUTRAL, 0, &curtime, time_fmtW, time_str, sizeof(time_str)/sizeof(WCHAR));
507
508     /* szTimeLocalized */
509     GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &curtime, NULL, date_str, sizeof(date_str)/sizeof(WCHAR));
510
511     snprintfW(datetime_str, sizeof(datetime_str)/sizeof(WCHAR), datetime_fmtW, date_str, time_str);
512
513     hr = add_bstr_property(node, szTimeLocalized, datetime_str);
514     if (FAILED(hr))
515         return hr;
516
517     /* szTimeEnglish */
518     GetDateFormatW(LOCALE_NEUTRAL, 0, &curtime, date_fmtW, date_str, sizeof(date_str)/sizeof(WCHAR));
519
520     snprintfW(datetime_str, sizeof(datetime_str)/sizeof(WCHAR), datetime_fmtW, date_str, time_str);
521
522     hr = add_bstr_property(node, szTimeEnglish, datetime_str);
523     if (FAILED(hr))
524         return hr;
525
526     return S_OK;
527 }
528
529 static HRESULT fill_os_string_information(IDxDiagContainerImpl_Container *node, OSVERSIONINFOW *info)
530 {
531     static const WCHAR winxpW[] = {'W','i','n','d','o','w','s',' ','X','P',' ','P','r','o','f','e','s','s','i','o','n','a','l',0};
532     static const WCHAR szOSLocalized[] = {'s','z','O','S','L','o','c','a','l','i','z','e','d',0};
533     static const WCHAR szOSExLocalized[] = {'s','z','O','S','E','x','L','o','c','a','l','i','z','e','d',0};
534     static const WCHAR szOSExLongLocalized[] = {'s','z','O','S','E','x','L','o','n','g','L','o','c','a','l','i','z','e','d',0};
535     static const WCHAR szOSEnglish[] = {'s','z','O','S','E','n','g','l','i','s','h',0};
536     static const WCHAR szOSExEnglish[] = {'s','z','O','S','E','x','E','n','g','l','i','s','h',0};
537     static const WCHAR szOSExLongEnglish[] = {'s','z','O','S','E','x','L','o','n','g','E','n','g','l','i','s','h',0};
538
539     static const WCHAR *prop_list[] = {szOSLocalized, szOSExLocalized, szOSExLongLocalized,
540                                        szOSEnglish, szOSExEnglish, szOSExLongEnglish};
541
542     size_t i;
543     HRESULT hr;
544
545     /* FIXME: OS detection should be performed, and localized OS strings
546      * should contain translated versions of the "build" phrase. */
547     for (i = 0; i < sizeof(prop_list)/sizeof(prop_list[0]); i++)
548     {
549         hr = add_bstr_property(node, prop_list[i], winxpW);
550         if (FAILED(hr))
551             return hr;
552     }
553
554     return S_OK;
555 }
556
557 static HRESULT build_systeminfo_tree(IDxDiagContainerImpl_Container *node)
558 {
559     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};
560     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};
561     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};
562     static const WCHAR szDirectXVersionLetter_v[] = {'c',0};
563     static const WCHAR bDebug[] = {'b','D','e','b','u','g',0};
564     static const WCHAR bNECPC98[] = {'b','N','E','C','P','C','9','8',0};
565     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};
566     static const WCHAR szDirectXVersionEnglish_v[] = {'4','.','0','9','.','0','0','0','0','.','0','9','0','4',0};
567     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};
568     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};
569     static const WCHAR ullPhysicalMemory[] = {'u','l','l','P','h','y','s','i','c','a','l','M','e','m','o','r','y',0};
570     static const WCHAR ullUsedPageFile[]   = {'u','l','l','U','s','e','d','P','a','g','e','F','i','l','e',0};
571     static const WCHAR ullAvailPageFile[]  = {'u','l','l','A','v','a','i','l','P','a','g','e','F','i','l','e',0};
572     static const WCHAR bNetMeetingRunning[] = {'b','N','e','t','M','e','e','t','i','n','g','R','u','n','n','i','n','g',0};
573     static const WCHAR szWindowsDir[] = {'s','z','W','i','n','d','o','w','s','D','i','r',0};
574     static const WCHAR dwOSMajorVersion[] = {'d','w','O','S','M','a','j','o','r','V','e','r','s','i','o','n',0};
575     static const WCHAR dwOSMinorVersion[] = {'d','w','O','S','M','i','n','o','r','V','e','r','s','i','o','n',0};
576     static const WCHAR dwOSBuildNumber[] = {'d','w','O','S','B','u','i','l','d','N','u','m','b','e','r',0};
577     static const WCHAR dwOSPlatformID[] = {'d','w','O','S','P','l','a','t','f','o','r','m','I','D',0};
578     static const WCHAR szCSDVersion[] = {'s','z','C','S','D','V','e','r','s','i','o','n',0};
579     static const WCHAR szPhysicalMemoryEnglish[] = {'s','z','P','h','y','s','i','c','a','l','M','e','m','o','r','y','E','n','g','l','i','s','h',0};
580     static const WCHAR szPageFileLocalized[] = {'s','z','P','a','g','e','F','i','l','e','L','o','c','a','l','i','z','e','d',0};
581     static const WCHAR szPageFileEnglish[] = {'s','z','P','a','g','e','F','i','l','e','E','n','g','l','i','s','h',0};
582     static const WCHAR szMachineNameLocalized[] = {'s','z','M','a','c','h','i','n','e','N','a','m','e','L','o','c','a','l','i','z','e','d',0};
583     static const WCHAR szMachineNameEnglish[] = {'s','z','M','a','c','h','i','n','e','N','a','m','e','E','n','g','l','i','s','h',0};
584     static const WCHAR szSystemManufacturerEnglish[] = {'s','z','S','y','s','t','e','m','M','a','n','u','f','a','c','t','u','r','e','r','E','n','g','l','i','s','h',0};
585     static const WCHAR szSystemModelEnglish[] = {'s','z','S','y','s','t','e','m','M','o','d','e','l','E','n','g','l','i','s','h',0};
586     static const WCHAR szBIOSEnglish[] = {'s','z','B','I','O','S','E','n','g','l','i','s','h',0};
587     static const WCHAR szProcessorEnglish[] = {'s','z','P','r','o','c','e','s','s','o','r','E','n','g','l','i','s','h',0};
588     static const WCHAR szSetupParamEnglish[] = {'s','z','S','e','t','u','p','P','a','r','a','m','E','n','g','l','i','s','h',0};
589     static const WCHAR szDxDiagVersion[] = {'s','z','D','x','D','i','a','g','V','e','r','s','i','o','n',0};
590
591     static const WCHAR notpresentW[] = {'N','o','t',' ','p','r','e','s','e','n','t',0};
592
593     static const WCHAR pagefile_fmtW[] = {'%','u','M','B',' ','u','s','e','d',',',' ','%','u','M','B',' ','a','v','a','i','l','a','b','l','e',0};
594     static const WCHAR physmem_fmtW[] = {'%','u','M','B',' ','R','A','M',0};
595
596     HRESULT hr;
597     MEMORYSTATUSEX msex;
598     OSVERSIONINFOW info;
599     DWORD count, usedpage_mb, availpage_mb;
600     WCHAR buffer[MAX_PATH], computer_name[MAX_COMPUTERNAME_LENGTH + 1], print_buf[200], localized_pagefile_fmt[200];
601
602     hr = add_ui4_property(node, dwDirectXVersionMajor, 9);
603     if (FAILED(hr))
604         return hr;
605
606     hr = add_ui4_property(node, dwDirectXVersionMinor, 0);
607     if (FAILED(hr))
608         return hr;
609
610     hr = add_bstr_property(node, szDirectXVersionLetter, szDirectXVersionLetter_v);
611     if (FAILED(hr))
612         return hr;
613
614     hr = add_bstr_property(node, szDirectXVersionEnglish, szDirectXVersionEnglish_v);
615     if (FAILED(hr))
616         return hr;
617
618     hr = add_bstr_property(node, szDirectXVersionLongEnglish, szDirectXVersionLongEnglish_v);
619     if (FAILED(hr))
620         return hr;
621
622     hr = add_bool_property(node, bDebug, FALSE);
623     if (FAILED(hr))
624         return hr;
625
626     hr = add_bool_property(node, bNECPC98, FALSE);
627     if (FAILED(hr))
628         return hr;
629
630     msex.dwLength = sizeof(msex);
631     GlobalMemoryStatusEx(&msex);
632
633     hr = add_ull_as_bstr_property(node, ullPhysicalMemory, msex.ullTotalPhys);
634     if (FAILED(hr))
635         return hr;
636
637     hr = add_ull_as_bstr_property(node, ullUsedPageFile, msex.ullTotalPageFile - msex.ullAvailPageFile);
638     if (FAILED(hr))
639         return hr;
640
641     hr = add_ull_as_bstr_property(node, ullAvailPageFile, msex.ullAvailPageFile);
642     if (FAILED(hr))
643         return hr;
644
645     hr = add_bool_property(node, bNetMeetingRunning, is_netmeeting_running());
646     if (FAILED(hr))
647         return hr;
648
649     info.dwOSVersionInfoSize = sizeof(info);
650     GetVersionExW(&info);
651
652     hr = add_ui4_property(node, dwOSMajorVersion, info.dwMajorVersion);
653     if (FAILED(hr))
654         return hr;
655
656     hr = add_ui4_property(node, dwOSMinorVersion, info.dwMinorVersion);
657     if (FAILED(hr))
658         return hr;
659
660     hr = add_ui4_property(node, dwOSBuildNumber, info.dwBuildNumber);
661     if (FAILED(hr))
662         return hr;
663
664     hr = add_ui4_property(node, dwOSPlatformID, info.dwPlatformId);
665     if (FAILED(hr))
666         return hr;
667
668     hr = add_bstr_property(node, szCSDVersion, info.szCSDVersion);
669     if (FAILED(hr))
670         return hr;
671
672     /* FIXME: Roundoff should not be done with truncated division. */
673     snprintfW(print_buf, sizeof(print_buf)/sizeof(WCHAR), physmem_fmtW, (DWORD)(msex.ullTotalPhys / (1024 * 1024)));
674     hr = add_bstr_property(node, szPhysicalMemoryEnglish, print_buf);
675     if (FAILED(hr))
676         return hr;
677
678     usedpage_mb = (DWORD)((msex.ullTotalPageFile - msex.ullAvailPageFile) / (1024 * 1024));
679     availpage_mb = (DWORD)(msex.ullAvailPageFile / (1024 * 1024));
680     LoadStringW(dxdiagn_instance, IDS_PAGE_FILE_FORMAT, localized_pagefile_fmt, sizeof(localized_pagefile_fmt)/sizeof(WCHAR));
681     snprintfW(print_buf, sizeof(print_buf)/sizeof(WCHAR), localized_pagefile_fmt, usedpage_mb, availpage_mb);
682
683     hr = add_bstr_property(node, szPageFileLocalized, print_buf);
684     if (FAILED(hr))
685         return hr;
686
687     snprintfW(print_buf, sizeof(print_buf)/sizeof(WCHAR), pagefile_fmtW, usedpage_mb, availpage_mb);
688
689     hr = add_bstr_property(node, szPageFileEnglish, print_buf);
690     if (FAILED(hr))
691         return hr;
692
693     GetWindowsDirectoryW(buffer, MAX_PATH);
694
695     hr = add_bstr_property(node, szWindowsDir, buffer);
696     if (FAILED(hr))
697         return hr;
698
699     count = sizeof(computer_name)/sizeof(WCHAR);
700     if (!GetComputerNameW(computer_name, &count))
701         return E_FAIL;
702
703     hr = add_bstr_property(node, szMachineNameLocalized, computer_name);
704     if (FAILED(hr))
705         return hr;
706
707     hr = add_bstr_property(node, szMachineNameEnglish, computer_name);
708     if (FAILED(hr))
709         return hr;
710
711     hr = add_bstr_property(node, szSystemManufacturerEnglish, szEmpty);
712     if (FAILED(hr))
713         return hr;
714
715     hr = add_bstr_property(node, szSystemModelEnglish, szEmpty);
716     if (FAILED(hr))
717         return hr;
718
719     hr = add_bstr_property(node, szBIOSEnglish, szEmpty);
720     if (FAILED(hr))
721         return hr;
722
723     hr = add_bstr_property(node, szProcessorEnglish, szEmpty);
724     if (FAILED(hr))
725         return hr;
726
727     hr = add_bstr_property(node, szSetupParamEnglish, notpresentW);
728     if (FAILED(hr))
729         return hr;
730
731     hr = add_bstr_property(node, szDxDiagVersion, szEmpty);
732     if (FAILED(hr))
733         return hr;
734
735     hr = fill_language_information(node);
736     if (FAILED(hr))
737         return hr;
738
739     hr = fill_datetime_information(node);
740     if (FAILED(hr))
741         return hr;
742
743     hr = fill_os_string_information(node, &info);
744     if (FAILED(hr))
745         return hr;
746
747     return S_OK;
748 }
749
750 /* The logic from pixelformat_for_depth() in dlls/wined3d/utils.c is reversed. */
751 static DWORD depth_for_pixelformat(D3DFORMAT format)
752 {
753     switch (format)
754     {
755     case D3DFMT_P8: return 8;
756     case D3DFMT_X1R5G5B5: return 15;
757     case D3DFMT_R5G6B5: return 16;
758     /* This case will fail to distinguish an original bpp of 24. */
759     case D3DFMT_X8R8G8B8: return 32;
760     default:
761         FIXME("Unknown D3DFORMAT %d, returning 32 bpp\n", format);
762         return 32;
763     }
764 }
765
766 static BOOL get_texture_memory(GUID *adapter, DWORD *available_mem)
767 {
768     IDirectDraw7 *pDirectDraw;
769     HRESULT hr;
770     DDSCAPS2 dd_caps;
771
772     hr = DirectDrawCreateEx(adapter, (void **)&pDirectDraw, &IID_IDirectDraw7, NULL);
773     if (SUCCEEDED(hr))
774     {
775         dd_caps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
776         dd_caps.dwCaps2 = dd_caps.dwCaps3 = dd_caps.dwCaps4 = 0;
777         hr = IDirectDraw7_GetAvailableVidMem(pDirectDraw, &dd_caps, available_mem, NULL);
778         IDirectDraw7_Release(pDirectDraw);
779         if (SUCCEEDED(hr))
780             return TRUE;
781     }
782
783     return FALSE;
784 }
785
786 static const WCHAR *vendor_id_to_manufacturer_string(DWORD vendor_id)
787 {
788     static const WCHAR atiW[] = {'A','T','I',' ','T','e','c','h','n','o','l','o','g','i','e','s',' ','I','n','c','.',0};
789     static const WCHAR nvidiaW[] = {'N','V','I','D','I','A',0};
790     static const WCHAR intelW[] = {'I','n','t','e','l',' ','C','o','r','p','o','r','a','t','i','o','n',0};
791     static const WCHAR unknownW[] = {'U','n','k','n','o','w','n',0};
792
793     /* Enumeration copied from dlls/wined3d/wined3d_private.h and slightly modified. */
794     enum pci_vendor
795     {
796         HW_VENDOR_AMD = 0x1002,
797         HW_VENDOR_NVIDIA = 0x10de,
798         HW_VENDOR_INTEL = 0x8086,
799     };
800
801     switch (vendor_id)
802     {
803     case HW_VENDOR_AMD:
804         return atiW;
805     case HW_VENDOR_NVIDIA:
806         return nvidiaW;
807     case HW_VENDOR_INTEL:
808         return intelW;
809     default:
810         FIXME("Unknown PCI vendor ID 0x%04x\n", vendor_id);
811         return unknownW;
812     }
813 }
814
815 static HRESULT fill_display_information_d3d(IDxDiagContainerImpl_Container *node)
816 {
817     IDxDiagContainerImpl_Container *display_adapter;
818     HRESULT hr;
819     IDirect3D9 *pDirect3D9;
820     WCHAR buffer[256];
821     UINT index, count;
822
823     pDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);
824     if (!pDirect3D9)
825         return E_FAIL;
826
827     count = IDirect3D9_GetAdapterCount(pDirect3D9);
828     for (index = 0; index < count; index++)
829     {
830         static const WCHAR adapterid_fmtW[] = {'%','u',0};
831         static const WCHAR driverversion_fmtW[] = {'%','u','.','%','u','.','%','0','4','u','.','%','0','4','u',0};
832         static const WCHAR id_fmtW[] = {'0','x','%','0','4','x',0};
833         static const WCHAR subsysid_fmtW[] = {'0','x','%','0','8','x',0};
834         static const WCHAR mem_fmt[] = {'%','.','1','f',' ','M','B',0};
835
836         D3DADAPTER_IDENTIFIER9 adapter_info;
837         D3DDISPLAYMODE adapter_mode;
838         DWORD available_mem = 0;
839
840         snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), adapterid_fmtW, index);
841         display_adapter = allocate_information_node(buffer);
842         if (!display_adapter)
843         {
844             hr = E_OUTOFMEMORY;
845             goto cleanup;
846         }
847
848         add_subcontainer(node, display_adapter);
849
850         hr = IDirect3D9_GetAdapterIdentifier(pDirect3D9, index, 0, &adapter_info);
851         if (SUCCEEDED(hr))
852         {
853             WCHAR driverW[sizeof(adapter_info.Driver)];
854             WCHAR descriptionW[sizeof(adapter_info.Description)];
855             WCHAR devicenameW[sizeof(adapter_info.DeviceName)];
856
857             MultiByteToWideChar(CP_ACP, 0, adapter_info.Driver, -1, driverW, sizeof(driverW)/sizeof(WCHAR));
858             MultiByteToWideChar(CP_ACP, 0, adapter_info.Description, -1, descriptionW, sizeof(descriptionW)/sizeof(WCHAR));
859             MultiByteToWideChar(CP_ACP, 0, adapter_info.DeviceName, -1, devicenameW, sizeof(devicenameW)/sizeof(WCHAR));
860
861             hr = add_bstr_property(display_adapter, szDriverName, driverW);
862             if (FAILED(hr))
863                 goto cleanup;
864
865             hr = add_bstr_property(display_adapter, szDescription, descriptionW);
866             if (FAILED(hr))
867                 goto cleanup;
868
869             hr = add_bstr_property(display_adapter, szDeviceName, devicenameW);
870             if (FAILED(hr))
871                 goto cleanup;
872
873             snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), driverversion_fmtW,
874                       HIWORD(adapter_info.DriverVersion.u.HighPart), LOWORD(adapter_info.DriverVersion.u.HighPart),
875                       HIWORD(adapter_info.DriverVersion.u.LowPart), LOWORD(adapter_info.DriverVersion.u.LowPart));
876
877             hr = add_bstr_property(display_adapter, szDriverVersion, buffer);
878             if (FAILED(hr))
879                 goto cleanup;
880
881             snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), id_fmtW, adapter_info.VendorId);
882             hr = add_bstr_property(display_adapter, szVendorId, buffer);
883             if (FAILED(hr))
884                 goto cleanup;
885
886             snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), id_fmtW, adapter_info.DeviceId);
887             hr = add_bstr_property(display_adapter, szDeviceId, buffer);
888             if (FAILED(hr))
889                 goto cleanup;
890
891             snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), subsysid_fmtW, adapter_info.SubSysId);
892             hr = add_bstr_property(display_adapter, szSubSysId, buffer);
893             if (FAILED(hr))
894                 goto cleanup;
895
896             snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), id_fmtW, adapter_info.Revision);
897             hr = add_bstr_property(display_adapter, szRevisionId, buffer);
898             if (FAILED(hr))
899                 goto cleanup;
900
901             StringFromGUID2(&adapter_info.DeviceIdentifier, buffer, 39);
902             hr = add_bstr_property(display_adapter, szDeviceIdentifier, buffer);
903             if (FAILED(hr))
904                 goto cleanup;
905
906             hr = add_bstr_property(display_adapter, szManufacturer, vendor_id_to_manufacturer_string(adapter_info.VendorId));
907             if (FAILED(hr))
908                 goto cleanup;
909         }
910
911         hr = IDirect3D9_GetAdapterDisplayMode(pDirect3D9, index, &adapter_mode);
912         if (SUCCEEDED(hr))
913         {
914             hr = add_ui4_property(display_adapter, dwWidth, adapter_mode.Width);
915             if (FAILED(hr))
916                 goto cleanup;
917
918             hr = add_ui4_property(display_adapter, dwHeight, adapter_mode.Height);
919             if (FAILED(hr))
920                 goto cleanup;
921
922             hr = add_ui4_property(display_adapter, dwRefreshRate, adapter_mode.RefreshRate);
923             if (FAILED(hr))
924                 goto cleanup;
925
926             hr = add_ui4_property(display_adapter, dwBpp, depth_for_pixelformat(adapter_mode.Format));
927             if (FAILED(hr))
928                 goto cleanup;
929         }
930
931         hr = add_bstr_property(display_adapter, szKeyDeviceKey, szEmpty);
932         if (FAILED(hr))
933             goto cleanup;
934
935         hr = add_bstr_property(display_adapter, szKeyDeviceID, szEmpty);
936         if (FAILED(hr))
937             goto cleanup;
938
939         if (!get_texture_memory(&adapter_info.DeviceIdentifier, &available_mem))
940             WARN("get_texture_memory helper failed\n");
941
942         snprintfW(buffer, sizeof(buffer)/sizeof(buffer[0]), mem_fmt, available_mem / 1000000.0f);
943
944         hr = add_bstr_property(display_adapter, szDisplayMemoryLocalized, buffer);
945         if (FAILED(hr))
946             goto cleanup;
947
948         hr = add_bstr_property(display_adapter, szDisplayMemoryEnglish, buffer);
949         if (FAILED(hr))
950             goto cleanup;
951     }
952
953     hr = S_OK;
954 cleanup:
955     IDirect3D9_Release(pDirect3D9);
956     return hr;
957 }
958
959 static HRESULT fill_display_information_fallback(IDxDiagContainerImpl_Container *node)
960 {
961     static const WCHAR szAdapterID[] = {'0',0};
962     static const WCHAR *empty_properties[] = {szDeviceIdentifier, szVendorId, szDeviceId,
963                                               szKeyDeviceKey, szKeyDeviceID, szDriverName,
964                                               szDriverVersion, szSubSysId, szRevisionId,
965                                               szManufacturer};
966
967     IDxDiagContainerImpl_Container *display_adapter;
968     HRESULT hr;
969     IDirectDraw7 *pDirectDraw;
970     DDSCAPS2 dd_caps;
971     DISPLAY_DEVICEW disp_dev;
972     DDSURFACEDESC2 surface_descr;
973     DWORD tmp;
974     WCHAR buffer[256];
975
976     display_adapter = allocate_information_node(szAdapterID);
977     if (!display_adapter)
978         return E_OUTOFMEMORY;
979
980     add_subcontainer(node, display_adapter);
981
982     disp_dev.cb = sizeof(disp_dev);
983     if (EnumDisplayDevicesW( NULL, 0, &disp_dev, 0 ))
984     {
985         hr = add_bstr_property(display_adapter, szDeviceName, disp_dev.DeviceName);
986         if (FAILED(hr))
987             return hr;
988
989         hr = add_bstr_property(display_adapter, szDescription, disp_dev.DeviceString);
990         if (FAILED(hr))
991             return hr;
992     }
993
994     /* Silently ignore a failure from DirectDrawCreateEx. */
995     hr = DirectDrawCreateEx(NULL, (void **)&pDirectDraw, &IID_IDirectDraw7, NULL);
996     if (FAILED(hr))
997         return S_OK;
998
999     dd_caps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1000     dd_caps.dwCaps2 = dd_caps.dwCaps3 = dd_caps.dwCaps4 = 0;
1001     hr = IDirectDraw7_GetAvailableVidMem(pDirectDraw, &dd_caps, &tmp, NULL);
1002     if (SUCCEEDED(hr))
1003     {
1004         static const WCHAR mem_fmt[] = {'%','.','1','f',' ','M','B',0};
1005
1006         snprintfW(buffer, sizeof(buffer)/sizeof(buffer[0]), mem_fmt, tmp / 1000000.0f);
1007
1008         hr = add_bstr_property(display_adapter, szDisplayMemoryLocalized, buffer);
1009         if (FAILED(hr))
1010             goto cleanup;
1011
1012         hr = add_bstr_property(display_adapter, szDisplayMemoryEnglish, buffer);
1013         if (FAILED(hr))
1014             goto cleanup;
1015     }
1016
1017     surface_descr.dwSize = sizeof(surface_descr);
1018     hr = IDirectDraw7_GetDisplayMode(pDirectDraw, &surface_descr);
1019     if (SUCCEEDED(hr))
1020     {
1021         if (surface_descr.dwFlags & DDSD_WIDTH)
1022         {
1023             hr = add_ui4_property(display_adapter, dwWidth, surface_descr.dwWidth);
1024             if (FAILED(hr))
1025                 goto cleanup;
1026         }
1027
1028         if (surface_descr.dwFlags & DDSD_HEIGHT)
1029         {
1030             hr = add_ui4_property(display_adapter, dwHeight, surface_descr.dwHeight);
1031             if (FAILED(hr))
1032                 goto cleanup;
1033         }
1034
1035         if (surface_descr.dwFlags & DDSD_PIXELFORMAT)
1036         {
1037             hr = add_ui4_property(display_adapter, dwBpp, surface_descr.u4.ddpfPixelFormat.u1.dwRGBBitCount);
1038             if (FAILED(hr))
1039                 goto cleanup;
1040         }
1041     }
1042
1043     hr = add_ui4_property(display_adapter, dwRefreshRate, 60);
1044     if (FAILED(hr))
1045         goto cleanup;
1046
1047     for (tmp = 0; tmp < sizeof(empty_properties)/sizeof(empty_properties[0]); tmp++)
1048     {
1049         hr = add_bstr_property(display_adapter, empty_properties[tmp], szEmpty);
1050         if (FAILED(hr))
1051             goto cleanup;
1052     }
1053
1054     hr = S_OK;
1055 cleanup:
1056     IDirectDraw7_Release(pDirectDraw);
1057     return hr;
1058 }
1059
1060 static HRESULT build_displaydevices_tree(IDxDiagContainerImpl_Container *node)
1061 {
1062     HRESULT hr;
1063
1064     /* Try to use Direct3D to obtain the required information first. */
1065     hr = fill_display_information_d3d(node);
1066     if (hr != E_FAIL)
1067         return hr;
1068
1069     return fill_display_information_fallback(node);
1070 }
1071
1072 static HRESULT build_directsound_tree(IDxDiagContainerImpl_Container *node)
1073 {
1074     static const WCHAR DxDiag_SoundDevices[] = {'D','x','D','i','a','g','_','S','o','u','n','d','D','e','v','i','c','e','s',0};
1075     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};
1076
1077     IDxDiagContainerImpl_Container *cont;
1078
1079     cont = allocate_information_node(DxDiag_SoundDevices);
1080     if (!cont)
1081         return E_OUTOFMEMORY;
1082
1083     add_subcontainer(node, cont);
1084
1085     cont = allocate_information_node(DxDiag_SoundCaptureDevices);
1086     if (!cont)
1087         return E_OUTOFMEMORY;
1088
1089     add_subcontainer(node, cont);
1090
1091     return S_OK;
1092 }
1093
1094 static HRESULT build_directmusic_tree(IDxDiagContainerImpl_Container *node)
1095 {
1096     return S_OK;
1097 }
1098
1099 static HRESULT build_directinput_tree(IDxDiagContainerImpl_Container *node)
1100 {
1101     return S_OK;
1102 }
1103
1104 static HRESULT build_directplay_tree(IDxDiagContainerImpl_Container *node)
1105 {
1106     return S_OK;
1107 }
1108
1109 static HRESULT build_systemdevices_tree(IDxDiagContainerImpl_Container *node)
1110 {
1111     return S_OK;
1112 }
1113
1114 static HRESULT fill_file_description(IDxDiagContainerImpl_Container *node, const WCHAR *szFilePath, const WCHAR *szFileName)
1115 {
1116     static const WCHAR szSlashSep[] = {'\\',0};
1117     static const WCHAR szPath[] = {'s','z','P','a','t','h',0};
1118     static const WCHAR szName[] = {'s','z','N','a','m','e',0};
1119     static const WCHAR szVersion[] = {'s','z','V','e','r','s','i','o','n',0};
1120     static const WCHAR szAttributes[] = {'s','z','A','t','t','r','i','b','u','t','e','s',0};
1121     static const WCHAR szLanguageEnglish[] = {'s','z','L','a','n','g','u','a','g','e','E','n','g','l','i','s','h',0};
1122     static const WCHAR dwFileTimeHigh[] = {'d','w','F','i','l','e','T','i','m','e','H','i','g','h',0};
1123     static const WCHAR dwFileTimeLow[] = {'d','w','F','i','l','e','T','i','m','e','L','o','w',0};
1124     static const WCHAR bBeta[] = {'b','B','e','t','a',0};
1125     static const WCHAR bDebug[] = {'b','D','e','b','u','g',0};
1126     static const WCHAR bExists[] = {'b','E','x','i','s','t','s',0};
1127
1128     /* Values */
1129     static const WCHAR szFinal_Retail_v[] = {'F','i','n','a','l',' ','R','e','t','a','i','l',0};
1130     static const WCHAR szEnglish_v[] = {'E','n','g','l','i','s','h',0};
1131     static const WCHAR szVersionFormat[] = {'%','u','.','%','0','2','u','.','%','0','4','u','.','%','0','4','u',0};
1132
1133     HRESULT hr;
1134     WCHAR *szFile;
1135     WCHAR szVersion_v[1024];
1136     DWORD retval, hdl;
1137     void *pVersionInfo = NULL;
1138     BOOL boolret = FALSE;
1139     UINT uiLength;
1140     VS_FIXEDFILEINFO *pFileInfo;
1141
1142     TRACE("Filling container %p for %s in %s\n", node,
1143           debugstr_w(szFileName), debugstr_w(szFilePath));
1144
1145     szFile = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(szFilePath) +
1146                                             lstrlenW(szFileName) + 2 /* slash + terminator */));
1147     if (!szFile)
1148         return E_OUTOFMEMORY;
1149
1150     lstrcpyW(szFile, szFilePath);
1151     lstrcatW(szFile, szSlashSep);
1152     lstrcatW(szFile, szFileName);
1153
1154     retval = GetFileVersionInfoSizeW(szFile, &hdl);
1155     if (retval)
1156     {
1157         pVersionInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
1158         if (!pVersionInfo)
1159         {
1160             hr = E_OUTOFMEMORY;
1161             goto cleanup;
1162         }
1163
1164         if (GetFileVersionInfoW(szFile, 0, retval, pVersionInfo) &&
1165             VerQueryValueW(pVersionInfo, szSlashSep, (void **)&pFileInfo, &uiLength))
1166             boolret = TRUE;
1167     }
1168
1169     hr = add_bstr_property(node, szPath, szFile);
1170     if (FAILED(hr))
1171         goto cleanup;
1172
1173     hr = add_bstr_property(node, szName, szFileName);
1174     if (FAILED(hr))
1175         goto cleanup;
1176
1177     hr = add_bool_property(node, bExists, boolret);
1178     if (FAILED(hr))
1179         goto cleanup;
1180
1181     if (boolret)
1182     {
1183         snprintfW(szVersion_v, sizeof(szVersion_v)/sizeof(szVersion_v[0]),
1184                   szVersionFormat,
1185                   HIWORD(pFileInfo->dwFileVersionMS),
1186                   LOWORD(pFileInfo->dwFileVersionMS),
1187                   HIWORD(pFileInfo->dwFileVersionLS),
1188                   LOWORD(pFileInfo->dwFileVersionLS));
1189
1190         TRACE("Found version as (%s)\n", debugstr_w(szVersion_v));
1191
1192         hr = add_bstr_property(node, szVersion, szVersion_v);
1193         if (FAILED(hr))
1194             goto cleanup;
1195
1196         hr = add_bstr_property(node, szAttributes, szFinal_Retail_v);
1197         if (FAILED(hr))
1198             goto cleanup;
1199
1200         hr = add_bstr_property(node, szLanguageEnglish, szEnglish_v);
1201         if (FAILED(hr))
1202             goto cleanup;
1203
1204         hr = add_ui4_property(node, dwFileTimeHigh, pFileInfo->dwFileDateMS);
1205         if (FAILED(hr))
1206             goto cleanup;
1207
1208         hr = add_ui4_property(node, dwFileTimeLow, pFileInfo->dwFileDateLS);
1209         if (FAILED(hr))
1210             goto cleanup;
1211
1212         hr = add_bool_property(node, bBeta, ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_PRERELEASE) != 0);
1213         if (FAILED(hr))
1214             goto cleanup;
1215
1216         hr = add_bool_property(node, bDebug, ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_DEBUG) != 0);
1217         if (FAILED(hr))
1218             goto cleanup;
1219     }
1220
1221     hr = S_OK;
1222 cleanup:
1223     HeapFree(GetProcessHeap(), 0, pVersionInfo);
1224     HeapFree(GetProcessHeap(), 0, szFile);
1225
1226     return hr;
1227 }
1228 static HRESULT build_directxfiles_tree(IDxDiagContainerImpl_Container *node)
1229 {
1230     static const WCHAR dlls[][15] =
1231     {
1232         {'d','3','d','8','.','d','l','l',0},
1233         {'d','3','d','9','.','d','l','l',0},
1234         {'d','d','r','a','w','.','d','l','l',0},
1235         {'d','e','v','e','n','u','m','.','d','l','l',0},
1236         {'d','i','n','p','u','t','8','.','d','l','l',0},
1237         {'d','i','n','p','u','t','.','d','l','l',0},
1238         {'d','m','b','a','n','d','.','d','l','l',0},
1239         {'d','m','c','o','m','p','o','s','.','d','l','l',0},
1240         {'d','m','i','m','e','.','d','l','l',0},
1241         {'d','m','l','o','a','d','e','r','.','d','l','l',0},
1242         {'d','m','s','c','r','i','p','t','.','d','l','l',0},
1243         {'d','m','s','t','y','l','e','.','d','l','l',0},
1244         {'d','m','s','y','n','t','h','.','d','l','l',0},
1245         {'d','m','u','s','i','c','.','d','l','l',0},
1246         {'d','p','l','a','y','x','.','d','l','l',0},
1247         {'d','p','n','e','t','.','d','l','l',0},
1248         {'d','s','o','u','n','d','.','d','l','l',0},
1249         {'d','s','w','a','v','e','.','d','l','l',0},
1250         {'d','x','d','i','a','g','n','.','d','l','l',0},
1251         {'q','u','a','r','t','z','.','d','l','l',0}
1252     };
1253
1254     HRESULT hr;
1255     WCHAR szFilePath[MAX_PATH];
1256     INT i;
1257
1258     GetSystemDirectoryW(szFilePath, MAX_PATH);
1259
1260     for (i = 0; i < sizeof(dlls) / sizeof(dlls[0]); i++)
1261     {
1262         static const WCHAR szFormat[] = {'%','d',0};
1263
1264         WCHAR szFileID[5];
1265         IDxDiagContainerImpl_Container *file_container;
1266
1267         snprintfW(szFileID, sizeof(szFileID)/sizeof(szFileID[0]), szFormat, i);
1268
1269         file_container = allocate_information_node(szFileID);
1270         if (!file_container)
1271             return E_OUTOFMEMORY;
1272
1273         hr = fill_file_description(file_container, szFilePath, dlls[i]);
1274         if (FAILED(hr))
1275         {
1276             free_information_tree(file_container);
1277             continue;
1278         }
1279
1280         add_subcontainer(node, file_container);
1281     }
1282
1283     return S_OK;
1284 }
1285
1286 static HRESULT read_property_names(IPropertyBag *pPropBag, VARIANT *friendly_name, VARIANT *clsid_name)
1287 {
1288     static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
1289     static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
1290
1291     HRESULT hr;
1292
1293     VariantInit(friendly_name);
1294     VariantInit(clsid_name);
1295
1296     hr = IPropertyBag_Read(pPropBag, wszFriendlyName, friendly_name, 0);
1297     if (FAILED(hr))
1298         return hr;
1299
1300     hr = IPropertyBag_Read(pPropBag, wszClsidName, clsid_name, 0);
1301     if (FAILED(hr))
1302     {
1303         VariantClear(friendly_name);
1304         return hr;
1305     }
1306
1307     return S_OK;
1308 }
1309
1310 static HRESULT fill_filter_data_information(IDxDiagContainerImpl_Container *subcont, BYTE *pData, ULONG cb)
1311 {
1312     static const WCHAR szVersionW[] = {'s','z','V','e','r','s','i','o','n',0};
1313     static const WCHAR dwInputs[] = {'d','w','I','n','p','u','t','s',0};
1314     static const WCHAR dwOutputs[] = {'d','w','O','u','t','p','u','t','s',0};
1315     static const WCHAR dwMeritW[] = {'d','w','M','e','r','i','t',0};
1316     static const WCHAR szVersionFormat[] = {'v','%','d',0};
1317
1318     HRESULT hr;
1319     IFilterMapper2 *pFileMapper = NULL;
1320     IAMFilterData *pFilterData = NULL;
1321     REGFILTER2 *pRF = NULL;
1322     WCHAR bufferW[10];
1323     ULONG j;
1324     DWORD dwNOutputs = 0;
1325     DWORD dwNInputs = 0;
1326
1327     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC, &IID_IFilterMapper2,
1328                           (void **)&pFileMapper);
1329     if (FAILED(hr))
1330         return hr;
1331
1332     hr = IFilterMapper2_QueryInterface(pFileMapper, &IID_IAMFilterData, (void **)&pFilterData);
1333     if (FAILED(hr))
1334         goto cleanup;
1335
1336     hr = IAMFilterData_ParseFilterData(pFilterData, pData, cb, (BYTE **)&pRF);
1337     if (FAILED(hr))
1338         goto cleanup;
1339
1340     snprintfW(bufferW, sizeof(bufferW)/sizeof(bufferW[0]), szVersionFormat, pRF->dwVersion);
1341     hr = add_bstr_property(subcont, szVersionW, bufferW);
1342     if (FAILED(hr))
1343         goto cleanup;
1344
1345     if (pRF->dwVersion == 1)
1346     {
1347         for (j = 0; j < pRF->u.s.cPins; j++)
1348             if (pRF->u.s.rgPins[j].bOutput)
1349                 dwNOutputs++;
1350             else
1351                 dwNInputs++;
1352     }
1353     else if (pRF->dwVersion == 2)
1354     {
1355         for (j = 0; j < pRF->u.s1.cPins2; j++)
1356             if (pRF->u.s1.rgPins2[j].dwFlags & REG_PINFLAG_B_OUTPUT)
1357                 dwNOutputs++;
1358             else
1359                 dwNInputs++;
1360     }
1361
1362     hr = add_ui4_property(subcont, dwInputs, dwNInputs);
1363     if (FAILED(hr))
1364         goto cleanup;
1365
1366     hr = add_ui4_property(subcont, dwOutputs, dwNOutputs);
1367     if (FAILED(hr))
1368         goto cleanup;
1369
1370     hr = add_ui4_property(subcont, dwMeritW, pRF->dwMerit);
1371     if (FAILED(hr))
1372         goto cleanup;
1373
1374     hr = S_OK;
1375 cleanup:
1376     CoTaskMemFree(pRF);
1377     if (pFilterData) IAMFilterData_Release(pFilterData);
1378     if (pFileMapper) IFilterMapper2_Release(pFileMapper);
1379
1380     return hr;
1381 }
1382
1383 static HRESULT fill_filter_container(IDxDiagContainerImpl_Container *subcont, IMoniker *pMoniker)
1384 {
1385     static const WCHAR szName[] = {'s','z','N','a','m','e',0};
1386     static const WCHAR ClsidFilterW[] = {'C','l','s','i','d','F','i','l','t','e','r',0};
1387     static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
1388
1389     HRESULT hr;
1390     IPropertyBag *pPropFilterBag = NULL;
1391     BYTE *pData;
1392     VARIANT friendly_name;
1393     VARIANT clsid_name;
1394     VARIANT v;
1395
1396     VariantInit(&friendly_name);
1397     VariantInit(&clsid_name);
1398     VariantInit(&v);
1399
1400     hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (void **)&pPropFilterBag);
1401     if (FAILED(hr))
1402         return hr;
1403
1404     hr = read_property_names(pPropFilterBag, &friendly_name, &clsid_name);
1405     if (FAILED(hr))
1406         goto cleanup;
1407
1408     TRACE("Name = %s\n", debugstr_w(V_BSTR(&friendly_name)));
1409     TRACE("CLSID = %s\n", debugstr_w(V_BSTR(&clsid_name)));
1410
1411     hr = add_bstr_property(subcont, szName, V_BSTR(&friendly_name));
1412     if (FAILED(hr))
1413         goto cleanup;
1414
1415     hr = add_bstr_property(subcont, ClsidFilterW, V_BSTR(&clsid_name));
1416     if (FAILED(hr))
1417         goto cleanup;
1418
1419     hr = IPropertyBag_Read(pPropFilterBag, wszFilterDataName, &v, NULL);
1420     if (FAILED(hr))
1421         goto cleanup;
1422
1423     hr = SafeArrayAccessData(V_ARRAY(&v), (void **)&pData);
1424     if (FAILED(hr))
1425         goto cleanup;
1426
1427     hr = fill_filter_data_information(subcont, pData, V_ARRAY(&v)->rgsabound->cElements);
1428     SafeArrayUnaccessData(V_ARRAY(&v));
1429     if (FAILED(hr))
1430         goto cleanup;
1431
1432     hr = S_OK;
1433 cleanup:
1434     VariantClear(&v);
1435     VariantClear(&clsid_name);
1436     VariantClear(&friendly_name);
1437     if (pPropFilterBag) IPropertyBag_Release(pPropFilterBag);
1438
1439     return hr;
1440 }
1441
1442 static HRESULT build_directshowfilters_tree(IDxDiagContainerImpl_Container *node)
1443 {
1444     static const WCHAR szCatName[] = {'s','z','C','a','t','N','a','m','e',0};
1445     static const WCHAR ClsidCatW[] = {'C','l','s','i','d','C','a','t',0};
1446     static const WCHAR szIdFormat[] = {'%','d',0};
1447
1448     HRESULT hr;
1449     int i = 0;
1450     ICreateDevEnum *pCreateDevEnum;
1451     IEnumMoniker *pEmCat = NULL;
1452     IMoniker *pMCat = NULL;
1453         IEnumMoniker *pEnum = NULL;
1454
1455     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
1456                           &IID_ICreateDevEnum, (void **)&pCreateDevEnum);
1457     if (FAILED(hr))
1458         return hr;
1459
1460     hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEmCat, 0);
1461     if (FAILED(hr))
1462         goto cleanup;
1463
1464     while (IEnumMoniker_Next(pEmCat, 1, &pMCat, NULL) == S_OK)
1465     {
1466         VARIANT vCatName;
1467         VARIANT vCatClsid;
1468         IPropertyBag *pPropBag;
1469         CLSID clsidCat;
1470         IMoniker *pMoniker = NULL;
1471
1472         hr = IMoniker_BindToStorage(pMCat, NULL, NULL, &IID_IPropertyBag, (void **)&pPropBag);
1473         if (FAILED(hr))
1474         {
1475             IMoniker_Release(pMCat);
1476             break;
1477         }
1478
1479         hr = read_property_names(pPropBag, &vCatName, &vCatClsid);
1480         IPropertyBag_Release(pPropBag);
1481         if (FAILED(hr))
1482         {
1483             IMoniker_Release(pMCat);
1484             break;
1485         }
1486
1487         hr = CLSIDFromString(V_BSTR(&vCatClsid), &clsidCat);
1488         if (FAILED(hr))
1489         {
1490             IMoniker_Release(pMCat);
1491             VariantClear(&vCatClsid);
1492             VariantClear(&vCatName);
1493             break;
1494         }
1495
1496         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
1497         if (hr != S_OK)
1498         {
1499             IMoniker_Release(pMCat);
1500             VariantClear(&vCatClsid);
1501             VariantClear(&vCatName);
1502             continue;
1503         }
1504
1505         TRACE("Enumerating class %s\n", debugstr_guid(&clsidCat));
1506
1507         while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
1508         {
1509             WCHAR bufferW[10];
1510             IDxDiagContainerImpl_Container *subcont;
1511
1512             snprintfW(bufferW, sizeof(bufferW)/sizeof(bufferW[0]), szIdFormat, i);
1513             subcont = allocate_information_node(bufferW);
1514             if (!subcont)
1515             {
1516                 hr = E_OUTOFMEMORY;
1517                 IMoniker_Release(pMoniker);
1518                 break;
1519             }
1520
1521             hr = add_bstr_property(subcont, szCatName, V_BSTR(&vCatName));
1522             if (FAILED(hr))
1523             {
1524                 free_information_tree(subcont);
1525                 IMoniker_Release(pMoniker);
1526                 break;
1527             }
1528
1529             hr = add_bstr_property(subcont, ClsidCatW, V_BSTR(&vCatClsid));
1530             if (FAILED(hr))
1531             {
1532                 free_information_tree(subcont);
1533                 IMoniker_Release(pMoniker);
1534                 break;
1535             }
1536
1537             hr = fill_filter_container(subcont, pMoniker);
1538             if (FAILED(hr))
1539             {
1540                 free_information_tree(subcont);
1541                 IMoniker_Release(pMoniker);
1542                 break;
1543             }
1544
1545             add_subcontainer(node, subcont);
1546             i++;
1547             IMoniker_Release(pMoniker);
1548         }
1549
1550         IEnumMoniker_Release(pEnum);
1551         IMoniker_Release(pMCat);
1552         VariantClear(&vCatClsid);
1553         VariantClear(&vCatName);
1554
1555         if (FAILED(hr))
1556             break;
1557     }
1558
1559 cleanup:
1560     if (pEmCat) IEnumMoniker_Release(pEmCat);
1561     ICreateDevEnum_Release(pCreateDevEnum);
1562     return hr;
1563 }
1564
1565 static HRESULT build_logicaldisks_tree(IDxDiagContainerImpl_Container *node)
1566 {
1567     return S_OK;
1568 }
1569
1570 static HRESULT build_information_tree(IDxDiagContainerImpl_Container **pinfo_root)
1571 {
1572     static const WCHAR DxDiag_SystemInfo[] = {'D','x','D','i','a','g','_','S','y','s','t','e','m','I','n','f','o',0};
1573     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};
1574     static const WCHAR DxDiag_DirectSound[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','S','o','u','n','d',0};
1575     static const WCHAR DxDiag_DirectMusic[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','M','u','s','i','c',0};
1576     static const WCHAR DxDiag_DirectInput[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','I','n','p','u','t',0};
1577     static const WCHAR DxDiag_DirectPlay[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','P','l','a','y',0};
1578     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};
1579     static const WCHAR DxDiag_DirectXFiles[] = {'D','x','D','i','a','g','_','D','i','r','e','c','t','X','F','i','l','e','s',0};
1580     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};
1581     static const WCHAR DxDiag_LogicalDisks[] = {'D','x','D','i','a','g','_','L','o','g','i','c','a','l','D','i','s','k','s',0};
1582
1583     static const struct
1584     {
1585         const WCHAR *name;
1586         HRESULT (*initfunc)(IDxDiagContainerImpl_Container *);
1587     } root_children[] =
1588     {
1589         {DxDiag_SystemInfo, build_systeminfo_tree},
1590         {DxDiag_DisplayDevices, build_displaydevices_tree},
1591         {DxDiag_DirectSound, build_directsound_tree},
1592         {DxDiag_DirectMusic, build_directmusic_tree},
1593         {DxDiag_DirectInput, build_directinput_tree},
1594         {DxDiag_DirectPlay, build_directplay_tree},
1595         {DxDiag_SystemDevices, build_systemdevices_tree},
1596         {DxDiag_DirectXFiles, build_directxfiles_tree},
1597         {DxDiag_DirectShowFilters, build_directshowfilters_tree},
1598         {DxDiag_LogicalDisks, build_logicaldisks_tree},
1599     };
1600
1601     IDxDiagContainerImpl_Container *info_root;
1602     size_t index;
1603
1604     info_root = allocate_information_node(NULL);
1605     if (!info_root)
1606         return E_OUTOFMEMORY;
1607
1608     for (index = 0; index < sizeof(root_children)/sizeof(root_children[0]); index++)
1609     {
1610         IDxDiagContainerImpl_Container *node;
1611         HRESULT hr;
1612
1613         node = allocate_information_node(root_children[index].name);
1614         if (!node)
1615         {
1616             free_information_tree(info_root);
1617             return E_OUTOFMEMORY;
1618         }
1619
1620         hr = root_children[index].initfunc(node);
1621         if (FAILED(hr))
1622         {
1623             free_information_tree(node);
1624             free_information_tree(info_root);
1625             return hr;
1626         }
1627
1628         add_subcontainer(info_root, node);
1629     }
1630
1631     *pinfo_root = info_root;
1632     return S_OK;
1633 }