provider.c 52.4 KB
Newer Older
1 2 3
/* 
 * IDxDiagProvider Implementation
 * 
4
 * Copyright 2004-2005 Raphael Junqueira
5
 * Copyright 2010 Andrew Nguyen
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 24

#define COBJMACROS
25
#define NONAMELESSUNION
26
#define NONAMELESSSTRUCT
27 28 29
#include "dxdiag_private.h"
#include "winver.h"
#include "objidl.h"
30
#include "uuids.h"
31 32
#include "vfw.h"
#include "mmddk.h"
33
#include "d3d9.h"
34 35
#include "strmif.h"
#include "initguid.h"
36
#include "wine/fil_data.h"
37
#include "psapi.h"
38
#include "wbemcli.h"
39
#include "dsound.h"
40

41 42
#include "wine/debug.h"

43 44
WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);

45 46 47
static HRESULT build_information_tree(IDxDiagContainerImpl_Container **pinfo_root);
static void free_information_tree(IDxDiagContainerImpl_Container *node);

48 49 50 51 52 53 54 55 56 57 58 59 60 61
struct IDxDiagProviderImpl
{
  IDxDiagProvider IDxDiagProvider_iface;
  LONG ref;
  BOOL init;
  DXDIAG_INIT_PARAMS params;
  IDxDiagContainerImpl_Container *info_root;
};

static inline IDxDiagProviderImpl *impl_from_IDxDiagProvider(IDxDiagProvider *iface)
{
     return CONTAINING_RECORD(iface, IDxDiagProviderImpl, IDxDiagProvider_iface);
}

62
/* IDxDiagProvider IUnknown parts follow: */
63 64
static HRESULT WINAPI IDxDiagProviderImpl_QueryInterface(IDxDiagProvider *iface, REFIID riid,
        void **ppobj)
65
{
66
    IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
67

68 69
    if (!ppobj) return E_INVALIDARG;

70 71
    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IDxDiagProvider)) {
72
        IUnknown_AddRef(iface);
73 74 75 76 77
        *ppobj = This;
        return S_OK;
    }

    WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
78
    *ppobj = NULL;
79 80 81
    return E_NOINTERFACE;
}

82 83 84
static ULONG WINAPI IDxDiagProviderImpl_AddRef(IDxDiagProvider *iface)
{
    IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
85 86
    ULONG refCount = InterlockedIncrement(&This->ref);

87
    TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
88

89 90
    DXDIAGN_LockModule();

91
    return refCount;
92 93
}

94 95 96
static ULONG WINAPI IDxDiagProviderImpl_Release(IDxDiagProvider *iface)
{
    IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
97 98
    ULONG refCount = InterlockedDecrement(&This->ref);

99
    TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
100 101

    if (!refCount) {
102
        free_information_tree(This->info_root);
103 104
        HeapFree(GetProcessHeap(), 0, This);
    }
105 106 107

    DXDIAGN_UnlockModule();
    
108
    return refCount;
109 110 111
}

/* IDxDiagProvider Interface follow: */
112 113 114 115
static HRESULT WINAPI IDxDiagProviderImpl_Initialize(IDxDiagProvider *iface,
        DXDIAG_INIT_PARAMS *pParams)
{
    IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
116 117
    HRESULT hr;

118 119 120 121 122
    TRACE("(%p,%p)\n", iface, pParams);

    if (NULL == pParams) {
      return E_POINTER;
    }
123 124
    if (pParams->dwSize != sizeof(DXDIAG_INIT_PARAMS) ||
        pParams->dwDxDiagHeaderVersion != DXDIAG_DX9_SDK_VERSION) {
125 126 127
      return E_INVALIDARG;
    }

128 129 130 131 132 133 134
    if (!This->info_root)
    {
        hr = build_information_tree(&This->info_root);
        if (FAILED(hr))
            return hr;
    }

135 136 137
    This->init = TRUE;
    memcpy(&This->params, pParams, pParams->dwSize);
    return S_OK;
138 139
}

140 141 142 143
static HRESULT WINAPI IDxDiagProviderImpl_GetRootContainer(IDxDiagProvider *iface,
        IDxDiagContainer **ppInstance)
{
  IDxDiagProviderImpl *This = impl_from_IDxDiagProvider(iface);
144

145 146 147
  TRACE("(%p,%p)\n", iface, ppInstance);

  if (FALSE == This->init) {
148
    return CO_E_NOTINITIALIZED;
149
  }
150

151
  return DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, This->info_root,
152
          &This->IDxDiagProvider_iface, (void **)ppInstance);
153 154
}

155
static const IDxDiagProviderVtbl DxDiagProvider_Vtbl =
156 157 158 159 160 161 162 163 164 165 166 167
{
    IDxDiagProviderImpl_QueryInterface,
    IDxDiagProviderImpl_AddRef,
    IDxDiagProviderImpl_Release,
    IDxDiagProviderImpl_Initialize,
    IDxDiagProviderImpl_GetRootContainer
};

HRESULT DXDiag_CreateDXDiagProvider(LPCLASSFACTORY iface, LPUNKNOWN punkOuter, REFIID riid, LPVOID *ppobj) {
  IDxDiagProviderImpl* provider;

  TRACE("(%p, %s, %p)\n", punkOuter, debugstr_guid(riid), ppobj);
168 169 170 171

  *ppobj = NULL;
  if (punkOuter) return CLASS_E_NOAGGREGATION;

172
  provider = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagProviderImpl));
173
  if (NULL == provider) return E_OUTOFMEMORY;
174
  provider->IDxDiagProvider_iface.lpVtbl = &DxDiagProvider_Vtbl;
175
  provider->ref = 0; /* will be inited with QueryInterface */
176
  return IDxDiagProviderImpl_QueryInterface(&provider->IDxDiagProvider_iface, riid, ppobj);
177 178
}

179 180 181 182 183 184 185
static void free_property_information(IDxDiagContainerImpl_Property *prop)
{
    VariantClear(&prop->vProp);
    HeapFree(GetProcessHeap(), 0, prop->propName);
    HeapFree(GetProcessHeap(), 0, prop);
}

186 187 188 189 190 191 192 193 194 195 196
static void free_information_tree(IDxDiagContainerImpl_Container *node)
{
    IDxDiagContainerImpl_Container *ptr, *cursor2;

    if (!node)
        return;

    HeapFree(GetProcessHeap(), 0, node->contName);

    LIST_FOR_EACH_ENTRY_SAFE(ptr, cursor2, &node->subContainers, IDxDiagContainerImpl_Container, entry)
    {
197 198 199 200 201 202 203 204
        IDxDiagContainerImpl_Property *prop, *prop_cursor2;

        LIST_FOR_EACH_ENTRY_SAFE(prop, prop_cursor2, &ptr->properties, IDxDiagContainerImpl_Property, entry)
        {
            list_remove(&prop->entry);
            free_property_information(prop);
        }

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        list_remove(&ptr->entry);
        free_information_tree(ptr);
    }

    HeapFree(GetProcessHeap(), 0, node);
}

static IDxDiagContainerImpl_Container *allocate_information_node(const WCHAR *name)
{
    IDxDiagContainerImpl_Container *ret;

    ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
    if (!ret)
        return NULL;

    if (name)
    {
222
        ret->contName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name) + 1) * sizeof(*name));
223 224 225 226 227
        if (!ret->contName)
        {
            HeapFree(GetProcessHeap(), 0, ret);
            return NULL;
        }
228
        lstrcpyW(ret->contName, name);
229 230 231 232 233 234 235 236
    }

    list_init(&ret->subContainers);
    list_init(&ret->properties);

    return ret;
}

237 238 239 240 241 242 243 244
static IDxDiagContainerImpl_Property *allocate_property_information(const WCHAR *name)
{
    IDxDiagContainerImpl_Property *ret;

    ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
    if (!ret)
        return NULL;

245
    ret->propName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name) + 1) * sizeof(*name));
246 247 248 249 250
    if (!ret->propName)
    {
        HeapFree(GetProcessHeap(), 0, ret);
        return NULL;
    }
251
    lstrcpyW(ret->propName, name);
252 253 254 255

    return ret;
}

256 257 258 259 260 261
static inline void add_subcontainer(IDxDiagContainerImpl_Container *node, IDxDiagContainerImpl_Container *subCont)
{
    list_add_tail(&node->subContainers, &subCont->entry);
    ++node->nSubContainers;
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static inline HRESULT add_bstr_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, const WCHAR *str)
{
    IDxDiagContainerImpl_Property *prop;
    BSTR bstr;

    prop = allocate_property_information(propName);
    if (!prop)
        return E_OUTOFMEMORY;

    bstr = SysAllocString(str);
    if (!bstr)
    {
        free_property_information(prop);
        return E_OUTOFMEMORY;
    }

    V_VT(&prop->vProp) = VT_BSTR;
    V_BSTR(&prop->vProp) = bstr;

    list_add_tail(&node->properties, &prop->entry);
    ++node->nProperties;

    return S_OK;
}

static inline HRESULT add_ui4_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, DWORD data)
{
    IDxDiagContainerImpl_Property *prop;

    prop = allocate_property_information(propName);
    if (!prop)
        return E_OUTOFMEMORY;

    V_VT(&prop->vProp) = VT_UI4;
    V_UI4(&prop->vProp) = data;

    list_add_tail(&node->properties, &prop->entry);
    ++node->nProperties;

    return S_OK;
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
static inline HRESULT add_i4_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, LONG data)
{
    IDxDiagContainerImpl_Property *prop;

    prop = allocate_property_information(propName);
    if (!prop)
        return E_OUTOFMEMORY;

    V_VT(&prop->vProp) = VT_I4;
    V_I4(&prop->vProp) = data;

    list_add_tail(&node->properties, &prop->entry);
    ++node->nProperties;

    return S_OK;
}

321 322 323 324 325 326 327 328 329
static inline HRESULT add_bool_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, BOOL data)
{
    IDxDiagContainerImpl_Property *prop;

    prop = allocate_property_information(propName);
    if (!prop)
        return E_OUTOFMEMORY;

    V_VT(&prop->vProp) = VT_BOOL;
330
    V_BOOL(&prop->vProp) = data ? VARIANT_TRUE : VARIANT_FALSE;
331 332 333 334 335 336 337 338 339 340

    list_add_tail(&node->properties, &prop->entry);
    ++node->nProperties;

    return S_OK;
}

static inline HRESULT add_ull_as_bstr_property(IDxDiagContainerImpl_Container *node, const WCHAR *propName, ULONGLONG data )
{
    IDxDiagContainerImpl_Property *prop;
341
    HRESULT hr;
342 343 344 345 346 347 348 349

    prop = allocate_property_information(propName);
    if (!prop)
        return E_OUTOFMEMORY;

    V_VT(&prop->vProp) = VT_UI8;
    V_UI8(&prop->vProp) = data;

350 351 352 353 354 355
    hr = VariantChangeType(&prop->vProp, &prop->vProp, 0, VT_BSTR);
    if (FAILED(hr))
    {
        free_property_information(prop);
        return hr;
    }
356 357 358 359 360 361 362

    list_add_tail(&node->properties, &prop->entry);
    ++node->nProperties;

    return S_OK;
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
/* Copied from programs/taskkill/taskkill.c. */
static DWORD *enumerate_processes(DWORD *list_count)
{
    DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;

    pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
    if (!pid_list)
        return NULL;

    for (;;)
    {
        DWORD *realloc_list;

        if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
        {
            HeapFree(GetProcessHeap(), 0, pid_list);
            return NULL;
        }

        /* EnumProcesses can't signal an insufficient buffer condition, so the
         * only way to possibly determine whether a larger buffer is required
         * is to see whether the written number of bytes is the same as the
         * buffer size. If so, the buffer will be reallocated to twice the
         * size. */
        if (alloc_bytes != needed_bytes)
            break;

        alloc_bytes *= 2;
        realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
        if (!realloc_list)
        {
            HeapFree(GetProcessHeap(), 0, pid_list);
            return NULL;
        }
        pid_list = realloc_list;
    }

    *list_count = needed_bytes / sizeof(*pid_list);
    return pid_list;
}

/* Copied from programs/taskkill/taskkill.c. */
static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
{
    HANDLE process;
    HMODULE module;
    DWORD required_size;

    process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
    if (!process)
        return FALSE;

    if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
    {
        CloseHandle(process);
        return FALSE;
    }

    if (!GetModuleBaseNameW(process, module, buf, chars))
    {
        CloseHandle(process);
        return FALSE;
    }

    CloseHandle(process);
    return TRUE;
}

/* dxdiagn's detection scheme is simply to look for a process called conf.exe. */
static BOOL is_netmeeting_running(void)
{
    DWORD list_count;
    DWORD *pid_list = enumerate_processes(&list_count);

    if (pid_list)
    {
        DWORD i;
        WCHAR process_name[MAX_PATH];

        for (i = 0; i < list_count; i++)
        {
444
            if (get_process_name_from_pid(pid_list[i], process_name, ARRAY_SIZE(process_name)) &&
445
                    !lstrcmpW(L"conf.exe", process_name))
446 447
            {
                HeapFree(GetProcessHeap(), 0, pid_list);
448
                return TRUE;
449
            }
450
        }
451
        HeapFree(GetProcessHeap(), 0, pid_list);
452 453 454 455 456
    }

    return FALSE;
}

457 458 459 460 461 462
static HRESULT fill_language_information(IDxDiagContainerImpl_Container *node)
{
    WCHAR system_lang[80], regional_setting[100], user_lang[80], language_str[300];
    HRESULT hr;

    /* szLanguagesLocalized */
463 464 465
    GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SNATIVELANGNAME, system_lang, ARRAY_SIZE(system_lang));
    LoadStringW(dxdiagn_instance, IDS_REGIONAL_SETTING, regional_setting, ARRAY_SIZE(regional_setting));
    GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SNATIVELANGNAME, user_lang, ARRAY_SIZE(user_lang));
466

467 468
    swprintf(language_str, ARRAY_SIZE(language_str), L"%s (%s: %s)", system_lang, regional_setting,
            user_lang);
469

470
    hr = add_bstr_property(node, L"szLanguagesLocalized", language_str);
471 472 473 474
    if (FAILED(hr))
        return hr;

    /* szLanguagesEnglish */
475 476
    GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, system_lang, ARRAY_SIZE(system_lang));
    GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, user_lang, ARRAY_SIZE(user_lang));
477

478 479
    swprintf(language_str, ARRAY_SIZE(language_str), L"%s (%s: %s)", system_lang,
            L"Regional Setting", user_lang);
480

481
    hr = add_bstr_property(node, L"szLanguagesEnglish", language_str);
482 483 484 485 486 487
    if (FAILED(hr))
        return hr;

    return S_OK;
}

488 489 490 491 492 493 494 495
static HRESULT fill_datetime_information(IDxDiagContainerImpl_Container *node)
{
    SYSTEMTIME curtime;
    WCHAR date_str[80], time_str[80], datetime_str[200];
    HRESULT hr;

    GetLocalTime(&curtime);

496
    GetTimeFormatW(LOCALE_NEUTRAL, 0, &curtime, L"HH':'mm':'ss", time_str, ARRAY_SIZE(time_str));
497 498

    /* szTimeLocalized */
499
    GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &curtime, NULL, date_str, ARRAY_SIZE(date_str));
500

501
    swprintf(datetime_str, ARRAY_SIZE(datetime_str), L"%s, %s", date_str, time_str);
502

503
    hr = add_bstr_property(node, L"szTimeLocalized", datetime_str);
504 505 506 507
    if (FAILED(hr))
        return hr;

    /* szTimeEnglish */
508
    GetDateFormatW(LOCALE_NEUTRAL, 0, &curtime, L"M'/'d'/'yyyy", date_str, ARRAY_SIZE(date_str));
509

510
    swprintf(datetime_str, ARRAY_SIZE(datetime_str), L"%s, %s", date_str, time_str);
511

512
    hr = add_bstr_property(node, L"szTimeEnglish", datetime_str);
513 514 515 516 517 518
    if (FAILED(hr))
        return hr;

    return S_OK;
}

519 520
static HRESULT fill_os_string_information(IDxDiagContainerImpl_Container *node, OSVERSIONINFOW *info)
{
521 522 523 524 525
    static const WCHAR *prop_list[] =
    {
        L"szOSLocalized", L"szOSExLocalized", L"szOSExLongLocalized",
        L"szOSEnglish", L"szOSExEnglish", L"szOSExLongEnglish"
    };
526 527 528 529 530
    size_t i;
    HRESULT hr;

    /* FIXME: OS detection should be performed, and localized OS strings
     * should contain translated versions of the "build" phrase. */
531
    for (i = 0; i < ARRAY_SIZE(prop_list); i++)
532
    {
533
        hr = add_bstr_property(node, prop_list[i], L"Windows XP Professional");
534 535 536 537 538 539 540
        if (FAILED(hr))
            return hr;
    }

    return S_OK;
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static HRESULT fill_processor_information(IDxDiagContainerImpl_Container *node)
{
    IWbemLocator *wbem_locator;
    IWbemServices *wbem_service;
    IWbemClassObject *wbem_class;
    IEnumWbemClassObject *wbem_enum;
    VARIANT cpu_name, cpu_no, clock_speed;
    WCHAR print_buf[200];
    BSTR bstr;
    ULONG no;
    HRESULT hr;

    hr = CoCreateInstance(&CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (void**)&wbem_locator);
    if(FAILED(hr))
        return hr;

557
    bstr = SysAllocString(L"\\\\.\\root\\cimv2");
558 559 560 561 562 563 564 565 566 567
    if(!bstr) {
        IWbemLocator_Release(wbem_locator);
        return E_OUTOFMEMORY;
    }
    hr = IWbemLocator_ConnectServer(wbem_locator, bstr, NULL, NULL, NULL, 0, NULL, NULL, &wbem_service);
    IWbemLocator_Release(wbem_locator);
    SysFreeString(bstr);
    if(FAILED(hr))
        return hr;

568
    bstr = SysAllocString(L"Win32_Processor");
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    if(!bstr) {
        IWbemServices_Release(wbem_service);
        return E_OUTOFMEMORY;
    }
    hr = IWbemServices_CreateInstanceEnum(wbem_service, bstr, WBEM_FLAG_SYSTEM_ONLY, NULL, &wbem_enum);
    IWbemServices_Release(wbem_service);
    SysFreeString(bstr);
    if(FAILED(hr))
        return hr;

    hr = IEnumWbemClassObject_Next(wbem_enum, 1000, 1, &wbem_class, &no);
    IEnumWbemClassObject_Release(wbem_enum);
    if(FAILED(hr))
        return hr;

584
    hr = IWbemClassObject_Get(wbem_class, L"NumberOfLogicalProcessors", 0, &cpu_no, NULL, NULL);
585 586 587 588
    if(FAILED(hr)) {
        IWbemClassObject_Release(wbem_class);
        return hr;
    }
589
    hr = IWbemClassObject_Get(wbem_class, L"MaxClockSpeed", 0, &clock_speed, NULL, NULL);
590 591 592 593
    if(FAILED(hr)) {
        IWbemClassObject_Release(wbem_class);
        return hr;
    }
594
    hr = IWbemClassObject_Get(wbem_class, L"Name", 0, &cpu_name, NULL, NULL);
595 596 597 598
    IWbemClassObject_Release(wbem_class);
    if(FAILED(hr))
        return hr;

599
    swprintf(print_buf, ARRAY_SIZE(print_buf), L"%s(%d CPUs), ~%dMHz",
600
             V_BSTR(&cpu_name), V_I4(&cpu_no), V_I4(&clock_speed));
601 602 603 604
    VariantClear(&cpu_name);
    VariantClear(&cpu_no);
    VariantClear(&clock_speed);

605
    return add_bstr_property(node, L"szProcessorEnglish", print_buf);
606 607
}

608 609
static HRESULT build_systeminfo_tree(IDxDiagContainerImpl_Container *node)
{
610 611 612
    HRESULT hr;
    MEMORYSTATUSEX msex;
    OSVERSIONINFOW info;
613 614
    DWORD count, usedpage_mb, availpage_mb;
    WCHAR buffer[MAX_PATH], computer_name[MAX_COMPUTERNAME_LENGTH + 1], print_buf[200], localized_pagefile_fmt[200];
615
    DWORD_PTR args[2];
616

617
    hr = add_ui4_property(node, L"dwDirectXVersionMajor", 9);
618 619 620
    if (FAILED(hr))
        return hr;

621
    hr = add_ui4_property(node, L"dwDirectXVersionMinor", 0);
622 623 624
    if (FAILED(hr))
        return hr;

625
    hr = add_bstr_property(node, L"szDirectXVersionLetter", L"c");
626 627 628
    if (FAILED(hr))
        return hr;

629
    hr = add_bstr_property(node, L"szDirectXVersionEnglish", L"4.09.0000.0904");
630 631 632
    if (FAILED(hr))
        return hr;

633
    hr = add_bstr_property(node, L"szDirectXVersionLongEnglish", L"= \"DirectX 9.0c (4.09.0000.0904)");
634 635 636
    if (FAILED(hr))
        return hr;

637
    hr = add_bool_property(node, L"bDebug", FALSE);
638 639 640
    if (FAILED(hr))
        return hr;

641
    hr = add_bool_property(node, L"bNECPC98", FALSE);
642 643 644
    if (FAILED(hr))
        return hr;

645 646 647
    msex.dwLength = sizeof(msex);
    GlobalMemoryStatusEx(&msex);

648
    hr = add_ull_as_bstr_property(node, L"ullPhysicalMemory", msex.ullTotalPhys);
649 650 651
    if (FAILED(hr))
        return hr;

652
    hr = add_ull_as_bstr_property(node, L"ullUsedPageFile", msex.ullTotalPageFile - msex.ullAvailPageFile);
653 654 655
    if (FAILED(hr))
        return hr;

656
    hr = add_ull_as_bstr_property(node, L"ullAvailPageFile", msex.ullAvailPageFile);
657 658 659
    if (FAILED(hr))
        return hr;

660
    hr = add_bool_property(node, L"bNetMeetingRunning", is_netmeeting_running());
661 662 663
    if (FAILED(hr))
        return hr;

664 665 666
    info.dwOSVersionInfoSize = sizeof(info);
    GetVersionExW(&info);

667
    hr = add_ui4_property(node, L"dwOSMajorVersion", info.dwMajorVersion);
668 669 670
    if (FAILED(hr))
        return hr;

671
    hr = add_ui4_property(node, L"dwOSMinorVersion", info.dwMinorVersion);
672 673 674
    if (FAILED(hr))
        return hr;

675
    hr = add_ui4_property(node, L"dwOSBuildNumber", info.dwBuildNumber);
676 677 678
    if (FAILED(hr))
        return hr;

679
    hr = add_ui4_property(node, L"dwOSPlatformID", info.dwPlatformId);
680 681 682
    if (FAILED(hr))
        return hr;

683
    hr = add_bstr_property(node, L"szCSDVersion", info.szCSDVersion);
684 685 686
    if (FAILED(hr))
        return hr;

687
    /* FIXME: Roundoff should not be done with truncated division. */
688
    swprintf(print_buf, ARRAY_SIZE(print_buf), L"%I64uMB RAM", msex.ullTotalPhys / (1024 * 1024));
689
    hr = add_bstr_property(node, L"szPhysicalMemoryEnglish", print_buf);
690 691 692
    if (FAILED(hr))
        return hr;

693 694
    usedpage_mb = (DWORD)((msex.ullTotalPageFile - msex.ullAvailPageFile) / (1024 * 1024));
    availpage_mb = (DWORD)(msex.ullAvailPageFile / (1024 * 1024));
695 696
    LoadStringW(dxdiagn_instance, IDS_PAGE_FILE_FORMAT, localized_pagefile_fmt,
                ARRAY_SIZE(localized_pagefile_fmt));
697 698
    args[0] = usedpage_mb;
    args[1] = availpage_mb;
699 700
    FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY, localized_pagefile_fmt,
                   0, 0, print_buf, ARRAY_SIZE(print_buf), (__ms_va_list*)args);
701

702
    hr = add_bstr_property(node, L"szPageFileLocalized", print_buf);
703 704 705
    if (FAILED(hr))
        return hr;

706
    swprintf(print_buf, ARRAY_SIZE(print_buf), L"%uMB used, %uMB available", usedpage_mb, availpage_mb);
707

708
    hr = add_bstr_property(node, L"szPageFileEnglish", print_buf);
709 710 711
    if (FAILED(hr))
        return hr;

712 713
    GetWindowsDirectoryW(buffer, MAX_PATH);

714
    hr = add_bstr_property(node, L"szWindowsDir", buffer);
715 716 717
    if (FAILED(hr))
        return hr;

718
    count = ARRAY_SIZE(computer_name);
719 720 721
    if (!GetComputerNameW(computer_name, &count))
        return E_FAIL;

722
    hr = add_bstr_property(node, L"szMachineNameLocalized", computer_name);
723 724 725
    if (FAILED(hr))
        return hr;

726
    hr = add_bstr_property(node, L"szMachineNameEnglish", computer_name);
727 728 729
    if (FAILED(hr))
        return hr;

730
    hr = add_bstr_property(node, L"szSystemManufacturerEnglish", L"");
731 732 733
    if (FAILED(hr))
        return hr;

734
    hr = add_bstr_property(node, L"szSystemModelEnglish", L"");
735 736 737
    if (FAILED(hr))
        return hr;

738
    hr = add_bstr_property(node, L"szBIOSEnglish", L"");
739 740 741
    if (FAILED(hr))
        return hr;

742
    hr = fill_processor_information(node);
743 744 745
    if (FAILED(hr))
        return hr;

746
    hr = add_bstr_property(node, L"szSetupParamEnglish", L"Not present");
747 748 749
    if (FAILED(hr))
        return hr;

750
    hr = add_bstr_property(node, L"szDxDiagVersion", L"");
751 752 753
    if (FAILED(hr))
        return hr;

754 755 756 757
    hr = fill_language_information(node);
    if (FAILED(hr))
        return hr;

758 759 760 761
    hr = fill_datetime_information(node);
    if (FAILED(hr))
        return hr;

762 763 764 765
    hr = fill_os_string_information(node, &info);
    if (FAILED(hr))
        return hr;

766 767 768
    return S_OK;
}

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
/* The logic from pixelformat_for_depth() in dlls/wined3d/utils.c is reversed. */
static DWORD depth_for_pixelformat(D3DFORMAT format)
{
    switch (format)
    {
    case D3DFMT_P8: return 8;
    case D3DFMT_X1R5G5B5: return 15;
    case D3DFMT_R5G6B5: return 16;
    /* This case will fail to distinguish an original bpp of 24. */
    case D3DFMT_X8R8G8B8: return 32;
    default:
        FIXME("Unknown D3DFORMAT %d, returning 32 bpp\n", format);
        return 32;
    }
}

static BOOL get_texture_memory(GUID *adapter, DWORD *available_mem)
{
    IDirectDraw7 *pDirectDraw;
    HRESULT hr;
    DDSCAPS2 dd_caps;

    hr = DirectDrawCreateEx(adapter, (void **)&pDirectDraw, &IID_IDirectDraw7, NULL);
    if (SUCCEEDED(hr))
    {
        dd_caps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
795
        dd_caps.dwCaps2 = dd_caps.dwCaps3 = dd_caps.u1.dwCaps4 = 0;
796 797 798 799 800 801 802 803 804
        hr = IDirectDraw7_GetAvailableVidMem(pDirectDraw, &dd_caps, available_mem, NULL);
        IDirectDraw7_Release(pDirectDraw);
        if (SUCCEEDED(hr))
            return TRUE;
    }

    return FALSE;
}

805 806
static const WCHAR *vendor_id_to_manufacturer_string(DWORD vendor_id)
{
807 808 809 810 811 812 813
    unsigned int i;
    static const struct
    {
        DWORD id;
        const WCHAR *name;
    }
    vendors[] =
814
    {
815 816 817 818 819
        {0x1002, L"ATI Technologies Inc."},
        {0x10de, L"NVIDIA"},
        {0x15ad, L"VMware"},
        {0x1af4, L"Red Hat"},
        {0x8086, L"Intel Corporation"},
820 821
    };

822
    for (i = 0; i < ARRAY_SIZE(vendors); ++i)
823
    {
824 825
        if (vendors[i].id == vendor_id)
            return vendors[i].name;
826
    }
827

828
    FIXME("Unknown PCI vendor ID 0x%04lx.\n", vendor_id);
829

830
    return L"Unknown";
831 832
}

833
static HRESULT fill_display_information_d3d(IDxDiagContainerImpl_Container *node)
834
{
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    IDxDiagContainerImpl_Container *display_adapter;
    HRESULT hr;
    IDirect3D9 *pDirect3D9;
    WCHAR buffer[256];
    UINT index, count;

    pDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);
    if (!pDirect3D9)
        return E_FAIL;

    count = IDirect3D9_GetAdapterCount(pDirect3D9);
    for (index = 0; index < count; index++)
    {
        D3DADAPTER_IDENTIFIER9 adapter_info;
        D3DDISPLAYMODE adapter_mode;
850
        D3DCAPS9 device_caps;
851
        DWORD available_mem = 0;
852
        BOOL hardware_accel;
853

854
        swprintf(buffer, ARRAY_SIZE(buffer), L"%u", index);
855 856 857 858 859 860 861 862 863 864 865 866
        display_adapter = allocate_information_node(buffer);
        if (!display_adapter)
        {
            hr = E_OUTOFMEMORY;
            goto cleanup;
        }

        add_subcontainer(node, display_adapter);

        hr = IDirect3D9_GetAdapterIdentifier(pDirect3D9, index, 0, &adapter_info);
        if (SUCCEEDED(hr))
        {
867
            WCHAR driverW[sizeof(adapter_info.Driver)];
868
            WCHAR descriptionW[sizeof(adapter_info.Description)];
869
            WCHAR devicenameW[sizeof(adapter_info.DeviceName)];
870

871 872 873 874 875
            MultiByteToWideChar(CP_ACP, 0, adapter_info.Driver, -1, driverW, ARRAY_SIZE(driverW));
            MultiByteToWideChar(CP_ACP, 0, adapter_info.Description, -1, descriptionW,
                                ARRAY_SIZE(descriptionW));
            MultiByteToWideChar(CP_ACP, 0, adapter_info.DeviceName, -1, devicenameW,
                                ARRAY_SIZE(devicenameW));
876

877
            hr = add_bstr_property(display_adapter, L"szDriverName", driverW);
878 879 880
            if (FAILED(hr))
                goto cleanup;

881
            hr = add_bstr_property(display_adapter, L"szDescription", descriptionW);
882 883 884
            if (FAILED(hr))
                goto cleanup;

885
            hr = add_bstr_property(display_adapter, L"szDeviceName", devicenameW);
886 887 888
            if (FAILED(hr))
                goto cleanup;

889 890 891
            swprintf(buffer, ARRAY_SIZE(buffer), L"%u.%u.%04u.%04u",
                    HIWORD(adapter_info.DriverVersion.u.HighPart), LOWORD(adapter_info.DriverVersion.u.HighPart),
                    HIWORD(adapter_info.DriverVersion.u.LowPart), LOWORD(adapter_info.DriverVersion.u.LowPart));
892

893
            hr = add_bstr_property(display_adapter, L"szDriverVersion", buffer);
894 895 896
            if (FAILED(hr))
                goto cleanup;

897 898
            swprintf(buffer, ARRAY_SIZE(buffer), L"0x%04x", adapter_info.VendorId);
            hr = add_bstr_property(display_adapter, L"szVendorId", buffer);
899 900 901
            if (FAILED(hr))
                goto cleanup;

902 903
            swprintf(buffer, ARRAY_SIZE(buffer), L"0x%04x", adapter_info.DeviceId);
            hr = add_bstr_property(display_adapter, L"szDeviceId", buffer);
904 905
            if (FAILED(hr))
                goto cleanup;
906

907 908
            swprintf(buffer, ARRAY_SIZE(buffer), L"0x%08x", adapter_info.SubSysId);
            hr = add_bstr_property(display_adapter, L"szSubSysId", buffer);
909 910 911
            if (FAILED(hr))
                goto cleanup;

912 913
            swprintf(buffer, ARRAY_SIZE(buffer), L"0x%04x", adapter_info.Revision);
            hr = add_bstr_property(display_adapter, L"szRevisionId", buffer);
914 915 916 917
            if (FAILED(hr))
                goto cleanup;

            StringFromGUID2(&adapter_info.DeviceIdentifier, buffer, 39);
918
            hr = add_bstr_property(display_adapter, L"szDeviceIdentifier", buffer);
919 920 921
            if (FAILED(hr))
                goto cleanup;

922 923
            hr = add_bstr_property(display_adapter, L"szManufacturer",
                    vendor_id_to_manufacturer_string(adapter_info.VendorId));
924 925
            if (FAILED(hr))
                goto cleanup;
926 927 928 929 930
        }

        hr = IDirect3D9_GetAdapterDisplayMode(pDirect3D9, index, &adapter_mode);
        if (SUCCEEDED(hr))
        {
931
            hr = add_ui4_property(display_adapter, L"dwWidth", adapter_mode.Width);
932 933 934
            if (FAILED(hr))
                goto cleanup;

935
            hr = add_ui4_property(display_adapter, L"dwHeight", adapter_mode.Height);
936 937 938
            if (FAILED(hr))
                goto cleanup;

939
            hr = add_ui4_property(display_adapter, L"dwRefreshRate", adapter_mode.RefreshRate);
940 941 942
            if (FAILED(hr))
                goto cleanup;

943
            hr = add_ui4_property(display_adapter, L"dwBpp", depth_for_pixelformat(adapter_mode.Format));
944 945
            if (FAILED(hr))
                goto cleanup;
946

947 948 949
            swprintf(buffer, ARRAY_SIZE(buffer), L"%d x %d (%d bit) (%dHz)", adapter_mode.Width,
                    adapter_mode.Height, depth_for_pixelformat(adapter_mode.Format),
                    adapter_mode.RefreshRate);
950

951
            hr = add_bstr_property(display_adapter, L"szDisplayModeLocalized", buffer);
952 953 954
            if (FAILED(hr))
                goto cleanup;

955
            hr = add_bstr_property(display_adapter, L"szDisplayModeEnglish", buffer);
956 957
            if (FAILED(hr))
                goto cleanup;
958 959
        }

960
        hr = add_bstr_property(display_adapter, L"szKeyDeviceKey", L"");
961 962 963
        if (FAILED(hr))
            goto cleanup;

964
        hr = add_bstr_property(display_adapter, L"szKeyDeviceID", L"");
965 966 967
        if (FAILED(hr))
            goto cleanup;

968
        hr = add_bstr_property(display_adapter, L"szChipType", L"");
969 970 971
        if (FAILED(hr))
            goto cleanup;

972
        hr = add_bstr_property(display_adapter, L"szDACType", L"");
973 974 975
        if (FAILED(hr))
            goto cleanup;

976
        hr = add_bstr_property(display_adapter, L"szRevision", L"");
977 978 979
        if (FAILED(hr))
            goto cleanup;

980 981 982
        if (!get_texture_memory(&adapter_info.DeviceIdentifier, &available_mem))
            WARN("get_texture_memory helper failed\n");

983
        swprintf(buffer, ARRAY_SIZE(buffer), L"%.1f MB", available_mem / 1000000.0f);
984

985
        hr = add_bstr_property(display_adapter, L"szDisplayMemoryLocalized", buffer);
986 987 988
        if (FAILED(hr))
            goto cleanup;

989
        hr = add_bstr_property(display_adapter, L"szDisplayMemoryEnglish", buffer);
990 991
        if (FAILED(hr))
            goto cleanup;
992 993 994 995

        hr = IDirect3D9_GetDeviceCaps(pDirect3D9, index, D3DDEVTYPE_HAL, &device_caps);
        hardware_accel = SUCCEEDED(hr);

996
        hr = add_bool_property(display_adapter, L"b3DAccelerationEnabled", hardware_accel);
997 998 999
        if (FAILED(hr))
            goto cleanup;

1000
        hr = add_bool_property(display_adapter, L"b3DAccelerationExists", hardware_accel);
1001 1002 1003
        if (FAILED(hr))
            goto cleanup;

1004
        hr = add_bool_property(display_adapter, L"bDDAccelerationEnabled", hardware_accel);
1005 1006
        if (FAILED(hr))
            goto cleanup;
1007

1008
        hr = add_bool_property(display_adapter, L"bNoHardware", FALSE);
1009 1010
        if (FAILED(hr))
            goto cleanup;
1011

1012
        hr = add_bool_property(display_adapter, L"bCanRenderWindow", TRUE);
1013 1014 1015
        if (FAILED(hr))
            goto cleanup;

1016
        hr = add_bstr_property(display_adapter, L"szMonitorName", L"Generic PnP Monitor");
1017 1018 1019
        if (FAILED(hr))
            goto cleanup;

1020
        hr = add_bstr_property(display_adapter, L"szMonitorMaxRes", L"Failed to get parameter");
1021 1022 1023
        if (FAILED(hr))
            goto cleanup;

1024
        hr = add_bstr_property(display_adapter, L"szDriverAttributes", L"Final Retail");
1025 1026 1027
        if (FAILED(hr))
            goto cleanup;

1028
        hr = add_bstr_property(display_adapter, L"szDriverLanguageEnglish", L"English");
1029 1030 1031
        if (FAILED(hr))
            goto cleanup;

1032
        hr = add_bstr_property(display_adapter, L"szDriverLanguageLocalized", L"English");
1033 1034 1035
        if (FAILED(hr))
            goto cleanup;

1036
        hr = add_bstr_property(display_adapter, L"szDriverDateEnglish", L"1/1/2016 10:00:00");
1037 1038 1039
        if (FAILED(hr))
            goto cleanup;

1040
        hr = add_bstr_property(display_adapter, L"szDriverDateLocalized", L"1/1/2016 10:00:00 AM");
1041 1042 1043
        if (FAILED(hr))
            goto cleanup;

1044
        hr = add_i4_property(display_adapter, L"lDriverSize", 10 * 1024 * 1024);
1045 1046 1047
        if (FAILED(hr))
            goto cleanup;

1048
        hr = add_bstr_property(display_adapter, L"szMiniVdd", L"n/a");
1049 1050 1051
        if (FAILED(hr))
            goto cleanup;

1052
        hr = add_bstr_property(display_adapter, L"szMiniVddDateLocalized", L"n/a");
1053 1054 1055
        if (FAILED(hr))
            goto cleanup;

1056
        hr = add_bstr_property(display_adapter, L"szMiniVddDateEnglish", L"n/a");
1057 1058 1059
        if (FAILED(hr))
            goto cleanup;

1060
        hr = add_i4_property(display_adapter, L"lMiniVddSize", 0);
1061 1062 1063
        if (FAILED(hr))
            goto cleanup;

1064
        hr = add_bstr_property(display_adapter, L"szVdd", L"n/a");
1065 1066 1067
        if (FAILED(hr))
            goto cleanup;

1068
        hr = add_bool_property(display_adapter, L"bDriverBeta", FALSE);
1069 1070 1071
        if (FAILED(hr))
            goto cleanup;

1072
        hr = add_bool_property(display_adapter, L"bDriverDebug", FALSE);
1073 1074 1075
        if (FAILED(hr))
            goto cleanup;

1076
        hr = add_bool_property(display_adapter, L"bDriverSigned", TRUE);
1077 1078 1079
        if (FAILED(hr))
            goto cleanup;

1080
        hr = add_bool_property(display_adapter, L"bDriverSignedValid", TRUE);
1081 1082 1083
        if (FAILED(hr))
            goto cleanup;

1084
        hr = add_bstr_property(display_adapter, L"szDriverSignDate", L"n/a");
1085 1086 1087
        if (FAILED(hr))
            goto cleanup;

1088
        hr = add_ui4_property(display_adapter, L"dwDDIVersion", 11);
1089 1090 1091
        if (FAILED(hr))
            goto cleanup;

1092
        hr = add_bstr_property(display_adapter, L"szDDIVersionEnglish", L"11");
1093 1094 1095
        if (FAILED(hr))
            goto cleanup;

1096
        hr = add_bstr_property(display_adapter, L"szDDIVersionLocalized", L"11");
1097 1098 1099
        if (FAILED(hr))
            goto cleanup;

1100
        hr = add_ui4_property(display_adapter, L"iAdapter", index);
1101 1102 1103
        if (FAILED(hr))
            goto cleanup;

1104
        hr = add_ui4_property(display_adapter, L"dwWHQLLevel", 0);
1105 1106
        if (FAILED(hr))
            goto cleanup;
1107 1108 1109 1110 1111 1112 1113
    }

    hr = S_OK;
cleanup:
    IDirect3D9_Release(pDirect3D9);
    return hr;
}
1114

1115
static HRESULT fill_display_information_fallback(IDxDiagContainerImpl_Container *node)
1116
{
1117 1118 1119 1120 1121 1122
    static const WCHAR *empty_properties[] =
    {
        L"szDeviceIdentifier", L"szVendorId", L"szDeviceId", L"szKeyDeviceKey",
        L"szKeyDeviceID", L"szDriverName", L"szDriverVersion", L"szSubSysId",
        L"szRevisionId", L"szManufacturer", L"szChipType", L"szDACType", L"szRevision"
    };
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

    IDxDiagContainerImpl_Container *display_adapter;
    HRESULT hr;
    IDirectDraw7 *pDirectDraw;
    DDSCAPS2 dd_caps;
    DISPLAY_DEVICEW disp_dev;
    DDSURFACEDESC2 surface_descr;
    DWORD tmp;
    WCHAR buffer[256];

1133
    display_adapter = allocate_information_node(L"0");
1134 1135 1136 1137 1138 1139 1140 1141
    if (!display_adapter)
        return E_OUTOFMEMORY;

    add_subcontainer(node, display_adapter);

    disp_dev.cb = sizeof(disp_dev);
    if (EnumDisplayDevicesW( NULL, 0, &disp_dev, 0 ))
    {
1142
        hr = add_bstr_property(display_adapter, L"szDeviceName", disp_dev.DeviceName);
1143 1144 1145
        if (FAILED(hr))
            return hr;

1146
        hr = add_bstr_property(display_adapter, L"szDescription", disp_dev.DeviceString);
1147 1148 1149 1150
        if (FAILED(hr))
            return hr;
    }

1151 1152
    /* Silently ignore a failure from DirectDrawCreateEx. */
    hr = DirectDrawCreateEx(NULL, (void **)&pDirectDraw, &IID_IDirectDraw7, NULL);
1153 1154 1155 1156
    if (FAILED(hr))
        return S_OK;

    dd_caps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1157
    dd_caps.dwCaps2 = dd_caps.dwCaps3 = dd_caps.u1.dwCaps4 = 0;
1158 1159 1160
    hr = IDirectDraw7_GetAvailableVidMem(pDirectDraw, &dd_caps, &tmp, NULL);
    if (SUCCEEDED(hr))
    {
1161
        swprintf(buffer, ARRAY_SIZE(buffer), L"%.1f MB", tmp / 1000000.0f);
1162

1163
        hr = add_bstr_property(display_adapter, L"szDisplayMemoryLocalized", buffer);
1164 1165 1166
        if (FAILED(hr))
            goto cleanup;

1167
        hr = add_bstr_property(display_adapter, L"szDisplayMemoryEnglish", buffer);
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        if (FAILED(hr))
            goto cleanup;
    }

    surface_descr.dwSize = sizeof(surface_descr);
    hr = IDirectDraw7_GetDisplayMode(pDirectDraw, &surface_descr);
    if (SUCCEEDED(hr))
    {
        if (surface_descr.dwFlags & DDSD_WIDTH)
        {
1178
            hr = add_ui4_property(display_adapter, L"dwWidth", surface_descr.dwWidth);
1179 1180 1181 1182 1183 1184
            if (FAILED(hr))
                goto cleanup;
        }

        if (surface_descr.dwFlags & DDSD_HEIGHT)
        {
1185
            hr = add_ui4_property(display_adapter, L"dwHeight", surface_descr.dwHeight);
1186 1187 1188 1189 1190 1191
            if (FAILED(hr))
                goto cleanup;
        }

        if (surface_descr.dwFlags & DDSD_PIXELFORMAT)
        {
1192 1193
            hr = add_ui4_property(display_adapter, L"dwBpp",
                    surface_descr.u4.ddpfPixelFormat.u1.dwRGBBitCount);
1194 1195 1196 1197 1198
            if (FAILED(hr))
                goto cleanup;
        }
    }

1199
    hr = add_ui4_property(display_adapter, L"dwRefreshRate", 60);
1200 1201 1202
    if (FAILED(hr))
        goto cleanup;

1203
    for (tmp = 0; tmp < ARRAY_SIZE(empty_properties); tmp++)
1204
    {
1205
        hr = add_bstr_property(display_adapter, empty_properties[tmp], L"");
1206 1207 1208
        if (FAILED(hr))
            goto cleanup;
    }
1209 1210 1211 1212 1213

    hr = S_OK;
cleanup:
    IDirectDraw7_Release(pDirectDraw);
    return hr;
1214 1215
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
static HRESULT build_displaydevices_tree(IDxDiagContainerImpl_Container *node)
{
    HRESULT hr;

    /* Try to use Direct3D to obtain the required information first. */
    hr = fill_display_information_d3d(node);
    if (hr != E_FAIL)
        return hr;

    return fill_display_information_fallback(node);
}

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
struct enum_context
{
    IDxDiagContainerImpl_Container *cont;
    HRESULT hr;
    int index;
};

static LPWSTR guid_to_string(LPWSTR lpwstr, REFGUID lpcguid)
{
    wsprintfW(lpwstr, L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", lpcguid->Data1, lpcguid->Data2,
        lpcguid->Data3, lpcguid->Data4[0], lpcguid->Data4[1], lpcguid->Data4[2], lpcguid->Data4[3], lpcguid->Data4[4],
        lpcguid->Data4[5], lpcguid->Data4[6], lpcguid->Data4[7]);

    return lpwstr;
}

BOOL CALLBACK dsound_enum(LPGUID guid, LPCWSTR desc, LPCWSTR module, LPVOID context)
{
    struct enum_context *enum_ctx = context;
    IDxDiagContainerImpl_Container *device;
    WCHAR buffer[256];
    const WCHAR *p, *name;

    /* the default device is enumerated twice, one time without GUID */
    if (!guid) return TRUE;

    swprintf(buffer, ARRAY_SIZE(buffer), L"%u", enum_ctx->index);
    device = allocate_information_node(buffer);
    if (!device)
    {
        enum_ctx->hr = E_OUTOFMEMORY;
        return FALSE;
    }

    add_subcontainer(enum_ctx->cont, device);

    guid_to_string(buffer, guid);
    enum_ctx->hr = add_bstr_property(device, L"szGuidDeviceID", buffer);
    if (FAILED(enum_ctx->hr))
        return FALSE;

1269
    enum_ctx->hr = add_bstr_property(device, L"szDescription", desc);
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    if (FAILED(enum_ctx->hr))
        return FALSE;

    enum_ctx->hr = add_bstr_property(device, L"szDriverPath", module);
    if (FAILED(enum_ctx->hr))
        return FALSE;

    name = module;
    if ((p = wcsrchr(name, '\\'))) name = p + 1;
    if ((p = wcsrchr(name, '/'))) name = p + 1;

1281
    enum_ctx->hr = add_bstr_property(device, L"szDriverName", name);
1282 1283 1284 1285 1286 1287 1288
    if (FAILED(enum_ctx->hr))
        return FALSE;

    enum_ctx->index++;
    return TRUE;
}

1289 1290
static HRESULT build_directsound_tree(IDxDiagContainerImpl_Container *node)
{
1291
    struct enum_context enum_ctx;
1292 1293
    IDxDiagContainerImpl_Container *cont;

1294
    cont = allocate_information_node(L"DxDiag_SoundDevices");
1295 1296 1297 1298 1299
    if (!cont)
        return E_OUTOFMEMORY;

    add_subcontainer(node, cont);

1300 1301 1302 1303 1304 1305 1306 1307
    enum_ctx.cont = cont;
    enum_ctx.hr = S_OK;
    enum_ctx.index = 0;

    DirectSoundEnumerateW(dsound_enum, &enum_ctx);
    if (FAILED(enum_ctx.hr))
        return enum_ctx.hr;

1308
    cont = allocate_information_node(L"DxDiag_SoundCaptureDevices");
1309 1310 1311 1312 1313
    if (!cont)
        return E_OUTOFMEMORY;

    add_subcontainer(node, cont);

1314 1315 1316 1317 1318 1319 1320 1321
    enum_ctx.cont = cont;
    enum_ctx.hr = S_OK;
    enum_ctx.index = 0;

    DirectSoundCaptureEnumerateW(dsound_enum, &enum_ctx);
    if (FAILED(enum_ctx.hr))
        return enum_ctx.hr;

1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
    return S_OK;
}

static HRESULT build_directmusic_tree(IDxDiagContainerImpl_Container *node)
{
    return S_OK;
}

static HRESULT build_directinput_tree(IDxDiagContainerImpl_Container *node)
{
    return S_OK;
}

static HRESULT build_directplay_tree(IDxDiagContainerImpl_Container *node)
{
    return S_OK;
}

static HRESULT build_systemdevices_tree(IDxDiagContainerImpl_Container *node)
{
    return S_OK;
}

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
static HRESULT fill_file_description(IDxDiagContainerImpl_Container *node, const WCHAR *szFilePath, const WCHAR *szFileName)
{
    HRESULT hr;
    WCHAR *szFile;
    WCHAR szVersion_v[1024];
    DWORD retval, hdl;
    void *pVersionInfo = NULL;
    BOOL boolret = FALSE;
    UINT uiLength;
    VS_FIXEDFILEINFO *pFileInfo;

1356 1357 1358
    TRACE("Filling container %p for %s in %s\n", node,
          debugstr_w(szFileName), debugstr_w(szFilePath));

1359 1360 1361 1362 1363 1364
    szFile = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(szFilePath) +
                                            lstrlenW(szFileName) + 2 /* slash + terminator */));
    if (!szFile)
        return E_OUTOFMEMORY;

    lstrcpyW(szFile, szFilePath);
1365
    lstrcatW(szFile, L"\\");
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    lstrcatW(szFile, szFileName);

    retval = GetFileVersionInfoSizeW(szFile, &hdl);
    if (retval)
    {
        pVersionInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
        if (!pVersionInfo)
        {
            hr = E_OUTOFMEMORY;
            goto cleanup;
        }

        if (GetFileVersionInfoW(szFile, 0, retval, pVersionInfo) &&
1379
            VerQueryValueW(pVersionInfo, L"\\", (void **)&pFileInfo, &uiLength))
1380 1381 1382
            boolret = TRUE;
    }

1383
    hr = add_bstr_property(node, L"szPath", szFile);
1384 1385 1386
    if (FAILED(hr))
        goto cleanup;

1387
    hr = add_bstr_property(node, L"szName", szFileName);
1388 1389 1390
    if (FAILED(hr))
        goto cleanup;

1391
    hr = add_bool_property(node, L"bExists", boolret);
1392 1393 1394 1395 1396
    if (FAILED(hr))
        goto cleanup;

    if (boolret)
    {
1397 1398 1399
        swprintf(szVersion_v, ARRAY_SIZE(szVersion_v), L"%u.%02u.%04u.%04u",
                HIWORD(pFileInfo->dwFileVersionMS), LOWORD(pFileInfo->dwFileVersionMS),
                HIWORD(pFileInfo->dwFileVersionLS), LOWORD(pFileInfo->dwFileVersionLS));
1400

1401 1402
        TRACE("Found version as (%s)\n", debugstr_w(szVersion_v));

1403
        hr = add_bstr_property(node, L"szVersion", szVersion_v);
1404 1405 1406
        if (FAILED(hr))
            goto cleanup;

1407
        hr = add_bstr_property(node, L"szAttributes", L"Final Retail");
1408 1409 1410
        if (FAILED(hr))
            goto cleanup;

1411
        hr = add_bstr_property(node, L"szLanguageEnglish", L"English");
1412 1413 1414
        if (FAILED(hr))
            goto cleanup;

1415
        hr = add_ui4_property(node, L"dwFileTimeHigh", pFileInfo->dwFileDateMS);
1416 1417 1418
        if (FAILED(hr))
            goto cleanup;

1419
        hr = add_ui4_property(node, L"dwFileTimeLow", pFileInfo->dwFileDateLS);
1420 1421 1422
        if (FAILED(hr))
            goto cleanup;

1423 1424
        hr = add_bool_property(node, L"bBeta",
                ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_PRERELEASE) != 0);
1425 1426 1427
        if (FAILED(hr))
            goto cleanup;

1428 1429
        hr = add_bool_property(node, L"bDebug",
                ((pFileInfo->dwFileFlags & pFileInfo->dwFileFlagsMask) & VS_FF_DEBUG) != 0);
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
        if (FAILED(hr))
            goto cleanup;
    }

    hr = S_OK;
cleanup:
    HeapFree(GetProcessHeap(), 0, pVersionInfo);
    HeapFree(GetProcessHeap(), 0, szFile);

    return hr;
}
1441 1442
static HRESULT build_directxfiles_tree(IDxDiagContainerImpl_Container *node)
{
1443 1444
    static const WCHAR dlls[][15] =
    {
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
        L"d3d8.dll",
        L"d3d9.dll",
        L"ddraw.dll",
        L"devenum.dll",
        L"dinput8.dll",
        L"dinput.dll",
        L"dmband.dll",
        L"dmcompos.dll",
        L"dmime.dll",
        L"dmloader.dll",
        L"dmscript.dll",
        L"dmstyle.dll",
        L"dmsynth.dll",
        L"dmusic.dll",
        L"dplayx.dll",
        L"dpnet.dll",
        L"dsound.dll",
        L"dswave.dll",
        L"dxdiagn.dll",
        L"quartz.dll"
1465 1466 1467 1468 1469 1470 1471 1472
    };

    HRESULT hr;
    WCHAR szFilePath[MAX_PATH];
    INT i;

    GetSystemDirectoryW(szFilePath, MAX_PATH);

1473
    for (i = 0; i < ARRAY_SIZE(dlls); i++)
1474 1475 1476 1477
    {
        WCHAR szFileID[5];
        IDxDiagContainerImpl_Container *file_container;

1478
        swprintf(szFileID, ARRAY_SIZE(szFileID), L"%d", i);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493

        file_container = allocate_information_node(szFileID);
        if (!file_container)
            return E_OUTOFMEMORY;

        hr = fill_file_description(file_container, szFilePath, dlls[i]);
        if (FAILED(hr))
        {
            free_information_tree(file_container);
            continue;
        }

        add_subcontainer(node, file_container);
    }

1494 1495 1496
    return S_OK;
}

1497
static HRESULT read_property_names(IPropertyBag *pPropBag, VARIANT *friendly_name, VARIANT *clsid_name)
1498
{
1499 1500 1501 1502 1503
    HRESULT hr;

    VariantInit(friendly_name);
    VariantInit(clsid_name);

1504
    hr = IPropertyBag_Read(pPropBag, L"FriendlyName", friendly_name, 0);
1505 1506 1507
    if (FAILED(hr))
        return hr;

1508
    hr = IPropertyBag_Read(pPropBag, L"CLSID", clsid_name, 0);
1509 1510 1511 1512 1513 1514
    if (FAILED(hr))
    {
        VariantClear(friendly_name);
        return hr;
    }

1515 1516 1517
    return S_OK;
}

1518 1519 1520 1521 1522
static HRESULT fill_filter_data_information(IDxDiagContainerImpl_Container *subcont, BYTE *pData, ULONG cb)
{
    HRESULT hr;
    IFilterMapper2 *pFileMapper = NULL;
    IAMFilterData *pFilterData = NULL;
1523
    BYTE *ppRF = NULL;
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    REGFILTER2 *pRF = NULL;
    WCHAR bufferW[10];
    ULONG j;
    DWORD dwNOutputs = 0;
    DWORD dwNInputs = 0;

    hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC, &IID_IFilterMapper2,
                          (void **)&pFileMapper);
    if (FAILED(hr))
        return hr;

    hr = IFilterMapper2_QueryInterface(pFileMapper, &IID_IAMFilterData, (void **)&pFilterData);
    if (FAILED(hr))
        goto cleanup;

1539
    hr = IAMFilterData_ParseFilterData(pFilterData, pData, cb, &ppRF);
1540 1541
    if (FAILED(hr))
        goto cleanup;
1542
    pRF = ((REGFILTER2**)ppRF)[0];
1543

1544 1545
    swprintf(bufferW, ARRAY_SIZE(bufferW), L"v%d", pRF->dwVersion);
    hr = add_bstr_property(subcont, L"szVersion", bufferW);
1546 1547 1548 1549 1550
    if (FAILED(hr))
        goto cleanup;

    if (pRF->dwVersion == 1)
    {
1551 1552
        for (j = 0; j < pRF->cPins; j++)
            if (pRF->rgPins[j].bOutput)
1553 1554 1555 1556 1557 1558
                dwNOutputs++;
            else
                dwNInputs++;
    }
    else if (pRF->dwVersion == 2)
    {
1559 1560
        for (j = 0; j < pRF->cPins2; j++)
            if (pRF->rgPins2[j].dwFlags & REG_PINFLAG_B_OUTPUT)
1561 1562 1563 1564 1565
                dwNOutputs++;
            else
                dwNInputs++;
    }

1566
    hr = add_ui4_property(subcont, L"dwInputs", dwNInputs);
1567 1568 1569
    if (FAILED(hr))
        goto cleanup;

1570
    hr = add_ui4_property(subcont, L"dwOutputs", dwNOutputs);
1571 1572 1573
    if (FAILED(hr))
        goto cleanup;

1574
    hr = add_ui4_property(subcont, L"dwMerit", pRF->dwMerit);
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
    if (FAILED(hr))
        goto cleanup;

    hr = S_OK;
cleanup:
    CoTaskMemFree(pRF);
    if (pFilterData) IAMFilterData_Release(pFilterData);
    if (pFileMapper) IFilterMapper2_Release(pFileMapper);

    return hr;
}

static HRESULT fill_filter_container(IDxDiagContainerImpl_Container *subcont, IMoniker *pMoniker)
{
    HRESULT hr;
    IPropertyBag *pPropFilterBag = NULL;
    BYTE *pData;
    VARIANT friendly_name;
    VARIANT clsid_name;
    VARIANT v;

    VariantInit(&friendly_name);
    VariantInit(&clsid_name);
    VariantInit(&v);

    hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (void **)&pPropFilterBag);
    if (FAILED(hr))
        return hr;

    hr = read_property_names(pPropFilterBag, &friendly_name, &clsid_name);
    if (FAILED(hr))
        goto cleanup;

1608 1609 1610
    TRACE("Name = %s\n", debugstr_w(V_BSTR(&friendly_name)));
    TRACE("CLSID = %s\n", debugstr_w(V_BSTR(&clsid_name)));

1611
    hr = add_bstr_property(subcont, L"szName", V_BSTR(&friendly_name));
1612 1613 1614
    if (FAILED(hr))
        goto cleanup;

1615
    hr = add_bstr_property(subcont, L"ClsidFilter", V_BSTR(&clsid_name));
1616 1617 1618
    if (FAILED(hr))
        goto cleanup;

1619
    hr = IPropertyBag_Read(pPropFilterBag, L"FilterData", &v, NULL);
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
    if (FAILED(hr))
        goto cleanup;

    hr = SafeArrayAccessData(V_ARRAY(&v), (void **)&pData);
    if (FAILED(hr))
        goto cleanup;

    hr = fill_filter_data_information(subcont, pData, V_ARRAY(&v)->rgsabound->cElements);
    SafeArrayUnaccessData(V_ARRAY(&v));
    if (FAILED(hr))
        goto cleanup;

    hr = S_OK;
cleanup:
    VariantClear(&v);
    VariantClear(&clsid_name);
    VariantClear(&friendly_name);
    if (pPropFilterBag) IPropertyBag_Release(pPropFilterBag);

    return hr;
}

static HRESULT build_directshowfilters_tree(IDxDiagContainerImpl_Container *node)
{
    HRESULT hr;
    int i = 0;
    ICreateDevEnum *pCreateDevEnum;
    IEnumMoniker *pEmCat = NULL;
    IMoniker *pMCat = NULL;
	IEnumMoniker *pEnum = NULL;

    hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
                          &IID_ICreateDevEnum, (void **)&pCreateDevEnum);
    if (FAILED(hr))
        return hr;

    hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEmCat, 0);
    if (FAILED(hr))
        goto cleanup;

    while (IEnumMoniker_Next(pEmCat, 1, &pMCat, NULL) == S_OK)
    {
        VARIANT vCatName;
        VARIANT vCatClsid;
        IPropertyBag *pPropBag;
        CLSID clsidCat;
        IMoniker *pMoniker = NULL;

        hr = IMoniker_BindToStorage(pMCat, NULL, NULL, &IID_IPropertyBag, (void **)&pPropBag);
        if (FAILED(hr))
        {
            IMoniker_Release(pMCat);
            break;
        }

        hr = read_property_names(pPropBag, &vCatName, &vCatClsid);
        IPropertyBag_Release(pPropBag);
        if (FAILED(hr))
        {
            IMoniker_Release(pMCat);
            break;
        }

        hr = CLSIDFromString(V_BSTR(&vCatClsid), &clsidCat);
        if (FAILED(hr))
        {
            IMoniker_Release(pMCat);
            VariantClear(&vCatClsid);
            VariantClear(&vCatName);
            break;
        }

        hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
        if (hr != S_OK)
        {
            IMoniker_Release(pMCat);
            VariantClear(&vCatClsid);
            VariantClear(&vCatName);
            continue;
        }

1701 1702
        TRACE("Enumerating class %s\n", debugstr_guid(&clsidCat));

1703 1704 1705 1706 1707
        while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
        {
            WCHAR bufferW[10];
            IDxDiagContainerImpl_Container *subcont;

1708
            swprintf(bufferW, ARRAY_SIZE(bufferW), L"%d", i);
1709 1710 1711 1712 1713 1714 1715 1716
            subcont = allocate_information_node(bufferW);
            if (!subcont)
            {
                hr = E_OUTOFMEMORY;
                IMoniker_Release(pMoniker);
                break;
            }

1717
            hr = add_bstr_property(subcont, L"szCatName", V_BSTR(&vCatName));
1718 1719 1720 1721 1722 1723 1724
            if (FAILED(hr))
            {
                free_information_tree(subcont);
                IMoniker_Release(pMoniker);
                break;
            }

1725
            hr = add_bstr_property(subcont, L"ClsidCat", V_BSTR(&vCatClsid));
1726 1727 1728 1729 1730 1731 1732 1733
            if (FAILED(hr))
            {
                free_information_tree(subcont);
                IMoniker_Release(pMoniker);
                break;
            }

            hr = fill_filter_container(subcont, pMoniker);
1734
            IMoniker_Release(pMoniker);
1735 1736
            if (FAILED(hr))
            {
1737
                WARN("Skipping invalid filter\n");
1738
                free_information_tree(subcont);
1739 1740
                hr = S_OK;
                continue;
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
            }

            add_subcontainer(node, subcont);
            i++;
        }

        IEnumMoniker_Release(pEnum);
        IMoniker_Release(pMCat);
        VariantClear(&vCatClsid);
        VariantClear(&vCatName);

        if (FAILED(hr))
            break;
    }

cleanup:
    if (pEmCat) IEnumMoniker_Release(pEmCat);
    ICreateDevEnum_Release(pCreateDevEnum);
    return hr;
}

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
static HRESULT build_logicaldisks_tree(IDxDiagContainerImpl_Container *node)
{
    return S_OK;
}

static HRESULT build_information_tree(IDxDiagContainerImpl_Container **pinfo_root)
{
    static const struct
    {
        const WCHAR *name;
        HRESULT (*initfunc)(IDxDiagContainerImpl_Container *);
    } root_children[] =
    {
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
        {L"DxDiag_SystemInfo", build_systeminfo_tree},
        {L"DxDiag_DisplayDevices", build_displaydevices_tree},
        {L"DxDiag_DirectSound", build_directsound_tree},
        {L"DxDiag_DirectMusic", build_directmusic_tree},
        {L"DxDiag_DirectInput", build_directinput_tree},
        {L"DxDiag_DirectPlay", build_directplay_tree},
        {L"DxDiag_SystemDevices", build_systemdevices_tree},
        {L"DxDiag_DirectXFiles", build_directxfiles_tree},
        {L"DxDiag_DirectShowFilters", build_directshowfilters_tree},
        {L"DxDiag_LogicalDisks", build_logicaldisks_tree},
1785 1786 1787 1788 1789 1790 1791 1792 1793
    };

    IDxDiagContainerImpl_Container *info_root;
    size_t index;

    info_root = allocate_information_node(NULL);
    if (!info_root)
        return E_OUTOFMEMORY;

1794
    for (index = 0; index < ARRAY_SIZE(root_children); index++)
1795 1796
    {
        IDxDiagContainerImpl_Container *node;
1797
        HRESULT hr;
1798 1799 1800 1801 1802 1803 1804 1805

        node = allocate_information_node(root_children[index].name);
        if (!node)
        {
            free_information_tree(info_root);
            return E_OUTOFMEMORY;
        }

1806 1807 1808 1809 1810 1811 1812
        hr = root_children[index].initfunc(node);
        if (FAILED(hr))
        {
            free_information_tree(node);
            free_information_tree(info_root);
            return hr;
        }
1813 1814 1815 1816 1817 1818 1819

        add_subcontainer(info_root, node);
    }

    *pinfo_root = info_root;
    return S_OK;
}