jscript: Always use jsval-based to_number implementation.
[wine] / dlls / wbemprox / builtin.c
1 /*
2  * Copyright 2012 Hans Leidekker for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
22
23 #include "config.h"
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wbemcli.h"
31 #include "winsock2.h"
32 #include "iphlpapi.h"
33 #include "tlhelp32.h"
34 #include "initguid.h"
35 #include "d3d10.h"
36 #include "winternl.h"
37 #include "winioctl.h"
38 #include "winsvc.h"
39
40 #include "wine/debug.h"
41 #include "wbemprox_private.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
44
45 static const WCHAR class_baseboardW[] =
46     {'W','i','n','3','2','_','B','a','s','e','B','o','a','r','d',0};
47 static const WCHAR class_biosW[] =
48     {'W','i','n','3','2','_','B','I','O','S',0};
49 static const WCHAR class_compsysW[] =
50     {'W','i','n','3','2','_','C','o','m','p','u','t','e','r','S','y','s','t','e','m',0};
51 static const WCHAR class_logicaldiskW[] =
52     {'W','i','n','3','2','_','L','o','g','i','c','a','l','D','i','s','k',0};
53 static const WCHAR class_networkadapterW[] =
54     {'W','i','n','3','2','_','N','e','t','w','o','r','k','A','d','a','p','t','e','r',0};
55 static const WCHAR class_osW[] =
56     {'W','i','n','3','2','_','O','p','e','r','a','t','i','n','g','S','y','s','t','e','m',0};
57 static const WCHAR class_paramsW[] =
58     {'_','_','P','A','R','A','M','E','T','E','R','S',0};
59 static const WCHAR class_processW[] =
60     {'W','i','n','3','2','_','P','r','o','c','e','s','s',0};
61 static const WCHAR class_processorW[] =
62     {'W','i','n','3','2','_','P','r','o','c','e','s','s','o','r',0};
63 static const WCHAR class_serviceW[] =
64     {'W','i','n','3','2','_','S','e','r','v','i','c','e',0};
65 static const WCHAR class_stdregprovW[] =
66     {'S','t','d','R','e','g','P','r','o','v',0};
67 static const WCHAR class_videocontrollerW[] =
68     {'W','i','n','3','2','_','V','i','d','e','o','C','o','n','t','r','o','l','l','e','r',0};
69
70 static const WCHAR prop_acceptpauseW[] =
71     {'A','c','c','e','p','t','P','a','u','s','e',0};
72 static const WCHAR prop_acceptstopW[] =
73     {'A','c','c','e','p','t','S','t','o','p',0};
74 static const WCHAR prop_adapterramW[] =
75     {'A','d','a','p','t','e','r','R','A','M',0};
76 static const WCHAR prop_captionW[] =
77     {'C','a','p','t','i','o','n',0};
78 static const WCHAR prop_classW[] =
79     {'C','l','a','s','s',0};
80 static const WCHAR prop_commandlineW[] =
81     {'C','o','m','m','a','n','d','L','i','n','e',0};
82 static const WCHAR prop_cpustatusW[] =
83     {'C','p','u','S','t','a','t','u','s',0};
84 static const WCHAR prop_csdversionW[] =
85     {'C','S','D','V','e','r','s','i','o','n',0};
86 static const WCHAR prop_currentbitsperpixelW[] =
87     {'C','u','r','r','e','n','t','B','i','t','s','P','e','r','P','i','x','e','l',0};
88 static const WCHAR prop_currenthorizontalresW[] =
89     {'C','u','r','r','e','n','t','H','o','r','i','z','o','n','t','a','l','R','e','s','o','l','u','t','i','o','n',0};
90 static const WCHAR prop_currentverticalresW[] =
91     {'C','u','r','r','e','n','t','V','e','r','t','i','c','a','l','R','e','s','o','l','u','t','i','o','n',0};
92 static const WCHAR prop_defaultvalueW[] =
93     {'D','e','f','a','u','l','t','V','a','l','u','e',0};
94 static const WCHAR prop_descriptionW[] =
95     {'D','e','s','c','r','i','p','t','i','o','n',0};
96 static const WCHAR prop_deviceidW[] =
97     {'D','e','v','i','c','e','I','d',0};
98 static const WCHAR prop_directionW[] =
99     {'D','i','r','e','c','t','i','o','n',0};
100 static const WCHAR prop_displaynameW[] =
101     {'D','i','s','p','l','a','y','N','a','m','e',0};
102 static const WCHAR prop_domainroleW[] =
103     {'D','o','m','a','i','n','R','o','l','e',0};
104 static const WCHAR prop_drivetypeW[] =
105     {'D','r','i','v','e','T','y','p','e',0};
106 static const WCHAR prop_filesystemW[] =
107     {'F','i','l','e','S','y','s','t','e','m',0};
108 static const WCHAR prop_freespaceW[] =
109     {'F','r','e','e','S','p','a','c','e',0};
110 static const WCHAR prop_handleW[] =
111     {'H','a','n','d','l','e',0};
112 static const WCHAR prop_interfaceindexW[] =
113     {'I','n','t','e','r','f','a','c','e','I','n','d','e','x',0};
114 static const WCHAR prop_macaddressW[] =
115     {'M','A','C','A','d','d','r','e','s','s',0};
116 static const WCHAR prop_manufacturerW[] =
117     {'M','a','n','u','f','a','c','t','u','r','e','r',0};
118 static const WCHAR prop_maxclockspeedW[] =
119     {'M','a','x','C','l','o','c','k','S','p','e','e','d',0};
120 static const WCHAR prop_methodW[] =
121     {'M','e','t','h','o','d',0};
122 static const WCHAR prop_modelW[] =
123     {'M','o','d','e','l',0};
124 static const WCHAR prop_nameW[] =
125     {'N','a','m','e',0};
126 static const WCHAR prop_netconnectionstatusW[] =
127     {'N','e','t','C','o','n','n','e','c','t','i','o','n','S','t','a','t','u','s',0};
128 static const WCHAR prop_numlogicalprocessorsW[] =
129     {'N','u','m','b','e','r','O','f','L','o','g','i','c','a','l','P','r','o','c','e','s','s','o','r','s',0};
130 static const WCHAR prop_numprocessorsW[] =
131     {'N','u','m','b','e','r','O','f','P','r','o','c','e','s','s','o','r','s',0};
132 static const WCHAR prop_osarchitectureW[] =
133     {'O','S','A','r','c','h','i','t','e','c','t','u','r','e',0};
134 static const WCHAR prop_oslanguageW[] =
135     {'O','S','L','a','n','g','u','a','g','e',0};
136 static const WCHAR prop_parameterW[] =
137     {'P','a','r','a','m','e','t','e','r',0};
138 static const WCHAR prop_pnpdeviceidW[] =
139     {'P','N','P','D','e','v','i','c','e','I','D',0};
140 static const WCHAR prop_pprocessidW[] =
141     {'P','a','r','e','n','t','P','r','o','c','e','s','s','I','D',0};
142 static const WCHAR prop_processidW[] =
143     {'P','r','o','c','e','s','s','I','D',0};
144 static const WCHAR prop_processoridW[] =
145     {'P','r','o','c','e','s','s','o','r','I','d',0};
146 static const WCHAR prop_releasedateW[] =
147     {'R','e','l','e','a','s','e','D','a','t','e',0};
148 static const WCHAR prop_serialnumberW[] =
149     {'S','e','r','i','a','l','N','u','m','b','e','r',0};
150 static const WCHAR prop_servicetypeW[] =
151     {'S','e','r','v','i','c','e','T','y','p','e',0};
152 static const WCHAR prop_startmodeW[] =
153     {'S','t','a','r','t','M','o','d','e',0};
154 static const WCHAR prop_sizeW[] =
155     {'S','i','z','e',0};
156 static const WCHAR prop_speedW[] =
157     {'S','p','e','e','d',0};
158 static const WCHAR prop_stateW[] =
159     {'S','t','a','t','e',0};
160 static const WCHAR prop_systemdirectoryW[] =
161     {'S','y','s','t','e','m','D','i','r','e','c','t','o','r','y',0};
162 static const WCHAR prop_systemnameW[] =
163     {'S','y','s','t','e','m','N','a','m','e',0};
164 static const WCHAR prop_tagW[] =
165     {'T','a','g',0};
166 static const WCHAR prop_threadcountW[] =
167     {'T','h','r','e','a','d','C','o','u','n','t',0};
168 static const WCHAR prop_totalphysicalmemoryW[] =
169     {'T','o','t','a','l','P','h','y','s','i','c','a','l','M','e','m','o','r','y',0};
170 static const WCHAR prop_typeW[] =
171     {'T','y','p','e',0};
172
173 static const WCHAR method_enumkeyW[] =
174     {'E','n','u','m','K','e','y',0};
175 static const WCHAR method_enumvaluesW[] =
176     {'E','n','u','m','V','a','l','u','e','s',0};
177
178 static const WCHAR param_defkeyW[] =
179     {'h','D','e','f','K','e','y',0};
180 static const WCHAR param_namesW[] =
181     {'N','a','m','e','s',0};
182 static const WCHAR param_returnvalueW[] =
183     {'R','e','t','u','r','n','V','a','l','u','e',0};
184 static const WCHAR param_subkeynameW[] =
185     {'s','S','u','b','K','e','y','N','a','m','e',0};
186 static const WCHAR param_typesW[] =
187     {'T','y','p','e','s',0};
188
189 /* column definitions must be kept in sync with record structures below */
190 static const struct column col_baseboard[] =
191 {
192     { prop_manufacturerW,  CIM_STRING },
193     { prop_serialnumberW,  CIM_STRING },
194     { prop_tagW,           CIM_STRING|COL_FLAG_KEY }
195 };
196 static const struct column col_bios[] =
197 {
198     { prop_descriptionW,  CIM_STRING },
199     { prop_manufacturerW, CIM_STRING },
200     { prop_releasedateW,  CIM_DATETIME },
201     { prop_serialnumberW, CIM_STRING }
202 };
203 static const struct column col_compsys[] =
204 {
205     { prop_descriptionW,          CIM_STRING },
206     { prop_domainroleW,           CIM_UINT16 },
207     { prop_manufacturerW,         CIM_STRING },
208     { prop_modelW,                CIM_STRING },
209     { prop_numlogicalprocessorsW, CIM_UINT32, VT_I4 },
210     { prop_numprocessorsW,        CIM_UINT32, VT_I4 },
211     { prop_totalphysicalmemoryW,  CIM_UINT64 }
212 };
213 static const struct column col_logicaldisk[] =
214 {
215     { prop_deviceidW,   CIM_STRING|COL_FLAG_DYNAMIC|COL_FLAG_KEY },
216     { prop_drivetypeW,  CIM_UINT32, VT_I4 },
217     { prop_filesystemW, CIM_STRING|COL_FLAG_DYNAMIC },
218     { prop_freespaceW,  CIM_UINT64 },
219     { prop_sizeW,       CIM_UINT64 }
220 };
221 static const struct column col_networkadapter[] =
222 {
223     { prop_deviceidW,            CIM_STRING|COL_FLAG_DYNAMIC|COL_FLAG_KEY },
224     { prop_interfaceindexW,      CIM_UINT32, VT_I4 },
225     { prop_macaddressW,          CIM_STRING|COL_FLAG_DYNAMIC },
226     { prop_netconnectionstatusW, CIM_UINT16, VT_I4 },
227     { prop_pnpdeviceidW,         CIM_STRING },
228     { prop_speedW,               CIM_UINT64 }
229 };
230 static const struct column col_os[] =
231 {
232     { prop_captionW,         CIM_STRING },
233     { prop_csdversionW,      CIM_STRING },
234     { prop_osarchitectureW,  CIM_STRING },
235     { prop_oslanguageW,      CIM_UINT32, VT_I4 },
236     { prop_systemdirectoryW, CIM_STRING }
237 };
238 static const struct column col_params[] =
239 {
240     { prop_classW,        CIM_STRING },
241     { prop_methodW,       CIM_STRING },
242     { prop_directionW,    CIM_SINT32 },
243     { prop_parameterW,    CIM_STRING },
244     { prop_typeW,         CIM_UINT32 },
245     { prop_defaultvalueW, CIM_UINT32 }
246 };
247 static const struct column col_process[] =
248 {
249     { prop_captionW,     CIM_STRING|COL_FLAG_DYNAMIC },
250     { prop_commandlineW, CIM_STRING|COL_FLAG_DYNAMIC },
251     { prop_descriptionW, CIM_STRING|COL_FLAG_DYNAMIC },
252     { prop_handleW,      CIM_STRING|COL_FLAG_DYNAMIC|COL_FLAG_KEY },
253     { prop_pprocessidW,  CIM_UINT32, VT_I4 },
254     { prop_processidW,   CIM_UINT32, VT_I4 },
255     { prop_threadcountW, CIM_UINT32, VT_I4 }
256 };
257 static const struct column col_processor[] =
258 {
259     { prop_cpustatusW,            CIM_UINT16 },
260     { prop_deviceidW,             CIM_STRING|COL_FLAG_DYNAMIC|COL_FLAG_KEY },
261     { prop_manufacturerW,         CIM_STRING|COL_FLAG_DYNAMIC },
262     { prop_maxclockspeedW,        CIM_UINT32, VT_I4 },
263     { prop_nameW,                 CIM_STRING|COL_FLAG_DYNAMIC },
264     { prop_numlogicalprocessorsW, CIM_UINT32, VT_I4 },
265     { prop_processoridW,          CIM_STRING|COL_FLAG_DYNAMIC }
266 };
267 static const struct column col_service[] =
268 {
269     { prop_acceptpauseW,      CIM_BOOLEAN },
270     { prop_acceptstopW,       CIM_BOOLEAN },
271     { prop_displaynameW,      CIM_STRING|COL_FLAG_DYNAMIC },
272     { prop_nameW,             CIM_STRING|COL_FLAG_DYNAMIC|COL_FLAG_KEY },
273     { prop_processidW,        CIM_UINT32 },
274     { prop_servicetypeW,      CIM_STRING },
275     { prop_startmodeW,        CIM_STRING },
276     { prop_stateW,            CIM_STRING },
277     { prop_systemnameW,       CIM_STRING|COL_FLAG_DYNAMIC }
278 };
279 static const struct column col_stdregprov[] =
280 {
281     { method_enumkeyW,    CIM_OBJECT|COL_FLAG_METHOD },
282     { method_enumvaluesW, CIM_OBJECT|COL_FLAG_METHOD }
283 };
284 static const struct column col_videocontroller[] =
285 {
286     { prop_adapterramW,           CIM_UINT32 },
287     { prop_currentbitsperpixelW,  CIM_UINT32 },
288     { prop_currenthorizontalresW, CIM_UINT32 },
289     { prop_currentverticalresW,   CIM_UINT32 },
290     { prop_descriptionW,          CIM_STRING|COL_FLAG_DYNAMIC },
291     { prop_deviceidW,             CIM_STRING|COL_FLAG_KEY },
292     { prop_nameW,                 CIM_STRING|COL_FLAG_DYNAMIC }
293 };
294
295 static const WCHAR baseboard_manufacturerW[] =
296     {'I','n','t','e','l',' ','C','o','r','p','o','r','a','t','i','o','n',0};
297 static const WCHAR baseboard_serialnumberW[] =
298     {'N','o','n','e',0};
299 static const WCHAR baseboard_tagW[] =
300     {'B','a','s','e',' ','B','o','a','r','d',0};
301 static const WCHAR bios_descriptionW[] =
302     {'D','e','f','a','u','l','t',' ','S','y','s','t','e','m',' ','B','I','O','S',0};
303 static const WCHAR bios_manufacturerW[] =
304     {'T','h','e',' ','W','i','n','e',' ','P','r','o','j','e','c','t',0};
305 static const WCHAR bios_releasedateW[] =
306     {'2','0','1','2','0','6','0','8','0','0','0','0','0','0','.','0','0','0','0','0','0','+','0','0','0',0};
307 static const WCHAR bios_serialnumberW[] =
308     {'0',0};
309 static const WCHAR compsys_descriptionW[] =
310     {'A','T','/','A','T',' ','C','O','M','P','A','T','I','B','L','E',0};
311 static const WCHAR compsys_manufacturerW[] =
312     {'T','h','e',' ','W','i','n','e',' ','P','r','o','j','e','c','t',0};
313 static const WCHAR compsys_modelW[] =
314     {'W','i','n','e',0};
315 static const WCHAR networkadapter_pnpdeviceidW[]=
316     {'P','C','I','\\','V','E','N','_','8','0','8','6','&','D','E','V','_','1','0','0','E','&',
317      'S','U','B','S','Y','S','_','0','0','1','E','8','0','8','6','&','R','E','V','_','0','2','\\',
318      '3','&','2','6','7','A','6','1','6','A','&','1','&','1','8',0};
319 static const WCHAR os_captionW[] =
320     {'M','i','c','r','o','s','o','f','t',' ','W','i','n','d','o','w','s',' ','X','P',' ',
321      'V','e','r','s','i','o','n',' ','=',' ','5','.','1','.','2','6','0','0',0};
322 static const WCHAR os_csdversionW[] =
323     {'S','e','r','v','i','c','e',' ','P','a','c','k',' ','3',0};
324 static const WCHAR os_32bitW[] =
325     {'3','2','-','b','i','t',0};
326 static const WCHAR os_64bitW[] =
327     {'6','4','-','b','i','t',0};
328 static const WCHAR videocontroller_deviceidW[] =
329     {'V','i','d','e','o','C','o','n','t','r','o','l','l','e','r','1',0};
330
331 #include "pshpack1.h"
332 struct record_baseboard
333 {
334     const WCHAR *manufacturer;
335     const WCHAR *serialnumber;
336     const WCHAR *tag;
337 };
338 struct record_bios
339 {
340     const WCHAR *description;
341     const WCHAR *manufacturer;
342     const WCHAR *releasedate;
343     const WCHAR *serialnumber;
344 };
345 struct record_computersystem
346 {
347     const WCHAR *description;
348     UINT16       domainrole;
349     const WCHAR *manufacturer;
350     const WCHAR *model;
351     UINT32       num_logical_processors;
352     UINT32       num_processors;
353     UINT64       total_physical_memory;
354 };
355 struct record_logicaldisk
356 {
357     const WCHAR *device_id;
358     UINT32       drivetype;
359     const WCHAR *filesystem;
360     UINT64       freespace;
361     UINT64       size;
362 };
363 struct record_networkadapter
364 {
365     const WCHAR *device_id;
366     INT32        interface_index;
367     const WCHAR *mac_address;
368     UINT16       netconnection_status;
369     const WCHAR *pnpdevice_id;
370     UINT64       speed;
371 };
372 struct record_operatingsystem
373 {
374     const WCHAR *caption;
375     const WCHAR *csdversion;
376     const WCHAR *osarchitecture;
377     UINT32       oslanguage;
378     const WCHAR *systemdirectory;
379 };
380 struct record_params
381 {
382     const WCHAR *class;
383     const WCHAR *method;
384     INT32        direction;
385     const WCHAR *parameter;
386     UINT32       type;
387     UINT32       defaultvalue;
388 };
389 struct record_process
390 {
391     const WCHAR *caption;
392     const WCHAR *commandline;
393     const WCHAR *description;
394     const WCHAR *handle;
395     UINT32       pprocess_id;
396     UINT32       process_id;
397     UINT32       thread_count;
398 };
399 struct record_processor
400 {
401     UINT16       cpu_status;
402     const WCHAR *device_id;
403     const WCHAR *manufacturer;
404     UINT32       maxclockspeed;
405     const WCHAR *name;
406     UINT32       num_logical_processors;
407     const WCHAR *processor_id;
408 };
409 struct record_service
410 {
411     int          accept_pause;
412     int          accept_stop;
413     const WCHAR *displayname;
414     const WCHAR *name;
415     UINT32       process_id;
416     const WCHAR *servicetype;
417     const WCHAR *startmode;
418     const WCHAR *state;
419     const WCHAR *systemname;
420 };
421 struct record_stdregprov
422 {
423     class_method *enumkey;
424     class_method *enumvalues;
425 };
426 struct record_videocontroller
427 {
428     UINT32       adapter_ram;
429     UINT32       current_bitsperpixel;
430     UINT32       current_horizontalres;
431     UINT32       current_verticalres;
432     const WCHAR *description;
433     const WCHAR *device_id;
434     const WCHAR *name;
435 };
436 #include "poppack.h"
437
438 static HRESULT reg_enumkey( IWbemClassObject *in, IWbemClassObject **out )
439 {
440     FIXME("\n");
441     return E_NOTIMPL;
442 }
443 static HRESULT reg_enumvalues( IWbemClassObject *in, IWbemClassObject **out )
444 {
445     FIXME("\n");
446     return E_NOTIMPL;
447 }
448
449 static const struct record_baseboard data_baseboard[] =
450 {
451     { baseboard_manufacturerW, baseboard_serialnumberW, baseboard_tagW }
452 };
453 static const struct record_bios data_bios[] =
454 {
455     { bios_descriptionW, bios_manufacturerW, bios_releasedateW, bios_serialnumberW }
456 };
457 static const struct record_params data_params[] =
458 {
459     { class_stdregprovW, method_enumkeyW, 1, param_defkeyW, CIM_UINT32, 0x80000002 },
460     { class_stdregprovW, method_enumkeyW, 1, param_subkeynameW, CIM_STRING },
461     { class_stdregprovW, method_enumkeyW, -1, param_returnvalueW, CIM_UINT32 },
462     { class_stdregprovW, method_enumkeyW, -1, param_namesW, CIM_STRING|CIM_FLAG_ARRAY },
463     { class_stdregprovW, method_enumvaluesW, 1, param_defkeyW, CIM_UINT32, 0x80000002 },
464     { class_stdregprovW, method_enumvaluesW, 1, param_subkeynameW, CIM_STRING },
465     { class_stdregprovW, method_enumvaluesW, -1, param_returnvalueW, CIM_UINT32 },
466     { class_stdregprovW, method_enumvaluesW, -1, param_namesW, CIM_STRING|CIM_FLAG_ARRAY },
467     { class_stdregprovW, method_enumvaluesW, -1, param_typesW, CIM_SINT32|CIM_FLAG_ARRAY }
468 };
469 static const struct record_stdregprov data_stdregprov[] =
470 {
471     { reg_enumkey, reg_enumvalues }
472 };
473
474 static UINT get_processor_count(void)
475 {
476     SYSTEM_BASIC_INFORMATION info;
477
478     if (NtQuerySystemInformation( SystemBasicInformation, &info, sizeof(info), NULL )) return 1;
479     return info.NumberOfProcessors;
480 }
481
482 static UINT get_logical_processor_count(void)
483 {
484     SYSTEM_LOGICAL_PROCESSOR_INFORMATION *info;
485     UINT i, j, count = 0;
486     NTSTATUS status;
487     ULONG len;
488
489     status = NtQuerySystemInformation( SystemLogicalProcessorInformation, NULL, 0, &len );
490     if (status != STATUS_INFO_LENGTH_MISMATCH) return get_processor_count();
491
492     if (!(info = heap_alloc( len ))) return get_processor_count();
493     status = NtQuerySystemInformation( SystemLogicalProcessorInformation, info, len, &len );
494     if (status != STATUS_SUCCESS)
495     {
496         heap_free( info );
497         return get_processor_count();
498     }
499     for (i = 0; i < len / sizeof(*info); i++)
500     {
501         if (info[i].Relationship != RelationProcessorCore) continue;
502         for (j = 0; j < sizeof(ULONG_PTR); j++) if (info[i].ProcessorMask & (1 << j)) count++;
503     }
504     heap_free( info );
505     return count;
506 }
507
508 static UINT64 get_total_physical_memory(void)
509 {
510     MEMORYSTATUSEX status;
511
512     status.dwLength = sizeof(status);
513     if (!GlobalMemoryStatusEx( &status )) return 1024 * 1024 * 1024;
514     return status.ullTotalPhys;
515 }
516
517 static void fill_compsys( struct table *table )
518 {
519     struct record_computersystem *rec;
520
521     if (!(table->data = heap_alloc( sizeof(*rec) ))) return;
522
523     rec = (struct record_computersystem *)table->data;
524     rec->description            = compsys_descriptionW;
525     rec->domainrole             = 0; /* standalone workstation */
526     rec->manufacturer           = compsys_manufacturerW;
527     rec->model                  = compsys_modelW;
528     rec->num_logical_processors = get_logical_processor_count();
529     rec->num_processors         = get_processor_count();
530     rec->total_physical_memory  = get_total_physical_memory();
531
532     TRACE("created 1 row\n");
533     table->num_rows = 1;
534 }
535
536 static WCHAR *get_filesystem( const WCHAR *root )
537 {
538     static const WCHAR ntfsW[] = {'N','T','F','S',0};
539     WCHAR buffer[MAX_PATH + 1];
540
541     if (GetVolumeInformationW( root, NULL, 0, NULL, NULL, NULL, buffer, MAX_PATH + 1 ))
542         return heap_strdupW( buffer );
543     return heap_strdupW( ntfsW );
544 }
545
546 static UINT64 get_freespace( const WCHAR *dir, UINT64 *disksize )
547 {
548     WCHAR root[] = {'\\','\\','.','\\','A',':',0};
549     ULARGE_INTEGER free;
550     DISK_GEOMETRY_EX info;
551     HANDLE handle;
552
553     free.QuadPart = 512 * 1024 * 1024;
554     GetDiskFreeSpaceExW( dir, NULL, NULL, &free );
555
556     root[4] = dir[0];
557     handle = CreateFileW( root, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
558     if (handle != INVALID_HANDLE_VALUE)
559     {
560         if (DeviceIoControl( handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &info, sizeof(info), NULL, NULL ))
561             *disksize = info.DiskSize.QuadPart;
562         CloseHandle( handle );
563     }
564     return free.QuadPart;
565 }
566
567 static void fill_logicaldisk( struct table *table )
568 {
569     static const WCHAR fmtW[] = {'%','c',':',0};
570     WCHAR device_id[3], root[] = {'A',':','\\',0};
571     struct record_logicaldisk *rec;
572     UINT i, num_rows = 0, offset = 0, count = 4, type;
573     UINT64 size = 1024 * 1024 * 1024;
574     DWORD drives = GetLogicalDrives();
575
576     if (!(table->data = heap_alloc( count * sizeof(*rec) ))) return;
577
578     for (i = 0; i < sizeof(drives); i++)
579     {
580         if (drives & (1 << i))
581         {
582             root[0] = 'A' + i;
583             type = GetDriveTypeW( root );
584             if (type != DRIVE_FIXED && type != DRIVE_CDROM && type != DRIVE_REMOVABLE)
585                 continue;
586
587             if (num_rows > count)
588             {
589                 BYTE *data;
590                 count *= 2;
591                 if (!(data = heap_realloc( table->data, count * sizeof(*rec) ))) return;
592                 table->data = data;
593             }
594             rec = (struct record_logicaldisk *)(table->data + offset);
595             sprintfW( device_id, fmtW, 'A' + i );
596             rec->device_id  = heap_strdupW( device_id );
597             rec->drivetype  = type;
598             rec->filesystem = get_filesystem( root );
599             rec->freespace  = get_freespace( root, &size );
600             rec->size       = size;
601             offset += sizeof(*rec);
602             num_rows++;
603         }
604     }
605     TRACE("created %u rows\n", num_rows);
606     table->num_rows = num_rows;
607 }
608
609 static UINT16 get_connection_status( IF_OPER_STATUS status )
610 {
611     switch (status)
612     {
613     case IfOperStatusDown:
614         return 0; /* Disconnected */
615     case IfOperStatusUp:
616         return 2; /* Connected */
617     default:
618         ERR("unhandled status %u\n", status);
619         break;
620     }
621     return 0;
622 }
623 static WCHAR *get_mac_address( const BYTE *addr, DWORD len )
624 {
625     static const WCHAR fmtW[] =
626         {'%','0','2','x',':','%','0','2','x',':','%','0','2','x',':',
627          '%','0','2','x',':','%','0','2','x',':','%','0','2','x',0};
628     WCHAR *ret;
629
630     if (len != 6 || !(ret = heap_alloc( 18 * sizeof(WCHAR) ))) return NULL;
631     sprintfW( ret, fmtW, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] );
632     return ret;
633 }
634
635 static void fill_networkadapter( struct table *table )
636 {
637     static const WCHAR fmtW[] = {'%','u',0};
638     WCHAR device_id[11];
639     struct record_networkadapter *rec;
640     IP_ADAPTER_ADDRESSES *aa, *buffer;
641     UINT num_rows = 0, offset = 0;
642     DWORD size = 0, ret;
643
644     ret = GetAdaptersAddresses( AF_UNSPEC, 0, NULL, NULL, &size );
645     if (ret != ERROR_BUFFER_OVERFLOW) return;
646
647     if (!(buffer = heap_alloc( size ))) return;
648     if (GetAdaptersAddresses( AF_UNSPEC, 0, NULL, buffer, &size ))
649     {
650         heap_free( buffer );
651         return;
652     }
653     for (aa = buffer; aa; aa = aa->Next) num_rows++;
654     if (!(table->data = heap_alloc( sizeof(*rec) * num_rows )))
655     {
656         heap_free( buffer );
657         return;
658     }
659     for (aa = buffer; aa; aa = aa->Next)
660     {
661         rec = (struct record_networkadapter *)(table->data + offset);
662         sprintfW( device_id, fmtW, aa->u.s.IfIndex );
663         rec->device_id            = heap_strdupW( device_id );
664         rec->interface_index      = aa->u.s.IfIndex;
665         rec->mac_address          = get_mac_address( aa->PhysicalAddress, aa->PhysicalAddressLength );
666         rec->netconnection_status = get_connection_status( aa->OperStatus );
667         rec->pnpdevice_id         = networkadapter_pnpdeviceidW;
668         rec->speed                = 1000000;
669         offset += sizeof(*rec);
670     }
671     TRACE("created %u rows\n", num_rows);
672     table->num_rows = num_rows;
673
674     heap_free( buffer );
675 }
676
677 static WCHAR *get_cmdline( DWORD process_id )
678 {
679     if (process_id == GetCurrentProcessId()) return heap_strdupW( GetCommandLineW() );
680     return NULL; /* FIXME handle different process case */
681 }
682
683 static void fill_process( struct table *table )
684 {
685     static const WCHAR fmtW[] = {'%','u',0};
686     WCHAR handle[11];
687     struct record_process *rec;
688     PROCESSENTRY32W entry;
689     HANDLE snap;
690     UINT num_rows = 0, offset = 0, count = 8;
691
692     snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
693     if (snap == INVALID_HANDLE_VALUE) return;
694
695     entry.dwSize = sizeof(entry);
696     if (!Process32FirstW( snap, &entry )) goto done;
697     if (!(table->data = heap_alloc( count * sizeof(*rec) ))) goto done;
698
699     do
700     {
701         if (num_rows > count)
702         {
703             BYTE *data;
704             count *= 2;
705             if (!(data = heap_realloc( table->data, count * sizeof(*rec) ))) goto done;
706             table->data = data;
707         }
708         rec = (struct record_process *)(table->data + offset);
709         rec->caption      = heap_strdupW( entry.szExeFile );
710         rec->commandline  = get_cmdline( entry.th32ProcessID );
711         rec->description  = heap_strdupW( entry.szExeFile );
712         sprintfW( handle, fmtW, entry.th32ProcessID );
713         rec->handle       = heap_strdupW( handle );
714         rec->process_id   = entry.th32ProcessID;
715         rec->pprocess_id  = entry.th32ParentProcessID;
716         rec->thread_count = entry.cntThreads;
717         offset += sizeof(*rec);
718         num_rows++;
719     } while (Process32NextW( snap, &entry ));
720
721     TRACE("created %u rows\n", num_rows);
722     table->num_rows = num_rows;
723
724 done:
725     CloseHandle( snap );
726 }
727
728 static inline void do_cpuid( unsigned int ax, unsigned int *p )
729 {
730 #ifdef __i386__
731     __asm__("pushl %%ebx\n\t"
732                 "cpuid\n\t"
733                 "movl %%ebx, %%esi\n\t"
734                 "popl %%ebx"
735                 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
736                 :  "0" (ax));
737 #endif
738 }
739
740 static void get_processor_id( WCHAR *processor_id )
741 {
742     static const WCHAR fmtW[] = {'%','0','8','X','%','0','8','X',0};
743     unsigned int regs[4] = {0, 0, 0, 0};
744
745     do_cpuid( 1, regs );
746     sprintfW( processor_id, fmtW, regs[3], regs[0] );
747 }
748 static void regs_to_str( unsigned int *regs, unsigned int len, WCHAR *buffer )
749 {
750     unsigned int i;
751     unsigned char *p = (unsigned char *)regs;
752
753     for (i = 0; i < len; i++)
754     {
755         buffer[i] = *p++;
756     }
757     buffer[i] = 0;
758 }
759 static void get_processor_manufacturer( WCHAR *manufacturer )
760 {
761     unsigned int tmp, regs[4] = {0, 0, 0, 0};
762
763     do_cpuid( 0, regs );
764     tmp = regs[2];      /* swap edx and ecx */
765     regs[2] = regs[3];
766     regs[3] = tmp;
767
768     regs_to_str( regs + 1, 12, manufacturer );
769 }
770 static void get_processor_name( WCHAR *name )
771 {
772     unsigned int regs[4] = {0, 0, 0, 0};
773
774     do_cpuid( 0x80000000, regs );
775     if (regs[0] >= 0x80000004)
776     {
777         do_cpuid( 0x80000002, regs );
778         regs_to_str( regs, 16, name );
779         do_cpuid( 0x80000003, regs );
780         regs_to_str( regs, 16, name + 16 );
781         do_cpuid( 0x80000004, regs );
782         regs_to_str( regs, 16, name + 32 );
783     }
784 }
785 static UINT get_processor_maxclockspeed( void )
786 {
787     PROCESSOR_POWER_INFORMATION *info;
788     UINT ret = 1000, size = get_processor_count() * sizeof(PROCESSOR_POWER_INFORMATION);
789     NTSTATUS status;
790
791     if ((info = heap_alloc( size )))
792     {
793         status = NtPowerInformation( ProcessorInformation, NULL, 0, info, size );
794         if (!status) ret = info[0].MaxMhz;
795         heap_free( info );
796     }
797     return ret;
798 }
799
800 static void fill_processor( struct table *table )
801 {
802     static const WCHAR fmtW[] = {'C','P','U','%','u',0};
803     WCHAR device_id[14], processor_id[17], manufacturer[13], name[49] = {0};
804     struct record_processor *rec;
805     UINT i, offset = 0, maxclockspeed, num_logical_processors, count = get_processor_count();
806
807     if (!(table->data = heap_alloc( sizeof(*rec) * count ))) return;
808
809     get_processor_id( processor_id );
810     get_processor_manufacturer( manufacturer );
811     get_processor_name( name );
812
813     maxclockspeed = get_processor_maxclockspeed();
814     num_logical_processors = get_logical_processor_count() / count;
815
816     for (i = 0; i < count; i++)
817     {
818         rec = (struct record_processor *)(table->data + offset);
819         rec->cpu_status             = 1; /* CPU Enabled */
820         sprintfW( device_id, fmtW, i );
821         rec->device_id              = heap_strdupW( device_id );
822         rec->manufacturer           = heap_strdupW( manufacturer );
823         rec->maxclockspeed          = maxclockspeed;
824         rec->name                   = heap_strdupW( name );
825         rec->num_logical_processors = num_logical_processors;
826         rec->processor_id           = heap_strdupW( processor_id );
827         offset += sizeof(*rec);
828     }
829
830     TRACE("created %u rows\n", count);
831     table->num_rows = count;
832 }
833
834 static void fill_os( struct table *table )
835 {
836     struct record_operatingsystem *rec;
837     WCHAR path[MAX_PATH];
838     SYSTEM_INFO info;
839     void *redir;
840
841     if (!(table->data = heap_alloc( sizeof(*rec) ))) return;
842
843     rec = (struct record_operatingsystem *)table->data;
844     rec->caption    = os_captionW;
845     rec->csdversion = os_csdversionW;
846
847     GetNativeSystemInfo( &info );
848     if (info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
849         rec->osarchitecture = os_64bitW;
850     else
851         rec->osarchitecture = os_32bitW;
852
853     rec->oslanguage = MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US );
854
855     Wow64DisableWow64FsRedirection( &redir );
856     GetSystemDirectoryW( path, MAX_PATH );
857     Wow64RevertWow64FsRedirection( redir );
858     rec->systemdirectory = heap_strdupW( path );
859
860     TRACE("created 1 row\n");
861     table->num_rows = 1;
862 }
863
864 static const WCHAR *get_service_type( DWORD type )
865 {
866     static const WCHAR filesystem_driverW[] =
867         {'F','i','l','e',' ','S','y','s','t','e','m',' ','D','r','i','v','e','r',0};
868     static const WCHAR kernel_driverW[] =
869         {'K','e','r','n','e','l',' ','D','r','i','v','e','r',0};
870     static const WCHAR own_processW[] =
871         {'O','w','n',' ','P','r','o','c','e','s','s',0};
872     static const WCHAR share_processW[] =
873         {'S','h','a','r','e',' ','P','r','o','c','e','s','s',0};
874
875     if (type & SERVICE_KERNEL_DRIVER)            return kernel_driverW;
876     else if (type & SERVICE_FILE_SYSTEM_DRIVER)  return filesystem_driverW;
877     else if (type & SERVICE_WIN32_OWN_PROCESS)   return own_processW;
878     else if (type & SERVICE_WIN32_SHARE_PROCESS) return share_processW;
879     else ERR("unhandled type 0x%08x\n", type);
880     return NULL;
881 }
882 static const WCHAR *get_service_state( DWORD state )
883 {
884     static const WCHAR runningW[] =
885         {'R','u','n','n','i','n','g',0};
886     static const WCHAR start_pendingW[] =
887         {'S','t','a','r','t',' ','P','e','n','d','i','n','g',0};
888     static const WCHAR stop_pendingW[] =
889         {'S','t','o','p',' ','P','e','n','d','i','n','g',0};
890     static const WCHAR stoppedW[] =
891         {'S','t','o','p','p','e','d',0};
892     static const WCHAR unknownW[] =
893         {'U','n','k','n','o','w','n',0};
894
895     switch (state)
896     {
897     case SERVICE_STOPPED:       return stoppedW;
898     case SERVICE_START_PENDING: return start_pendingW;
899     case SERVICE_STOP_PENDING:  return stop_pendingW;
900     case SERVICE_RUNNING:       return runningW;
901     default:
902         ERR("unknown state %u\n", state);
903         return unknownW;
904     }
905 }
906
907 static const WCHAR *get_service_startmode( DWORD mode )
908 {
909     static const WCHAR bootW[] = {'B','o','o','t',0};
910     static const WCHAR systemW[] = {'S','y','s','t','e','m',0};
911     static const WCHAR autoW[] = {'A','u','t','o',0};
912     static const WCHAR manualW[] = {'M','a','n','u','a','l',0};
913     static const WCHAR disabledW[] = {'D','i','s','a','b','l','e','d',0};
914     static const WCHAR unknownW[] = {'U','n','k','n','o','w','n',0};
915
916     switch (mode)
917     {
918     case SERVICE_BOOT_START:   return bootW;
919     case SERVICE_SYSTEM_START: return systemW;
920     case SERVICE_AUTO_START:   return autoW;
921     case SERVICE_DEMAND_START: return manualW;
922     case SERVICE_DISABLED:     return disabledW;
923     default:
924         ERR("unknown mode 0x%x\n", mode);
925         return unknownW;
926     }
927 }
928
929 static QUERY_SERVICE_CONFIGW *query_service_config( SC_HANDLE manager, const WCHAR *name )
930 {
931     QUERY_SERVICE_CONFIGW *config = NULL;
932     SC_HANDLE service;
933     DWORD size;
934
935     if (!(service = OpenServiceW( manager, name, SERVICE_QUERY_CONFIG ))) return NULL;
936     QueryServiceConfigW( service, NULL, 0, &size );
937     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto done;
938     if (!(config = heap_alloc( size ))) goto done;
939     if (QueryServiceConfigW( service, config, size, &size )) goto done;
940     heap_free( config );
941     config = NULL;
942
943 done:
944     CloseServiceHandle( service );
945     return config;
946 }
947
948 static void fill_service( struct table *table )
949 {
950     struct record_service *rec;
951     SC_HANDLE manager;
952     ENUM_SERVICE_STATUS_PROCESSW *tmp, *services = NULL;
953     SERVICE_STATUS_PROCESS *status;
954     WCHAR sysnameW[MAX_COMPUTERNAME_LENGTH + 1];
955     DWORD len = sizeof(sysnameW) / sizeof(sysnameW[0]);
956     UINT i, num_rows = 0, offset = 0, size = 256, needed, count;
957     BOOL ret;
958
959     if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE ))) return;
960     if (!(services = heap_alloc( size ))) goto done;
961
962     ret = EnumServicesStatusExW( manager, SC_ENUM_PROCESS_INFO, SERVICE_TYPE_ALL,
963                                  SERVICE_STATE_ALL, (BYTE *)services, size, &needed,
964                                  &count, NULL, NULL );
965     if (!ret)
966     {
967         if (GetLastError() != ERROR_MORE_DATA) goto done;
968         size = needed;
969         if (!(tmp = heap_realloc( services, size ))) goto done;
970         services = tmp;
971         ret = EnumServicesStatusExW( manager, SC_ENUM_PROCESS_INFO, SERVICE_TYPE_ALL,
972                                      SERVICE_STATE_ALL, (BYTE *)services, size, &needed,
973                                      &count, NULL, NULL );
974         if (!ret) goto done;
975     }
976     if (!(table->data = heap_alloc( sizeof(*rec) * count ))) goto done;
977
978     GetComputerNameW( sysnameW, &len );
979
980     for (i = 0; i < count; i++)
981     {
982         QUERY_SERVICE_CONFIGW *config;
983
984         if (!(config = query_service_config( manager, services[i].lpServiceName ))) continue;
985
986         status = &services[i].ServiceStatusProcess;
987         rec = (struct record_service *)(table->data + offset);
988         rec->accept_pause = (status->dwControlsAccepted & SERVICE_ACCEPT_PAUSE_CONTINUE) ? -1 : 0;
989         rec->accept_stop  = (status->dwControlsAccepted & SERVICE_ACCEPT_STOP) ? -1 : 0;
990         rec->displayname  = heap_strdupW( services[i].lpDisplayName );
991         rec->name         = heap_strdupW( services[i].lpServiceName );
992         rec->process_id   = status->dwProcessId;
993         rec->servicetype  = get_service_type( status->dwServiceType );
994         rec->startmode    = get_service_startmode( config->dwStartType );
995         rec->state        = get_service_state( status->dwCurrentState );
996         rec->systemname   = heap_strdupW( sysnameW );
997         heap_free( config );
998         offset += sizeof(*rec);
999         num_rows++;
1000     }
1001
1002     TRACE("created %u rows\n", num_rows);
1003     table->num_rows = num_rows;
1004
1005 done:
1006     CloseServiceHandle( manager );
1007     heap_free( services );
1008 }
1009
1010 static UINT32 get_bits_per_pixel( UINT *hres, UINT *vres )
1011 {
1012     HDC hdc = GetDC( NULL );
1013     UINT32 ret;
1014
1015     if (!hdc) return 32;
1016     ret = GetDeviceCaps( hdc, BITSPIXEL );
1017     *hres = GetDeviceCaps( hdc, HORZRES );
1018     *vres = GetDeviceCaps( hdc, VERTRES );
1019     ReleaseDC( NULL, hdc );
1020     return ret;
1021 }
1022
1023 static void fill_videocontroller( struct table *table )
1024 {
1025
1026     struct record_videocontroller *rec;
1027     HRESULT hr;
1028     IDXGIFactory *factory = NULL;
1029     IDXGIAdapter *adapter = NULL;
1030     DXGI_ADAPTER_DESC desc;
1031     UINT hres = 1024, vres = 768, vidmem = 512 * 1024 * 1024;
1032     const WCHAR *name = videocontroller_deviceidW;
1033
1034     if (!(table->data = heap_alloc( sizeof(*rec) ))) return;
1035
1036     hr = CreateDXGIFactory( &IID_IDXGIFactory, (void **)&factory );
1037     if (FAILED(hr)) goto done;
1038
1039     hr = IDXGIFactory_EnumAdapters( factory, 0, &adapter );
1040     if (FAILED(hr)) goto done;
1041
1042     hr = IDXGIAdapter_GetDesc( adapter, &desc );
1043     if (SUCCEEDED(hr))
1044     {
1045         vidmem = desc.DedicatedVideoMemory;
1046         name   = desc.Description;
1047     }
1048
1049 done:
1050     rec = (struct record_videocontroller *)table->data;
1051     rec->adapter_ram           = vidmem;
1052     rec->current_bitsperpixel  = get_bits_per_pixel( &hres, &vres );
1053     rec->current_horizontalres = hres;
1054     rec->current_verticalres   = vres;
1055     rec->description           = heap_strdupW( name );
1056     rec->device_id             = videocontroller_deviceidW;
1057     rec->name                  = heap_strdupW( name );
1058
1059     TRACE("created 1 row\n");
1060     table->num_rows = 1;
1061
1062     if (adapter) IDXGIAdapter_Release( adapter );
1063     if (factory) IDXGIFactory_Release( factory );
1064 }
1065
1066 static struct table builtin_classes[] =
1067 {
1068     { class_baseboardW, SIZEOF(col_baseboard), col_baseboard, SIZEOF(data_baseboard), (BYTE *)data_baseboard },
1069     { class_biosW, SIZEOF(col_bios), col_bios, SIZEOF(data_bios), (BYTE *)data_bios },
1070     { class_compsysW, SIZEOF(col_compsys), col_compsys, 0, NULL, fill_compsys },
1071     { class_logicaldiskW, SIZEOF(col_logicaldisk), col_logicaldisk, 0, NULL, fill_logicaldisk },
1072     { class_networkadapterW, SIZEOF(col_networkadapter), col_networkadapter, 0, NULL, fill_networkadapter },
1073     { class_osW, SIZEOF(col_os), col_os, 0, NULL, fill_os },
1074     { class_paramsW, SIZEOF(col_params), col_params, SIZEOF(data_params), (BYTE *)data_params },
1075     { class_processW, SIZEOF(col_process), col_process, 0, NULL, fill_process },
1076     { class_processorW, SIZEOF(col_processor), col_processor, 0, NULL, fill_processor },
1077     { class_serviceW, SIZEOF(col_service), col_service, 0, NULL, fill_service },
1078     { class_stdregprovW, SIZEOF(col_stdregprov), col_stdregprov, SIZEOF(data_stdregprov), (BYTE *)data_stdregprov },
1079     { class_videocontrollerW, SIZEOF(col_videocontroller), col_videocontroller, 0, NULL, fill_videocontroller }
1080 };
1081
1082 void init_table_list( void )
1083 {
1084     static struct list tables = LIST_INIT( tables );
1085     UINT i;
1086
1087     for (i = 0; i < SIZEOF(builtin_classes); i++)
1088     {
1089         list_add_tail( &tables, &builtin_classes[i].entry );
1090     }
1091     table_list = &tables;
1092 }