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