File: main.c

package info (click to toggle)
wine 10.0~repack-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 325,920 kB
  • sloc: ansic: 4,156,003; perl: 23,800; yacc: 22,031; javascript: 15,872; makefile: 12,346; pascal: 9,519; objc: 6,923; lex: 5,273; xml: 3,219; python: 2,673; cpp: 1,741; sh: 893; java: 750; asm: 299; cs: 62
file content (389 lines) | stat: -rw-r--r-- 13,897 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
/*
 * Copyright 2014 Austin English
 * Copyright 2023 Hans Leidekker for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS

#include "initguid.h"
#include "objidl.h"
#include "wbemcli.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(systeminfo);

enum format_flags
{
    FORMAT_STRING,
    FORMAT_DATE,
    FORMAT_LOCALE,
    FORMAT_SIZE,
};

struct sysinfo
{
    const WCHAR *item;
    const WCHAR *class;
    const WCHAR *property; /* hardcoded value if class is NULL */
    void (*callback)( IWbemServices *services, enum format_flags flags, UINT32 ); /* called if class and property are NULL */
    enum format_flags flags;
};

static void output_processors( IWbemServices *services, enum format_flags flags, UINT32 width )
{
    IEnumWbemClassObject *iter;
    IWbemClassObject *obj;
    DWORD i, num_cpus = 0, count;
    VARIANT value;
    BSTR str;
    HRESULT hr;

    str = SysAllocString( L"Win32_Processor" );
    hr = IWbemServices_CreateInstanceEnum( services, str, 0, NULL, &iter );
    SysFreeString( str );
    if (FAILED( hr )) return;

    while (IEnumWbemClassObject_Skip( iter, WBEM_INFINITE, 1 ) == S_OK) num_cpus++;

    fwprintf( stdout, L"Processor(s):%*s %u Processor(s) Installed.\n", width - wcslen(L"Processor(s)"), L" ", num_cpus );
    IEnumWbemClassObject_Reset( iter );

    for (i = 0; i < num_cpus; i++)
    {
        hr = IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
        if (FAILED( hr )) goto done;

        hr = IWbemClassObject_Get( obj, L"Caption", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        fwprintf( stdout, L"%*s[%02u]: %s", width + 2, L" ", i + 1, V_BSTR(&value) );
        VariantClear( &value );

        hr = IWbemClassObject_Get( obj, L"Manufacturer", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        fwprintf( stdout, L" %s", V_BSTR(&value) );
        VariantClear( &value );

        hr = IWbemClassObject_Get( obj, L"MaxClockSpeed", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        fwprintf( stdout, L" ~%u Mhz\n", V_I4(&value) );

        IWbemClassObject_Release( obj );
    }

done:
    IEnumWbemClassObject_Release( iter );
}

static void output_hotfixes( IWbemServices *services, enum format_flags flags, UINT32 width )
{
    IEnumWbemClassObject *iter;
    IWbemClassObject *obj;
    DWORD i, num_hotfixes = 0, count;
    VARIANT value;
    BSTR str;
    HRESULT hr;

    str = SysAllocString( L"Win32_QuickFixEngineering" );
    hr = IWbemServices_CreateInstanceEnum( services, str, 0, NULL, &iter );
    SysFreeString( str );
    if (FAILED( hr )) return;

    while (IEnumWbemClassObject_Skip( iter, WBEM_INFINITE, 1 ) == S_OK) num_hotfixes++;

    fwprintf( stdout, L"Hotfix(es):%*s %u Hotfix(es) Installed.\n", width - wcslen(L"Hotfix(es)"), L" ", num_hotfixes );
    IEnumWbemClassObject_Reset( iter );

    for (i = 0; i < num_hotfixes; i++)
    {
        hr = IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
        if (FAILED( hr )) goto done;

        hr = IWbemClassObject_Get( obj, L"Caption", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        fwprintf( stdout, L"%*s[%02u]: %s\n", width + 2, L" ", i + 1, V_BSTR(&value) );
        VariantClear( &value );

        IWbemClassObject_Release( obj );
    }

done:
    IEnumWbemClassObject_Release( iter );
}

static void output_nics( IWbemServices *services, enum format_flags flags, UINT32 width )
{
    IEnumWbemClassObject *iter;
    IWbemClassObject *obj;
    DWORD i, num_nics = 0, count;
    VARIANT value;
    SAFEARRAY *sa;
    LONG bound = -1, j;
    BSTR str;
    HRESULT hr;

    str = SysAllocString( L"Win32_NetworkAdapterConfiguration" );
    hr = IWbemServices_CreateInstanceEnum( services, str, 0, NULL, &iter );
    SysFreeString( str );
    if (FAILED( hr )) return;

    while (IEnumWbemClassObject_Skip( iter, WBEM_INFINITE, 1 ) == S_OK) num_nics++;

    fwprintf( stdout, L"Network Card(s):%*s %u NICs(s) Installed.\n", width - wcslen(L"Network Card(s)"), L" ", num_nics );
    IEnumWbemClassObject_Reset( iter );

    for (i = 0; i < num_nics; i++)
    {
        hr = IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
        if (FAILED( hr )) goto done;

        hr = IWbemClassObject_Get( obj, L"Description", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        fwprintf( stdout, L"%*s[%02u]: %s\n", width + 2, L" ", i + 1, V_BSTR(&value) );
        VariantClear( &value );

        /* FIXME: Connection Name, DHCP Server */

        hr = IWbemClassObject_Get( obj, L"DHCPEnabled", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        fwprintf( stdout, L"%*s      DHCP Enabled: %s\n", width + 2, L" ", V_BOOL(&value) ? L"Yes" : L"No" );

        hr = IWbemClassObject_Get( obj, L"IPAddress", 0, &value, NULL, NULL );
        if (FAILED( hr ))
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        if (V_VT( &value ) == (VT_BSTR | VT_ARRAY))
        {
            sa = V_ARRAY( &value );
            SafeArrayGetUBound( sa, 1, &bound );
            if (bound >= 0)
            {
                fwprintf( stdout, L"%*s      IP Addresse(es)\n", width + 2, L" " );
                for (j = 0; j <= bound; j++)
                {
                    SafeArrayGetElement( sa, &j, &str );
                    fwprintf( stdout, L"%*s      [%02u]: %s\n", width + 2, L" ", j + 1, str );
                    SysFreeString( str );
                }
            }
        }
        VariantClear( &value );
        IWbemClassObject_Release( obj );
    }

done:
    IEnumWbemClassObject_Release( iter );
}

static void output_timezone( IWbemServices *services, enum format_flags flags, UINT32 width )
{
    WCHAR name[64], timezone[256] = {};
    DWORD count = sizeof(name);
    HKEY key_current = 0, key_timezones = 0, key_name = 0;

    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\TimeZoneInformation", 0,
                       KEY_READ, &key_current )) goto done;
    if (RegQueryValueExW( key_current, L"TimeZoneKeyName", NULL, NULL, (BYTE *)name, &count )) goto done;
    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0,
                       KEY_READ, &key_timezones )) goto done;
    if (RegOpenKeyExW( key_timezones, name, 0, KEY_READ, &key_name )) goto done;
    count = sizeof(timezone);
    RegQueryValueExW( key_name, L"Display", NULL, NULL, (BYTE *)timezone, &count );

done:
    fwprintf( stdout, L"Time Zone:%*s %s\n", width - wcslen(L"Time Zone"), L" ", timezone );
    RegCloseKey( key_name );
    RegCloseKey( key_timezones );
    RegCloseKey( key_current );
}

static const struct sysinfo sysinfo_map[] =
{
    { L"Host Name", L"Win32_ComputerSystem", L"Name" },
    { L"OS Name", L"Win32_OperatingSystem", L"Caption" },
    { L"OS Version", L"Win32_OperatingSystem", L"Version" }, /* FIXME build number */
    { L"OS Manufacturer", L"Win32_OperatingSystem", L"Manufacturer" },
    { L"OS Configuration", NULL, L"Standalone Workstation" },
    { L"OS Build Type", L"Win32_OperatingSystem", L"BuildType" },
    { L"Registered Owner", L"Win32_OperatingSystem", L"RegisteredUser" },
    { L"Registered Organization", L"Win32_OperatingSystem", L"Organization" },
    { L"Product ID", L"Win32_OperatingSystem", L"SerialNumber" },
    { L"Original Install Date", L"Win32_OperatingSystem", L"InstallDate", NULL, FORMAT_DATE },
    { L"System Boot Time", L"Win32_OperatingSystem", L"LastBootUpTime", NULL, FORMAT_DATE },
    { L"System Manufacturer", L"Win32_ComputerSystem", L"Manufacturer" },
    { L"System Model", L"Win32_ComputerSystem", L"Model" },
    { L"System Type", L"Win32_ComputerSystem", L"SystemType" },
    { L"Processor(s)", NULL, NULL, output_processors },
    { L"BIOS Version", L"Win32_BIOS", L"SMBIOSBIOSVersion" },
    { L"Windows Directory", L"Win32_OperatingSystem", L"WindowsDirectory" },
    { L"System Directory", L"Win32_OperatingSystem", L"SystemDirectory" },
    { L"Boot Device", L"Win32_OperatingSystem", L"BootDevice" },
    { L"System Locale", L"Win32_OperatingSystem", L"Locale", NULL, FORMAT_LOCALE },
    { L"Input Locale", L"Win32_OperatingSystem", L"Locale", NULL, FORMAT_LOCALE }, /* FIXME */
    { L"Time Zone", NULL, NULL, output_timezone },
    { L"Total Physical Memory", L"Win32_OperatingSystem", L"TotalVisibleMemorySize", NULL, FORMAT_SIZE },
    { L"Available Physical Memory", L"Win32_OperatingSystem", L"FreePhysicalMemory", NULL, FORMAT_SIZE },
    { L"Virtual Memory: Max Size", L"Win32_OperatingSystem", L"TotalVirtualMemorySize", NULL, FORMAT_SIZE },
    { L"Virtual Memory: Available", L"Win32_OperatingSystem", L"FreeVirtualMemory", NULL, FORMAT_SIZE },
    /* FIXME Virtual Memory: In Use */
    { L"Page File Location(s)", L"Win32_PageFileUsage", L"Name" },
    { L"Domain", L"Win32_ComputerSystem", L"Domain" },
    /* FIXME Logon Server */
    { L"Hotfix(s)", NULL, NULL, output_hotfixes },
    { L"Network Card(s)", NULL, NULL, output_nics },
    /* FIXME Hyper-V Requirements */
};

static void output_item( IWbemServices *services, const struct sysinfo *info, UINT32 width )
{
    HRESULT hr;
    IWbemClassObject *obj = NULL;
    BSTR str;
    VARIANT value;

    if (!info->class)
    {
        if (info->property)
            fwprintf( stdout, L"%s:%*s %s\n", info->item, width - wcslen(info->item), L" ", info->property );
        else
            info->callback( services, info->flags, width );
        return;
    }

    if (!(str = SysAllocString( info->class ))) return;
    hr = IWbemServices_GetObject( services, str, 0, NULL, &obj, NULL );
    SysFreeString( str );
    if (FAILED( hr )) return;

    hr = IWbemClassObject_Get( obj, info->property, 0, &value, NULL, NULL );
    if (FAILED( hr ))
    {
        IWbemClassObject_Release( obj );
        return;
    }

    switch (info->flags)
    {
    case FORMAT_DATE:
    {
        SYSTEMTIME st;
        WCHAR date[32] = {}, time[32] = {};

        /* assume UTC */
        memset( &st, 0, sizeof(st) );
        swscanf( V_BSTR(&value), L"%04u%02u%02u%02u%02u%02u",
                 &st.wYear, &st.wMonth, &st.wDay, &st.wHour, &st.wMinute, &st.wSecond );
        GetDateFormatW( LOCALE_SYSTEM_DEFAULT, 0, &st, NULL, date, ARRAY_SIZE(date) );
        GetTimeFormatW( LOCALE_SYSTEM_DEFAULT, 0, &st, NULL, time, ARRAY_SIZE(time) );
        fwprintf( stdout, L"%s:%*s %s, %s\n", info->item, width - wcslen(info->item), L" ", date, time );
        break;
    }
    case FORMAT_LOCALE:
    {
        UINT32 lcid;
        WCHAR name[32] = {}, displayname[LOCALE_NAME_MAX_LENGTH] = {};

        swscanf( V_BSTR(&value), L"%x", &lcid );
        LCIDToLocaleName( lcid, name, ARRAY_SIZE(name), 0 );
        GetLocaleInfoW( lcid, LOCALE_SENGLISHDISPLAYNAME, displayname, ARRAY_SIZE(displayname) );
        fwprintf( stdout, L"%s:%*s %s;%s\n", info->item, width - wcslen(info->item), L" ", name, displayname );
        break;
    }
    case FORMAT_SIZE:
    {
        UINT64 size = 0;
        swscanf( V_BSTR(&value), L"%I64u", &size );
        fwprintf( stdout, L"%s:%*s %I64u MB\n", info->item, width - wcslen(info->item), L" ", size / 1024 );
        break;
    }
    default:
        fwprintf( stdout, L"%s:%*s %s\n", info->item, width - wcslen(info->item), L" ", V_BSTR(&value) );
        break;
    }
    VariantClear( &value );
}

static void output_sysinfo( void )
{
    IWbemLocator *locator;
    IWbemServices *services = NULL;
    UINT32 i, len, width = 0;
    HRESULT hr;
    BSTR path;

    CoInitialize( NULL );
    CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0, NULL );

    hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (void **)&locator );
    if (hr != S_OK) return;

    if (!(path = SysAllocString( L"ROOT\\CIMV2" ))) goto done;
    hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
    SysFreeString( path );
    if (hr != S_OK) goto done;

    for (i = 0; i < ARRAY_SIZE(sysinfo_map); i++) if ((len = wcslen( sysinfo_map[i].item )) > width) width = len;
    width++;

    for (i = 0; i < ARRAY_SIZE(sysinfo_map); i++) output_item( services, &sysinfo_map[i], width );

done:
    if (services) IWbemServices_Release( services );
    IWbemLocator_Release( locator );
    CoUninitialize();
}

int __cdecl wmain( int argc, WCHAR *argv[] )
{
    if (argc > 1)
    {
        int i;
        FIXME( "stub:" );
        for (i = 0; i < argc; i++) FIXME( " %s", wine_dbgstr_w(argv[i]) );
        FIXME( "\n" );
        return 0;
    }

    output_sysinfo();
    return 0;
}