My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
#include "SysInfo_in.h"


/////////////////////////////////////////////////////////////////////
// Hardware Info
#if defined(PLATFORM_WIN32)

bool GetWMIInfo(String system, Array <String> &data, Array <Value> *ret[], String nameSpace = "root\\cimv2")
{
HRESULT hRes;

hRes = CoInitialize(NULL);
if (hRes != S_OK && hRes != S_FALSE)
return false;

hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
if (hRes != S_OK && hRes != RPC_E_TOO_LATE) {
CoUninitialize();
return false;
}
IWbemLocator* pIWbemLocator = NULL;
if (CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **)&pIWbemLocator) != S_OK) {
CoUninitialize();
return false;
}

BSTR bstrNamespace = SysAllocString(nameSpace.ToWString());
IWbemServices* pWbemServices = NULL;
if (pIWbemLocator->ConnectServer(bstrNamespace, NULL, NULL, NULL, 0, NULL, NULL,
&pWbemServices) != S_OK) {
CoUninitialize();
return false;
}
SysFreeString(bstrNamespace);

hRes = CoSetProxyBlanket(pWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hRes)) {
pWbemServices->Release();
pIWbemLocator->Release();
CoUninitialize();
return false;
}

IEnumWbemClassObject* pEnumerator = NULL;
String query;
query << "Select * from " << system;
WCHAR wquery[1024*sizeof(WCHAR)];
MultiByteToWideChar(CP_UTF8, 0, query, -1, wquery, sizeof(wquery)/sizeof(wquery[0]));
BSTR strQuery = SysAllocString(wquery);
BSTR strQL = SysAllocString(L"WQL");
hRes = pWbemServices->ExecQuery(strQL, strQuery, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (hRes != S_OK) {
pWbemServices->Release();
pIWbemLocator->Release();
CoUninitialize();
return false;
}
SysFreeString(strQuery);
SysFreeString(strQL);

IWbemClassObject *pClassObject;
ULONG uReturn = 0;
int row = 0;
bool rt = false;
while (pEnumerator) {
hRes = pEnumerator->Next(WBEM_INFINITE, 1, &pClassObject, &uReturn);
if(0 == uReturn) {
if (rt)
break;
else {
pIWbemLocator->Release();
pWbemServices->Release();
pEnumerator->Release();
CoUninitialize();
return false;
}
}
if(hRes != S_OK) {
pWbemServices->Release();
pIWbemLocator->Release();
pEnumerator->Release();
pClassObject->Release();
CoUninitialize();
return false;
}
for (int col = 0; col < data.GetCount(); ++col) {
VARIANT vProp;
BSTR strClassProp = SysAllocString(data[col].ToWString());
hRes = pClassObject->Get(strClassProp, 0, &vProp, 0, 0);
if(hRes != S_OK){
pWbemServices->Release();
pIWbemLocator->Release();
pEnumerator->Release();
pClassObject->Release();
CoUninitialize();
return false;
}
SysFreeString(strClassProp);
ret[col]->Add(GetVARIANT(vProp));
rt = true;
}
row++;
}
pIWbemLocator->Release();
pWbemServices->Release();
pEnumerator->Release();
pClassObject->Release();
CoUninitialize();

return true;
}

bool GetWMIInfo(String system, String data, Value &res, String nameSpace = "root\\cimv2") {
Array <Value> arrRes;
Array <Value> *arrResP[1];
arrResP[0] = &arrRes;
Array <String> arrData;
arrData.Add(data);
bool ret = GetWMIInfo(system, arrData, arrResP, nameSpace);
if (ret)
res = arrRes[0];
return ret;
}

void GetSystemInfo(String &manufacturer, String &productName, String &version, int &numberOfProcessors, String &mbSerial) {
manufacturer = "";
Value vmanufacturer;
if (GetWMIInfo("Win32_ComputerSystem", "manufacturer", vmanufacturer))
manufacturer = Trim(vmanufacturer);
if (manufacturer.IsEmpty())
manufacturer = FromSystemCharset(GetWinRegString("SystemManufacturer", "HARDWARE\\DESCRIPTION\\System\\BIOS", HKEY_LOCAL_MACHINE));
if (manufacturer.IsEmpty()) {
StringParse fileData = LoadFile(AppendFileName(GetSystemFolder(), "oeminfo.ini"));
fileData.GoAfter("Manufacturer=");
manufacturer = fileData.GetText("\r\n");
}
productName = "";
Value vproductName;
if (GetWMIInfo("Win32_ComputerSystem", "model", vproductName))
productName = Trim(vproductName);
if (productName.IsEmpty())
productName = FromSystemCharset(GetWinRegString("SystemProductName", "HARDWARE\\DESCRIPTION\\System\\BIOS", HKEY_LOCAL_MACHINE));
if (productName.IsEmpty())
productName = FromSystemCharset(GetWinRegString("Model", "SOFTWARE\\Microsoft\\PCHealth\\HelpSvc\\OEMInfo", HKEY_LOCAL_MACHINE));

version = FromSystemCharset(GetWinRegString("SystemVersion", "HARDWARE\\DESCRIPTION\\System\\BIOS", HKEY_LOCAL_MACHINE));
numberOfProcessors = atoi(GetEnv("NUMBER_OF_PROCESSORS"));
Value vmbSerial;
if (GetWMIInfo("Win32_BaseBoard", "SerialNumber", vmbSerial))
mbSerial = Trim(vmbSerial);
}

void GetBiosInfo(String &biosVersion, Date &biosReleaseDate, String &biosSerial) {
// Alternative is "wmic bios get name" and "wmic bios get releasedate"
String strBios = FromSystemCharset(GetWinRegString("BIOSVersion", "HARDWARE\\DESCRIPTION\\System\\BIOS", HKEY_LOCAL_MACHINE));
if (strBios.IsEmpty())
strBios = FromSystemCharset(GetWinRegString("SystemBiosVersion", "HARDWARE\\DESCRIPTION\\System", HKEY_LOCAL_MACHINE));
for (int i = 0; i < strBios.GetLength(); ++i) {
if (strBios[i] == '\0')
biosVersion.Cat(". ");
else
biosVersion.Cat(strBios[i]);
}
String strDate = FromSystemCharset(GetWinRegString("BIOSReleaseDate", "HARDWARE\\DESCRIPTION\\System\\BIOS", HKEY_LOCAL_MACHINE));
if (strDate.IsEmpty())
strDate = FromSystemCharset(GetWinRegString("SystemBiosDate", "HARDWARE\\DESCRIPTION\\System", HKEY_LOCAL_MACHINE));
int lang = GetCurrentLanguage();
SetLanguage(LNG_ENGLISH);
if (!StrToDate(biosReleaseDate, strDate))
biosReleaseDate = Null;
SetLanguage(lang);
Value vmbSerial;
if (GetWMIInfo("Win32_BIOS", "SerialNumber", vmbSerial))
biosSerial = Trim(vmbSerial);
}

bool GetProcessorInfo(int number, String &vendor, String &identifier, String &architecture, int &speed) {
String strReg = Format("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d", number);
vendor = FromSystemCharset(GetWinRegString("VendorIdentifier", strReg, HKEY_LOCAL_MACHINE));
identifier = FromSystemCharset(GetWinRegString("ProcessorNameString", strReg, HKEY_LOCAL_MACHINE));
architecture = FromSystemCharset(GetWinRegString("Identifier", strReg, HKEY_LOCAL_MACHINE));
speed = GetWinRegInt("~MHz", strReg, HKEY_LOCAL_MACHINE);

return true;
}
/*
String GetMacAddressWMI() {
Value vmac;
if (!GetWMIInfo("Win32_NetworkAdapterConfiguration", "MacAddress", vmac))
return Null;
String mac = Trim(vmac);
if (mac.GetCount() > 0)
return mac;
return Null;
}
*/
#include <winsock2.h>
#include <iphlpapi.h>

Array <NetAdapter> GetAdapterInfo() {
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
ULONG family = AF_UNSPEC;
DWORD flags = GAA_FLAG_INCLUDE_PREFIX;
ULONG outBufLen = 0;
Buffer<BYTE> pBuffer;
Array <NetAdapter> ret;

switch (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen)) {
case ERROR_BUFFER_OVERFLOW:
pBuffer.Alloc(outBufLen);
pAddresses = (PIP_ADAPTER_ADDRESSES)~pBuffer;
break;
case ERROR_NO_DATA:
case ERROR_INVALID_FUNCTION:
default:
return ret;
}
if (NO_ERROR != GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen))
return ret;

for (PIP_ADAPTER_ADDRESSES pAdd = pAddresses; pAdd; pAdd = pAdd->Next) {
NetAdapter &adapter = ret.Add();
int len = min((DWORD)6, pAdd->PhysicalAddressLength);
if (len > 0)
adapter.mac = ToUpper(HexString(pAdd->PhysicalAddress, len, 1, ':'));
adapter.description = Trim(WideToString(pAdd->Description));
adapter.fullname = Trim(WideToString(pAdd->FriendlyName));
switch (pAdd->IfType) {
case IF_TYPE_ETHERNET_CSMACD: adapter.type = "ETHERNET"; break;
case IF_TYPE_ISO88025_TOKENRING: adapter.type = "TOKENRING"; break;
case IF_TYPE_PPP: adapter.type = "MODEM"; break;
case IF_TYPE_SOFTWARE_LOOPBACK: adapter.type = "SOFTWARE_LOOPBACK"; break;
case IF_TYPE_ATM: adapter.type = "ATM"; break;
case IF_TYPE_IEEE80211: adapter.type = "IEEE80211"; break;
case IF_TYPE_TUNNEL: adapter.type = "TUNNEL"; break;
case IF_TYPE_IEEE1394: adapter.type = "IEEE1394"; break;
default: adapter.type = "OTHER";
}
if (adapter.type == "ETHERNET" && ToLower(adapter.description).Find("wireless") >= 0)
adapter.type = "IEEE80211";
}
return ret;
}

String GetHDSerial() {
Value vmbSerial;
if (!GetWMIInfo("Win32_PhysicalMedia", "SerialNumber", vmbSerial))
return Null;
String serial = Trim(vmbSerial);
if (serial.GetCount() > 0)
return serial;
return Null;
}

bool GetVideoInfo(Array <Value> &name, Array <Value> &description, Array <Value> &videoProcessor, Array <Value> &ram, Array <Value> &videoMode) {
Array <Value> *res[5];
res[0] = &name;
res[1] = &description;
res[2] = &videoProcessor;
res[3] = &ram;
res[4] = &videoMode;
Array <String> data;
data.Add("Name");
data.Add("Description");
data.Add("VideoProcessor");
data.Add("AdapterRAM");
data.Add("VideoModeDescription");
if (!GetWMIInfo("Win32_VideoController", data, res))
return false;

for (int row = 0; row < ram.GetCount(); ++row)
ram[row] = (atoi(ram[row].ToString()) + 524288)/1048576;
return true;
}

bool GetPackagesInfo(Array <Value> &name, Array <Value> &version, Array <Value> &vendor,
Array <Value> &installDate, Array <Value> &caption, Array <Value> &description, Array <Value> &state)
{
Array <Value> *res[7];
res[0] = &name;
res[1] = &version;
res[2] = &vendor;
res[3] = &installDate;
res[4] = &caption;
res[5] = &description;
res[6] = &state;
Array <String> data;
data.Add("Name");
data.Add("Version");
data.Add("Vendor");
data.Add("InstallDate2");
data.Add("Caption");
data.Add("Description");
data.Add("InstallState");
if (!GetWMIInfo("Win32_Product", data, res))
return false;

for (int i = 0; i < installDate.GetCount(); ++i) {
String sdate = installDate[i];
Time t(atoi(sdate.Left(4)), atoi(sdate.Mid(4, 2)), atoi(sdate.Mid(6, 2)),
atoi(sdate.Mid(8, 2)), atoi(sdate.Mid(10, 2)), atoi(sdate.Mid(12, 2)));
installDate[i] = t;
int istate = state[i];
switch (istate) {
case -6: state[i] = "Bad Configuration"; break;
case -2: state[i] = "Invalid Argument"; break;
case -1: state[i] = "Unknown Package"; break;
case 1: state[i] = "Advertised"; break;
case 2: state[i] = "Absent"; break;
case 5: state[i] = "Ok"; break;
default: return false;
}
}
return true;
}

double GetCpuTemperature() {
Value data;
if (GetWMIInfo("MSAcpi_ThermalZoneTemperature", "CurrentTemperature", data, "root\\wmi"))
return (double(data) - 2732.) / 10.;
if (GetWMIInfo("Win32_TemperatureProbe", "CurrentReading", data))
return data;
return Null;
}
#endif
/*
String GetMacAddress() {
Array <NetAdapter> ret = GetAdapterInfo();
if (!ret.IsEmpty())
return ret[0].mac;
else
return Null;
}
*/

void NetAdapter::Copy(const NetAdapter& src) {
description <<= src.description;
fullname <<= src.fullname;
mac <<= src.mac;
type <<= src.type;
}

void NetAdapter::Xmlize(XmlIO xml) {
xml ("description", description)("fullname", fullname)("mac", mac)("type", type);
}

void NetAdapter::Jsonize(JsonIO& json) {
json ("description", description)("fullname", fullname)("mac", mac)("type", type);
}

void NetAdapter::Serialize(Stream& stream) {
stream % description % fullname % mac % type;
}


#if defined (PLATFORM_POSIX)
void GetSystemInfo(String &manufacturer, String &productName, String &version, int &numberOfProcessors, String &mbSerial)
{
manufacturer = LoadFile_Safe("/sys/devices/virtual/dmi/id/board_vendor");
productName = LoadFile_Safe("/sys/devices/virtual/dmi/id/board_name");
version = LoadFile_Safe("/sys/devices/virtual/dmi/id/product_version");
mbSerial = LoadFile_Safe("/sys/devices/virtual/dmi/id/board_serial");
if (mbSerial.IsEmpty())
mbSerial = FormatInt(gethostid());

StringParse cpu(LoadFile_Safe("/proc/cpuinfo"));
numberOfProcessors = 1;
while (cpu.GoAfter("processor")) {
cpu.GoAfter(":");
numberOfProcessors = atoi(cpu.GetText()) + 1;
}
}

void GetBiosInfo(String &biosVersion, Date &biosReleaseDate, String &biosSerial)
{
String biosVendor = LoadFile_Safe("/sys/devices/virtual/dmi/id/bios_vendor");
biosVersion = LoadFile_Safe("/sys/devices/virtual/dmi/id/bios_version");
StrToDate(biosReleaseDate, LoadFile_Safe("/sys/devices/virtual/dmi/id/bios_date"));
biosSerial = LoadFile_Safe("/sys/devices/virtual/dmi/id/chassis_serial");
}

bool GetProcessorInfo(int number, String &vendor, String &identifier, String &architecture, int &speed)
{
StringParse cpu(LoadFile_Safe("/proc/cpuinfo"));

int cpuNumber;
do {
if (!cpu.GoAfter("processor", ":"))
return false;
String sCpu = cpu.GetText();
if (sCpu == "")
return false;
cpuNumber = atoi(sCpu);
} while (cpuNumber != number);

cpu.GoAfter("vendor_id", ":");
vendor = cpu.GetText();
cpu.GoAfter("cpu family", ":");
String family = cpu.GetText(); // 6 means 686
cpu.GoAfter("model", ":");
String model = cpu.GetText();
cpu.GoAfter("model name", ":");
identifier = cpu.GetText("\n");
cpu.GoAfter("stepping", ":");
String stepping = cpu.GetText();
architecture = Sys("uname -m"); // CPU type
architecture << " Family " << family << " Model " << model << " Stepping " << stepping; // And 64 bits ?? uname -m
cpu.GoAfter_Init("cpu MHz", ":");
speed = cpu.GetInt();
}

double GetCpuTemperature() {
/* This is only if acpi package is present
StringParse data = Sys("acpi -V");
data.GoAfter("Thermal", ",");
return data.GetDouble();
*/
FindFile ff;
if(ff.Search("/proc/acpi/thermal_zone/*")) {
do {
if (ff.IsDirectory()) {
String name = ff.GetName();
if (name != "." && name != "..") {
StringParse str = LoadFile_Safe(AppendFileName(AppendFileName("/proc/acpi/thermal_zone", name), "temperature"));
str.GoAfter("temperature:");
return str.GetDouble();
}
}
} while(ff.Next());
}
return Null;
}

Array <NetAdapter> GetAdapterInfo() {
Array <NetAdapter> ret;

StringParse data = Sys("ifconfig -a");
for (StringParse line = data.GetLine(); !data.Eof(); line = data.GetLine()) {
NetAdapter &adapter = ret.Add();
String str = line.GetText();
if (str.Find("eth") >= 0)
adapter.type = "ETHERNET";
else if (str.Find("lo") >= 0)
adapter.type = "SOFTWARE_LOOPBACK";
else if (str.Find("ppp") >= 0)
adapter.type = "MODEM";
else if (str.Find("hci") >= 0)
adapter.type = "BLUETOOTH";
else if (str.Find("tr") >= 0)
adapter.type = "TOKENRING";
else if (str.Find("vbox") >= 0 || str.Find("wifi") >= 0 || str.Find("ath") >= 0)
adapter.type = "VIRTUALBOX";
else if (str.Find("wlan") >= 0)
adapter.type = "IEEE80211";
else
adapter.type = "OTHER";
line.GoAfter(":");
int pos = line.GetPos();
int npos = line.Find(" ", pos);
if (npos >= 0) {
adapter.fullname = line.Mid(pos, npos-pos);
line.SetPos(npos+1);
}
if (line.GetText() != "")
adapter.mac = ToUpper(Trim(line.GetText()));
do {
data.GoAfter("\n");
} while (data[data.GetPos()] == ' ');
}
return ret;
}

// Not implemented yet
String GetHDSerial() {
return Null;
}

#endif

/////////////////////////////////////////////////////////////////////
// Memory Info

#if defined(PLATFORM_WIN32)

bool GetMemoryInfo(
int &memoryLoad, // percent of memory in use
uint64 &totalPhys, // physical memory
uint64 &freePhys, // free physical memory
uint64 &totalPageFile, // total paging file
uint64 &freePageFile, // free paging file
uint64 &totalVirtual, // total virtual memory
uint64 &freeVirtual) // free virtual memory
{
MEMORYSTATUSEX status;
status.dwLength = sizeof (status);
if (!GlobalMemoryStatusEx(&status))
return false;
memoryLoad = status.dwMemoryLoad;
totalPhys = status.ullTotalPhys;
freePhys = status.ullAvailPhys;
totalPageFile = status.ullTotalPageFile;
freePageFile = status.ullAvailPageFile;
totalVirtual = status.ullTotalVirtual;
freeVirtual = status.ullAvailVirtual;

return true;
}

#else

bool GetMemoryInfo(
int &memoryLoad, // percent of memory in use
uint64 &totalPhys, // physical memory
uint64 &freePhys, // free physical memory
uint64 &totalPageFile, // total paging file
uint64 &freePageFile, // free paging file
uint64 &totalVirtual, // total virtual memory
uint64 &freeVirtual)
{
StringParse meminfo = LoadFile_Safe("/proc/meminfo");
if (meminfo == "")
return false;
meminfo.GoAfter_Init("MemTotal", ":"); totalPhys = 1024*meminfo.GetUInt64();
meminfo.GoAfter_Init("MemFree", ":"); freePhys = 1024*meminfo.GetUInt64();
memoryLoad = (int)(100.*(totalPhys-freePhys)/totalPhys);
meminfo.GoAfter_Init("SwapCached", ":");freePageFile = 1024*meminfo.GetUInt64();
meminfo.GoAfter_Init("Cached", ":"); totalPageFile = 1024*meminfo.GetUInt64() + freePageFile;
meminfo.GoAfter_Init("SwapTotal", ":"); totalVirtual = 1024*meminfo.GetUInt64();
meminfo.GoAfter_Init("SwapFree", ":"); freeVirtual = 1024*meminfo.GetUInt64();

return true;
}
#endif


/////////////////////////////////////////////////////////////////////
// Process list

#if defined(PLATFORM_WIN32)

// Get the list of process identifiers.
bool GetProcessList(Array<long> &pid, Array<String> &pNames)
{
PROCESSENTRY32 proc;
HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
return false;
proc.dwSize = sizeof(proc);
long f = Process32First(hSnap, &proc);
while (f) {
pid.Add(proc.th32ProcessID);
pNames.Add(proc.szExeFile);
f = Process32Next(hSnap, &proc);
}
CloseHandle(hSnap);
return true;
}
Array<long> GetProcessList()
{
PROCESSENTRY32 proc;
Array<long> ret;
HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
return ret;
proc.dwSize = sizeof(proc);
long f = Process32First(hSnap, &proc);
while (f) {
ret.Add(proc.th32ProcessID);
f = Process32Next(hSnap, &proc);
}
CloseHandle(hSnap);
return ret;
}
String GetProcessName(long processID)
{
WCHAR szProcessName[MAX_PATH];
String ret;

// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);

// Get the process name.
if (hProcess != NULL) {
HMODULE hMod;
DWORD cbNeeded;

if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
GetModuleBaseNameW(hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(WCHAR));
ret = FromSystemCharset(WString(szProcessName).ToString());
}
}
CloseHandle(hProcess);

return ret;
}

String GetProcessFileName(long processID)
{
WCHAR szProcessName[MAX_PATH];
String ret;

// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);

// Get the process name.
if (hProcess != NULL) {
HMODULE hMod;
DWORD cbNeeded;

if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
GetModuleFileNameExW(hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(WCHAR));
ret = FromSystemCharset(WString(szProcessName).ToString());
}
}
CloseHandle(hProcess);

return ret;
}

BOOL CALLBACK EnumGetWindowsList(HWND hWnd, LPARAM lParam)
{
if (!hWnd)
return TRUE; // Not a window
if (GetParent(hWnd) != 0)
return TRUE; // Child window
Array<int> *ret = (Array<int> *)lParam;
ret->Add((int)hWnd);
return TRUE;
}

void GetWindowsList(Array<long> &hWnd, Array<long> &processId, Array<String> &name, Array<String> &fileName, Array<String> &caption)
{
HANDLE hProcess;
DWORD dwThreadId, dwProcessId;
HINSTANCE hInstance;
WCHAR str[MAX_PATH];

EnumWindows(EnumGetWindowsList, (LPARAM)&hWnd);
for (int i = 0; i < hWnd.GetCount(); ++i) {
hInstance = (HINSTANCE)GetWindowLongPtr((HWND)hWnd[i], GWLP_HINSTANCE);
dwThreadId = GetWindowThreadProcessId((HWND)hWnd[i], &dwProcessId);
processId.Add(dwProcessId);
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
if (GetModuleFileNameExW(hProcess, hInstance, str, sizeof(str)/sizeof(WCHAR)))
fileName.Add(FromSystemCharset(WString(str).ToString()));
else
fileName.Add(t_("UNKNOWN"));
if (GetModuleBaseNameW(hProcess, hInstance, str, sizeof(str)/sizeof(WCHAR)))
name.Add(FromSystemCharset(WString(str).ToString()));
else
name.Add(t_("UNKNOWN"));
CloseHandle(hProcess);
if (IsWindowVisible((HWND)hWnd[i])) {
int count = int(SendMessageW((HWND)hWnd[i], WM_GETTEXT, sizeof(str)/sizeof(WCHAR), (LPARAM)str));
str[count] = '\0';
caption.Add(FromSystemCharset(WString(str).ToString()));
} else
caption.Add("");
}
}

Array<long> GetWindowsList()
{
Array<long> ret;
EnumWindows(EnumGetWindowsList, (LPARAM)&ret);
return ret;
}

BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam)
{
DWORD dwID ;
GetWindowThreadProcessId(hwnd, &dwID) ;
if(dwID == (DWORD)lParam)
PostMessage(hwnd, WM_CLOSE, 0, 0) ;
return TRUE ;
}

// pid Process ID of the process to shut down.
// timeout Wait time in milliseconds before shutting down the process.
bool ProcessTerminate(long processId, int timeout)
{
// If we can't open the process with PROCESS_TERMINATE rights, then we give up immediately.
HANDLE hProc = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, processId);
if(hProc == NULL)
return false ;
// TerminateAppEnum() posts WM_CLOSE to all windows whose PID matches your process's.
::EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)processId) ;

int ret;
// Wait on the handle. If it signals, great. If it times out,
// then you kill it.
int state = ::WaitForSingleObject(hProc, timeout);
if ((state == WAIT_TIMEOUT) || (state == WAIT_FAILED))
ret = ::TerminateProcess(hProc, 0);
else
ret = true;
CloseHandle(hProc) ;
return ret;
}

int GetProcessPriority(long pid)
{
int priority;
HANDLE hProc = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if(hProc == NULL)
return -1;
priority = ::GetPriorityClass(hProc);
CloseHandle(hProc);

switch(priority) {
case REALTIME_PRIORITY_CLASS: priority = 10; // Process that has the highest possible priority. The threads of a real-time priority class process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.
break;
case HIGH_PRIORITY_CLASS: priority = 8; // Process that performs time-critical tasks that must be executed immediately for it to run correctly. The threads of a high-priority class process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class CPU-bound application can use nearly all available cycles.
break;
case ABOVE_NORMAL_PRIORITY_CLASS: priority = 6; // Process that has priority above NORMAL_PRIORITY_CLASS but below HIGH_PRIORITY_CLASS.
break;
case NORMAL_PRIORITY_CLASS: priority = 5; // Process with no special scheduling needs.
break;
case BELOW_NORMAL_PRIORITY_CLASS: priority = 3; // Process that has priority above IDLE_PRIORITY_CLASS but below NORMAL_PRIORITY_CLASS.
break;
case IDLE_PRIORITY_CLASS: priority = 0; // Process whose threads run only when the system is idle and are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle priority class is inherited by child processes.
break;
default: return -1;
}
return priority;
}

bool SetProcessPriority(long pid, int priority)
{
HANDLE hProc = ::OpenProcess(PROCESS_SET_INFORMATION , FALSE, pid);
if(hProc == NULL)
return false;
if (priority == 10)
priority = REALTIME_PRIORITY_CLASS;
else if (priority >= 8)
priority = HIGH_PRIORITY_CLASS;
else if (priority >= 6)
priority = ABOVE_NORMAL_PRIORITY_CLASS;
else if (priority >= 5)
priority = NORMAL_PRIORITY_CLASS;
else if (priority >= 3)
priority = BELOW_NORMAL_PRIORITY_CLASS;
else
priority = IDLE_PRIORITY_CLASS;
int ret = ::SetPriorityClass(hProc, priority); // SetProcessAffinityMask
CloseHandle(hProc) ;
return ret;
}

#endif
#ifdef PLATFORM_POSIX //Check with ps

bool IsInteger(String s)
{
for (int i = 0; i < s.GetCount(); ++i) {
if (!isdigit(s[i]))
return false;
}
return true;
}
bool GetProcessList(Array<long> &pid, Array<String> &pNames)
{
FindFile ff;
if(ff.Search("/proc/*")) {
do {
if (IsInteger(ff.GetName())) {
String exe = Format("/proc/%s/exe", ff.GetName());
StringBuffer exeb;
exeb = exe;
char procName[2048];
int procNameLen = readlink(exeb, procName, sizeof(procName)-1);
if (procNameLen > 0) {
procName[procNameLen] = 0;
pNames.Add(procName);
pid.Add(atoi(ff.GetName()));
}
}
} while(ff.Next());
}
return true;
}
Array<long> GetProcessList()
{
FindFile ff;
Array<long> pid;
if(ff.Search("/proc/*")) {
do {
if (IsInteger(ff.GetName())) {
String exe = Format("/proc/%s/exe", ff.GetName());
StringBuffer exeb;
exeb = exe;
char procName[2048];
int procNameLen = readlink(exeb, procName, sizeof(procName)-1);
if (procNameLen > 0)
pid.Add(atoi(ff.GetName()));
}
} while(ff.Next());
}
return pid;
}
String GetProcessName(long pid)
{
return GetFileName(GetProcessFileName(pid));
}
// ls -l /proc/%d/fd gets also the files opened by the process
String GetProcessFileName(long pid)
{
String ret = "";
String exe = Format("/proc/%s/exe", FormatLong(pid));
StringBuffer exeb;
exeb = exe;
char procName[2048];
int procNameLen = readlink(exeb, procName, sizeof(procName)-1);
if (procNameLen > 0) {
procName[procNameLen] = 0;
ret = procName;
}
return ret;
}

#ifdef flagGUI
//#define SetX11ErrorHandler() {}
void SetSysInfoX11ErrorHandler() {return;}
#else
//#define SetX11ErrorHandler() {}
int SysInfoX11ErrorHandler(_XDisplay *, XErrorEvent *) {return 0;}
void SetSysInfoX11ErrorHandler() {XSetErrorHandler(SysInfoX11ErrorHandler);}
#endif

void GetWindowsList_Rec (_XDisplay *dpy, Window w, int depth, Array<long> &wid)
{
if (depth > 3) // 1 is enough for Gnome. 2 is necessary for Xfce and Kde
return;

wid.Add(w);

Window root, parent;
unsigned int nchildren;
Window *children = NULL;
if (XQueryTree (dpy, w, &root, &parent, &children, &nchildren)) {
for (int i = 0; i < nchildren; i++) {
XWindowAttributes windowattr;
XGetWindowAttributes(dpy, children[i], &windowattr);
if (windowattr.map_state == IsViewable)
GetWindowsList_Rec (dpy, children[i], depth + 1, wid);
}
}
if (children)
XFree((char *)children);
return;
}

Array<long> GetWindowsList()
{
Array<long> ret;
SetSysInfoX11ErrorHandler();

_XDisplay *dpy = XOpenDisplay (NULL);
if (!dpy) {
SetX11ErrorHandler();
return ret;
}
GetWindowsList_Rec (dpy, RootWindow(dpy, DefaultScreen(dpy)), 0, ret);
XCloseDisplay (dpy);
SetX11ErrorHandler();
return ret;
}

void GetWindowsList(Array<long> &hWnd, Array<long> &processId, Array<String> &nameL, Array<String> &fileName, Array<String> &caption)
{
SetSysInfoX11ErrorHandler();
_XDisplay *dpy = XOpenDisplay (NULL);
if (!dpy) {
SetX11ErrorHandler();
return;
}
GetWindowsList_Rec(dpy, RootWindow (dpy, DefaultScreen (dpy)), 0, hWnd);
for (int i = 0; i < hWnd.GetCount(); ++i) {
// Get window name
XTextProperty tp;
if (XGetWMName(dpy, hWnd[i], &tp) == 0)
caption.Add("");
else {
if (tp.nitems > 0) {
int count = 0, i, ret;
char **list = NULL;
ret = XmbTextPropertyToTextList(dpy, &tp, &list, &count);
if((ret == Success || ret > 0) && list != NULL) {
String sret;
for(i = 0; i < count; i++)
sret << list[i]; // << " ";
XFreeStringList(list);
caption.Add(FromSystemCharset(sret));
} else
caption.Add(FromSystemCharset((char *)tp.value));
} else
caption.Add("");
}
// Get pid
Atom atomPID = XInternAtom(dpy, "_NET_WM_PID", true);
unsigned long pid = 0;
if (atomPID == None)
processId.Add(0L);
else {
Atom type;
int format;
unsigned long nItems;
unsigned long bytesAfter;
unsigned char *propPID = 0;
if (0 == XGetWindowProperty(dpy, hWnd[i], atomPID, 0, 1024, false, XA_CARDINAL, &type, &format, &nItems, &bytesAfter, &propPID)) {
if(propPID != 0) {
pid = *((unsigned long *)propPID);
processId.Add(pid);
XFree(propPID);
} else
processId.Add(0L);
} else
processId.Add(0L);
}
if (pid != 0L)
fileName.Add(GetProcessFileName(pid));
else
fileName.Add("");
// Name and class
XClassHint ch;
ch.res_name = ch.res_class = NULL;
Status status = XGetClassHint (dpy, hWnd[i], &ch);
if (status != BadWindow) {
if (ch.res_name)
nameL.Add(ch.res_name);
else
nameL.Add("");
} else
nameL.Add("");
if (ch.res_name)
XFree (ch.res_name);
if (ch.res_class)
XFree (ch.res_class);
}
XCloseDisplay (dpy);
SetX11ErrorHandler();
return;
}

bool WindowKill(long wid)
{
if (wid == 0)
return false;

_XDisplay *dpy = XOpenDisplay (NULL);
if (!dpy)
return false;

XSync (dpy, 0);
XKillClient (dpy, wid);
XSync (dpy, 0);

XCloseDisplay (dpy);
return true;
}

// Also possible to stop or cont
bool ProcessTerminate(long pid, int timeout)
{
if (!ProcessExists(pid))
return false;
long wid = GetWindowIdFromProcessId(pid); // Just in case

// First... SIGTERM
kill(pid, SIGTERM);
Sleep(timeout/3);
if (!ProcessExists(pid))
return true;
// Second... SIGKILL
kill(pid, SIGKILL);
Sleep(timeout/3);
if (!ProcessExists(pid))
return true;
// Third ... WindowKill
Sleep((int)(timeout/3));
return WindowKill(wid);
}

int GetProcessPriority(long pid)
{
int priority = getpriority(PRIO_PROCESS, pid);
return 10 - (priority + 20)/4; // Rescale -20/20 to 10/0
}

bool SetProcessPriority(long pid, int priority)
{
priority = 20 - 4*priority;
if (0 == setpriority(PRIO_PROCESS, pid, priority))
return true;
else
return false;
}
#endif

long GetWindowIdFromCaption(String windowCaption, bool exactMatch)
{
Array<long> wid, pid;
Array<String> name, fileName, caption;
GetWindowsList(wid, pid, name, fileName, caption);
for (int i = 0; i < wid.GetCount(); ++i) {
if (exactMatch) {
String s = caption[i];
if (caption[i] == windowCaption)
return wid[i];
} else {
if (caption[i].Find(windowCaption) >= 0)
return wid[i];
}
}
return -1;
}

long GetProcessIdFromWindowCaption(String windowCaption, bool exactMatch)
{
Array<long> wid, pid;
Array<String> name, fileName, caption;
GetWindowsList(wid, pid, name, fileName, caption);
for (int i = 0; i < wid.GetCount(); ++i) {
if (exactMatch) {
if (caption[i] == windowCaption)
return pid[i];
} else {
if (caption[i].Find(windowCaption) >= 0)
return pid[i];
}
}
return -1;
}

long GetProcessIdFromWindowId(long _wId)
{
Array<long> wId, pid;
Array<String> name, fileName, caption;
GetWindowsList(wId, pid, name, fileName, caption);
for (int i = 0; i < pid.GetCount(); ++i) {
if (wId[i] == _wId)
return pid[i];
}
return 0;
}

long GetWindowIdFromProcessId(long _pid)
{
Array<long> wId, pid;
Array<String> name, fileName, caption;
GetWindowsList(wId, pid, name, fileName, caption);
for (int i = 0; i < pid.GetCount(); ++i) {
if (pid[i] == _pid)
return wId[i];
}
return 0;
}

bool ProcessExists(long pid)
{
return DirectoryExists(Format("/proc/%s", FormatLong(pid)));
}

/////////////////////////////////////////////////////////////////////
// Others

long GetProcessId() {return getpid();}

/////////////////////////////////////////////////////////////////////
// Drives list
#if defined(PLATFORM_WIN32) || defined (PLATFORM_WIN64)

bool GetDriveSpace(String drive,
//uint64 &totalBytes, // To determine the total number of bytes on a disk or volume, use IOCTL_DISK_GET_LENGTH_INFO.
uint64 &freeBytesUser, // Total number of free bytes on a disk that are available to the user who is associated with the calling thread.
uint64 &totalBytesUser, // Total number of bytes on a disk that are available to the user who is associated with the calling thread.
uint64 &totalFreeBytes) // Total number of free bytes on a disk.
{
StringBuffer sb(drive);

if(!GetDiskFreeSpaceEx(sb, (PULARGE_INTEGER)&freeBytesUser, (PULARGE_INTEGER)&totalBytesUser, (PULARGE_INTEGER)&totalFreeBytes))
return false;
//totalBytes = 0;
return true;
}

// return true if mounted
bool GetDriveInformation(String drive, String &type, String &volume, /*uint64 &serial, */int &maxName, String &fileSystem)
{
StringBuffer sb(drive);

switch (::GetDriveType(sb)) {
case DRIVE_UNKNOWN: type = "Drive unknown"; break;
case DRIVE_NO_ROOT_DIR: type = "The root directory does not exist"; break;
case DRIVE_REMOVABLE:
switch (*sb) {
case 'A':
case 'B': type = "Floppy";
volume = fileSystem = "";
/*serial = */maxName = 0;
return false;
default: type = "Removable"; break;
}
break;
case DRIVE_FIXED: type = "Hard"; break;
case DRIVE_REMOTE: type = "Network"; break;
case DRIVE_CDROM: type = "Optical"; break;
case DRIVE_RAMDISK: type = "RAM"; break;
}
char vol[MAX_PATH], fs[MAX_PATH];
long flags;
uint64 serial;
uint64 _maxName;
if(!::GetVolumeInformation(sb, vol, MAX_PATH, (LPDWORD)&serial, (LPDWORD)&_maxName, (LPDWORD)&flags, fs, MAX_PATH)) {
if (type == "Optical") {
volume = "";
fileSystem = "";
maxName = 0;
return true;
} else
return false;
}
volume = vol;
fileSystem = fs;
maxName = (int)_maxName;

return true;
}

#elif defined(PLATFORM_POSIX)

bool GetDriveSpace(String drive,
//uint64 &totalBytes, // To determine the total number of bytes on a disk or volume, use IOCTL_DISK_GET_LENGTH_INFO.
uint64 &freeBytesUser, // Total number of free bytes on a disk that are available to the user who is associated with the calling thread.
uint64 &totalBytesUser, // Total number of bytes on a disk that are available to the user who is associated with the calling thread.
uint64 &totalFreeBytes) // Total number of free bytes on a disk.
{
freeBytesUser = totalBytesUser = totalFreeBytes = 0;

StringParse space = Sys("df -T");
if (space == "")
return false;

while (drive != space.GetText())
;
space.MoveRel(-10); space.GoBeginLine();
space.GetText(); space.GetText(); // Jumps over device path and filesystem
totalBytesUser = 1024*space.GetUInt64();
space.GetText(); // Jumps over used space
freeBytesUser = totalFreeBytes = 1024*space.GetUInt64();
return true;
}

// return true if mounted
bool GetDriveInformation(String drive, String &type, String &volume, /*uint64 &serial, */int &maxName, String &fileSystem)
{
StringParse info = Sys("mount -l");
if (info == "")
return false;

String straux;
while (drive != (straux = info.GetText()))
if (straux == "")
return false;

if("type" != info.GetText()) // Jumps over "type"
return false;

fileSystem = info.GetText();
String details = info.GetText();
info.GoAfter("[");
volume = info.GetText("]");
//serial = 0; // Unknown
if ((fileSystem == "udf" || fileSystem == "iso9660") && details.Find("ro") >=0)
type = "Optical";
else if (details.Find("flush") >=0)
type = "Removable";
else
type = "Hard";

struct statfs buf;
if (0 == statfs(drive, &buf))
//puts(Format("%x", buf.f_type)); // Filesystem type
maxName = buf.f_namelen;
else
maxName = 0;

return true;
}

#endif

#if defined(PLATFORM_WIN32) || defined (PLATFORM_WIN64)

#define SHTDN_REASON_MINOR_OTHER 0

bool Shutdown(String action) {
action = ToLower(action);

bool hibernate, suspend;
hibernate = suspend = false;

unsigned int flag;
if (action == "logoff")
flag = EWX_LOGOFF;
else if (action == "poweroff")
flag = EWX_POWEROFF;
else if (action == "reboot")
flag = EWX_REBOOT;
else if (action == "shutdown")
flag = EWX_SHUTDOWN;
else if (action == "hibernate")
hibernate = true;
else if (action == "suspend")
suspend = true;
else
return false;

HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;

// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);

if (GetLastError() != ERROR_SUCCESS)
return false;

if (hibernate || suspend) {
if (!SetSuspendState(hibernate, TRUE, FALSE))
return false;
} else {
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(flag | EWX_FORCE, SHTDN_REASON_MINOR_OTHER))
return false;
}
return true;
}
#endif

#ifdef PLATFORM_POSIX
bool Shutdown(String action)
{
if (action == "logoff") {
kill(1, SIGTSTP);
sync();
signal(SIGTERM, SIG_IGN);
setpgrp();
kill(-1, SIGTERM);
sleep(1);
kill(-1, SIGHUP); //* Force PPPD's down, too *
sleep(1);
kill(-1, SIGKILL);
sync();
sleep(1);
} else if (action == "shutdown") {
#if __GNU_LIBRARY__ > 5
reboot(0xCDEF0123);
#else
reboot(0xfee1dead, 672274793, 0xCDEF0123);
#endif
} else if (action == "reboot") { // LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2
#if __GNU_LIBRARY__ > 5
reboot(0x01234567);
#else
reboot(0xfee1dead, 672274793, 0x01234567);
#endif
}
exit(0);
return true;
}
#endif


void _GetCompilerInfo(String &name, int &version, String &mode) {
name = "";
version = 0;
#if defined(PLATFORM_WIN32) || defined (PLATFORM_WIN64)
#if defined(__MINGW32__)
name = "mingw";
version = __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__; // __VERSION__
#elif defined(COMPILER_MSC)
name = "msc";
version = _MSC_FULL_VER;
#elif defined (__BORLANDC__)
name = "borlandc"
version = __BORLANDC__;
#elif defined (__WATCOMC__)
name = "watcomc"
version = __WATCOMC__;
#endif
#elif defined (PLATFORM_POSIX)
#if defined(__GNUC__)
name = "gnuc";
version = __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__;
#endif
#elif defined (__APPLE__)
// In a next future?
#endif
#ifdef _DEBUG
mode = "debug";
#else
mode = "release";
#endif
}

void GetCompilerInfo(String &name, int &version, String &time, String &mode) {
time = String(__DATE__) + " " + __TIME__;
_GetCompilerInfo(name, version, mode);
}

void GetCompilerInfo(String &name, int &version, Time &time, String &mode) {
time = ScanTime("mdy", String(__DATE__) + " " + __TIME__);
_GetCompilerInfo(name, version, mode);
}


#if defined(PLATFORM_WIN32) || defined (PLATFORM_WIN64)

bool PutWindowPlacement(HWND hwnd, RECT rcNormalPosition, POINT ptMinPosition, POINT ptMaxPosition, long showcmd, long flags)
{
WINDOWPLACEMENT place;

place.ptMinPosition = ptMinPosition;
place.ptMaxPosition = ptMaxPosition;
place.rcNormalPosition = rcNormalPosition;
place.showCmd = showcmd;
place.flags = flags;
place.length = sizeof(place);
return SetWindowPlacement(hwnd, &place);
}

bool TakeWindowPlacement(HWND hwnd, RECT &rcNormalPosition, POINT &ptMinPosition, POINT &ptMaxPosition, long &showcmd)
{
WINDOWPLACEMENT place;

place.length = sizeof(place);
bool ret = GetWindowPlacement(hwnd, &place);
ptMinPosition = place.ptMinPosition;
ptMaxPosition = place.ptMaxPosition;
rcNormalPosition = place.rcNormalPosition;
showcmd = place.showCmd; //SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, SW_SHOWNORMAL
//flags = place.flags; // Always 0

return ret;
}

bool Window_GetRect(long windowId, long &left, long &top, long &right, long &bottom)
{
RECT rcNormalPosition;
POINT ptMinPosition, ptMaxPosition;
long showcmd;

TakeWindowPlacement((HWND)windowId, rcNormalPosition, ptMinPosition, ptMaxPosition, showcmd);

left = rcNormalPosition.left;
top = rcNormalPosition.top;
right = rcNormalPosition.right;
bottom = rcNormalPosition.bottom;

return true;
}

bool Window_SetRect(long windowId, long left, long top, long right, long bottom)
{
RECT rcNormalPosition;
POINT ptMinPosition, ptMaxPosition;
long showcmd;

if (!TakeWindowPlacement((HWND)windowId, rcNormalPosition, ptMinPosition, ptMaxPosition, showcmd))
return false;

rcNormalPosition.left = left;
rcNormalPosition.top = top;
rcNormalPosition.right = right;
rcNormalPosition.bottom = bottom;
return PutWindowPlacement((HWND)windowId, rcNormalPosition, ptMinPosition, ptMaxPosition, showcmd, 0);
}

#endif

#ifdef PLATFORM_POSIX

bool Window_GetRect(long windowId, long &left, long &top, long &right, long &bottom)
{
SetSysInfoX11ErrorHandler();
_XDisplay *dpy = XOpenDisplay (NULL);
if (!dpy) {
SetX11ErrorHandler();
return false;
}
bool ret = false;
Window rt;
int x, y, rx, ry;
unsigned int width, height, bw, depth_;
if (XGetGeometry(dpy, windowId, &rt, &x, &y, &width, &height, &bw, &depth_)) {
left = x;
top = y;
right = x + width;
bottom = y + height;
ret = true;
}
XCloseDisplay (dpy);
SetX11ErrorHandler();
return ret;
}

bool Window_SetRect(long windowId, long left, long top, long right, long bottom)
{
SetSysInfoX11ErrorHandler();
_XDisplay *dpy = XOpenDisplay (NULL);
if (!dpy) {
SetX11ErrorHandler();
return false;
}
bool ret = false;

if (XMoveWindow(dpy, windowId, left, top)) {
if (!XResizeWindow(dpy, windowId, right-left, bottom-top))
ret = false;
else
ret = true;
}
XCloseDisplay (dpy);
SetX11ErrorHandler();
return ret;
}

#endif


#ifdef PLATFORM_POSIX

void SetDesktopWallPaper(const char *path)
{
String desktopManager = GetDesktopManagerNew();

if (desktopManager == "gnome") {
Sys("gconftool-2 -t str -s /desktop/gnome/background/picture_filename \"" + String(path) + "\"");
String mode;
if (*path == '\0')
mode = "none"; // Values "none", "wallpaper", "centered", "scaled", "stretched"
else
mode = "stretched";
Sys("gconftool-2 -t str -s /desktop/gnome/background/picture_options \"" + mode + "\"");
} else if (desktopManager == "kde") {
// 1: disabled, only background color
// 2: tiled with first image in top left corner
// 3: tiled with first image centered
// 4: centered stretched with proportions kept until one side hits screen, space filled by background color
// 5: same as 4, though wallpaper aligned to top left and space after stretching filled by tiling
// 6: stretched to fit screen
int mode;
if (*path == '\0')
mode = 1;
else
mode = 6;
Sys("dcop kdesktop KBackgroundIface setWallpaper \"" + String(path) + "\" " + AsString(mode));
} else
throw Exc(t_("Not possible to change Desktop bitmap"));
}

#endif

#if defined(PLATFORM_WIN32) || defined (PLATFORM_WIN64)

void SetDesktopWallPaper(const char *path)
{
if (0 == SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (LPVOID)path, SPIF_UPDATEINIFILE || SPIF_SENDWININICHANGE))
throw Exc(Format(t_("Error %s changing Desktop bitmap"), AsString(GetLastError())));
}
#endif



void SystemSignature::Load() {
GetSystemInfo(manufacturer, productName, version, numberOfProcessors, mbSerial);
hdserial = GetHDSerial();
userName = GetUserName();
netAdapters = GetAdapterInfo();
}

bool SystemSignature::operator==(const SystemSignature &other) const {
if (!(manufacturer == other.manufacturer && productName == other.productName &&
version == other.version && mbSerial == other.mbSerial &&
numberOfProcessors == other.numberOfProcessors && hdserial == other.hdserial &&
userName == other.userName))
return false;
for (int i = 0; i < netAdapters.GetCount(); ++i) {
if (TrimBoth(netAdapters[i].mac).IsEmpty())
continue;
for (int j = 0; j < other.netAdapters.GetCount(); ++j)
if(netAdapters[i].mac == other.netAdapters[j].mac)
return true;
}
return false;
}

void SystemSignature::Copy(const SystemSignature& src) {
manufacturer <<= src.manufacturer;
productName <<= src.productName;
version <<= src.version;
mbSerial <<= src.mbSerial;
numberOfProcessors = src.numberOfProcessors;
hdserial <<= src.hdserial;
userName <<= src.userName;
netAdapters.SetCount(src.netAdapters.GetCount());
for (int i = 0; i < src.netAdapters.GetCount(); ++i)
netAdapters[i] <<= src.netAdapters[i];
}

void SystemSignature::Xmlize(XmlIO xml) {
xml
("manufacturer", manufacturer)("productName", productName)("version", version)
("numberOfProcessors", numberOfProcessors)("mbSerial", mbSerial)("hdserial", hdserial)
("userName", userName)("netAdapters", netAdapters)
;
}

void SystemSignature::Jsonize(JsonIO& json) {
json
("manufacturer", manufacturer)("productName", productName)("version", version)
("numberOfProcessors", numberOfProcessors)("mbSerial", mbSerial)("hdserial", hdserial)
("userName", userName)("netAdapters", netAdapters)
;
}

void SystemSignature::Serialize(Stream& stream) {
stream % manufacturer % productName % version % numberOfProcessors % mbSerial % hdserial
% userName % netAdapters;
}

void SystemOverview::Load() {
signature.Load();
GetBiosInfo(biosVersion, biosReleaseDate, biosSerial);
computerName = GetComputerName();
GetOsInfo(kernel, kerVersion, kerArchitecture, distro, distVersion, desktop, deskVersion);
GetCompilerInfo(compilerName, compilerVersion, compilerTime, compilerMode);
}

void SystemOverview::Copy(const SystemOverview& src) {
signature <<= src.signature;
biosVersion <<= src.biosVersion;
biosSerial <<= src.biosSerial;
biosReleaseDate = src.biosReleaseDate;
computerName <<= src.computerName;
kernel <<= src.kernel;
kerVersion <<= src.kerVersion;
kerArchitecture <<= src.kerArchitecture;
distro <<= src.distro;
distVersion <<= src.distVersion;
desktop <<= src.desktop;
deskVersion <<= src.deskVersion;
compilerName <<= src.compilerName;
compilerVersion = src.compilerVersion;
compilerTime = src.compilerTime;
compilerMode <<= src.compilerMode;
}

void SystemOverview::Xmlize(XmlIO xml) {
xml
("signature", signature)("biosVersion", biosVersion)("biosSerial", biosSerial)
("biosReleaseDate", biosReleaseDate)("computerName", computerName)("kernel", kernel)
("kerVersion", kerVersion)("kerArchitecture", kerArchitecture)("distro", distro)
("distVersion", distVersion)("desktop", desktop)("deskVersion", deskVersion)
("compilerName", compilerName)("compilerVersion", compilerVersion)
("compilerTime", compilerTime)("compilerMode", compilerMode)
;
}

void SystemOverview::Jsonize(JsonIO& json) {
json
("signature", signature)("biosVersion", biosVersion)("biosSerial", biosSerial)
("biosReleaseDate", biosReleaseDate)("computerName", computerName)("kernel", kernel)
("kerVersion", kerVersion)("kerArchitecture", kerArchitecture)("distro", distro)
("distVersion", distVersion)("desktop", desktop)("deskVersion", deskVersion)
("compilerName", compilerName)("compilerVersion", compilerVersion)
("compilerTime", compilerTime)("compilerMode", compilerMode)
;
}

void SystemOverview::Serialize(Stream& stream) {
stream % signature % biosVersion % biosSerial % biosReleaseDate % computerName % kernel
% kerVersion % kerArchitecture % distro % distVersion % desktop % deskVersion
% compilerName % compilerVersion % compilerTime % compilerMode;
}

Change log

r4940 by koldo on May 12, 2012   Diff
SysInfo: New file structure and changes in
GetCPUInfo
Go to: 
Project members, sign in to write a code review

Older revisions

r4730 by koldo on Mar 30, 2012   Diff
*SysInfo: GetProcessName fix thanks to
Borbek
r4430 by koldo on Jan 17, 2012   Diff
.SysInfo: Added Xtest advice
r4270 by koldo on Dec 7, 2011   Diff
SysInfo: Added some changes for 64
bits
All revisions of this file

File info

Size: 50216 bytes, 1600 lines
Powered by Google Project Hosting