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