[r120]: interpreter-3.x / trunk / rexutils / windows / rxwinsys.c  Maximize  Restore  History

Download this file

2889 lines (2540 with data), 103.0 kB

   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
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
/* Copyright (c) 2005-2006 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.oorexx.org/license.html */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
#include <windows.h>
#define INCL_REXXSAA YES
#include "rexx.h"
#include "APIUtil.h"
#include <stdio.h>
#include <string.h>
#include <ddeml.h>
#include <time.h>
#include <shlobj.h>
#define STR_BUFFER 256
#define MAX_TIME_DATE 128
#define MAX_VARNAME 256
#define WINSYSDLL "RXWINSYS.DLL"
#define MSG_TIMEOUT 5000 // 5000ms
#if (WINVER >= 0x0500)
#define MSG_TIMEOUT_OPTS (SMTO_ABORTIFHUNG|SMTO_NORMAL|SMTO_NOTIMEOUTIFNOTHUNG)
#else
#define MSG_TIMEOUT_OPTS (SMTO_ABORTIFHUNG|SMTO_NORMAL)
#endif
VOID Little2BigEndian(BYTE *pbInt, INT iSize);
LONG HandleArgError(PRXSTRING r, BOOL ToMuch)
{
// no messagebox
// CHAR * text;
// HWND hW = NULL;
// if (ToMuch) text = "Too many arguments!";
// else text = "Not enough arguments!";
// MessageBox(hW,text,"Error",MB_OK | MB_ICONHAND);
r->strlength = 2;
r->strptr[0] = '4';
r->strptr[1] = '0';
r->strptr[2] = '\0';
return 40;
}
#define CHECKARG(argexpctl, argexpcth) \
if ((argc < argexpctl) || (argc > argexpcth)) return HandleArgError(retstr, (argc > argexpcth))
/* macros for a easier return code */
#define RETC(retcode) { \
retstr->strlength = 1;\
if (retcode) retstr->strptr[0] = '1'; else retstr->strptr[0] = '0'; \
retstr->strptr[1] = '\0'; \
return 0; \
}
#define RETERR { \
retstr->strlength = 1;\
retstr->strptr[0] = '1'; \
retstr->strptr[1] = '\0'; \
return 40; \
}
#define RETVAL(retvalue) { \
itoa(retvalue, retstr->strptr, 10); \
retstr->strlength = strlen(retstr->strptr);\
return 0; \
}
#define ISHEX(value) \
((value[0] == '0') && (toupper(value[1]) == 'X'))
#define GET_HKEY(argum, ghk) { \
if (strstr(argum,"MACHINE")) ghk = HKEY_LOCAL_MACHINE; else \
if (strstr(argum,"CLASSES")) ghk = HKEY_CLASSES_ROOT; else \
if (strstr(argum,"CURRENT_USER")) ghk = HKEY_CURRENT_USER; else \
if (strstr(argum,"USERS")) ghk = HKEY_USERS; else \
if (strstr(argum,"PERFORMANCE")) ghk = HKEY_PERFORMANCE_DATA; else \
if (strstr(argum,"CURRENT_CONFIG")) ghk = HKEY_CURRENT_CONFIG; else \
if (strstr(argum,"DYN_DATA")) ghk = HKEY_DYN_DATA; else \
if (ISHEX(argum)) ghk = (HKEY) strtoul(argum,'\0',16); else \
ghk = (HKEY) atol(argum); }
#define SET_VARIABLE(varname, data, retc) {\
shvb.shvnext = NULL; \
shvb.shvname.strptr = varname; \
shvb.shvname.strlength = strlen(varname); \
shvb.shvnamelen = shvb.shvname.strlength; \
shvb.shvvalue.strptr = data; \
shvb.shvvalue.strlength = strlen(data); \
shvb.shvvaluelen = strlen(data); \
shvb.shvcode = RXSHV_SYSET; \
shvb.shvret = 0; \
if (RexxVariablePool(&shvb) == RXSHV_BADN) RETC(retc); \
}
#define GET_ACCESS(argu, acc) \
{ \
if (!stricmp(argu,"READ")) acc = 'R'; else \
if (!stricmp(argu,"OPEN")) acc = 'O'; else \
if (!stricmp(argu,"CLOSE")) acc = 'C'; else \
if (!stricmp(argu,"NUM")) acc = 'N'; else \
if (!stricmp(argu,"CLEAR")) acc = 'L'; else \
if (!stricmp(argu,"WRITE")) acc = 'W'; else \
RETERR; \
}
#define GET_TYPE_INDEX(type, index) \
{ \
switch (type) \
{ \
case EVENTLOG_ERROR_TYPE: \
index=0; \
break; \
case EVENTLOG_WARNING_TYPE: \
index=1; \
break; \
case EVENTLOG_INFORMATION_TYPE: \
index=2; \
break; \
case EVENTLOG_AUDIT_SUCCESS: \
index=3; \
break; \
case EVENTLOG_AUDIT_FAILURE: \
index=4; \
break; \
default: \
index=4; \
} \
}
BOOL IsRunningNT()
{
OSVERSIONINFO version_info={0};
version_info.dwOSVersionInfoSize = sizeof(version_info);
GetVersionEx(&version_info);
if (version_info.dwPlatformId == VER_PLATFORM_WIN32_NT) return TRUE; // Windows NT
else return FALSE; // Windows 95
}
void firstarg(CHAR tar[STR_BUFFER], RXSTRING src)
{
register UINT i;
for (i=0; ((i<src.strlength) && (i<STR_BUFFER-1));i++) tar[i] = toupper(src.strptr[i]);
tar[i] = '\0';
}
ULONG APIENTRY WSRegistryKey(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
char farg[STR_BUFFER];
HKEY hk;
LONG rc;
CHECKARG(2,5);
firstarg(farg, argv[0]);
if (strstr(farg,"CREATE"))
{
HKEY hkResult;
if (RegCreateKey((HKEY)atoi(argv[1].strptr), // handle of an open key
argv[2].strptr, // address of name of subkey to open
&hkResult // address of buffer for opened handle
) == ERROR_SUCCESS) RETVAL((INT)hkResult) else RETC(0);
} else
if (strstr(farg,"OPEN"))
{
HKEY hkResult;
DWORD access=0;
GET_HKEY(argv[1].strptr, hk);
if (argc == 2) RETVAL((INT)hk); /* return predefined handle */
if ((argc < 4) || strstr(argv[3].strptr,"ALL")) access = KEY_ALL_ACCESS;
else {
if (strstr(argv[3].strptr,"WRITE")) access |= KEY_WRITE;
if (strstr(argv[3].strptr,"READ")) access |= KEY_READ;
if (strstr(argv[3].strptr,"QUERY")) access |= KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE;
if (strstr(argv[3].strptr,"EXECUTE")) access |= KEY_EXECUTE;
if (strstr(argv[3].strptr,"NOTIFY")) access |= KEY_NOTIFY;
if (strstr(argv[3].strptr,"LINK")) access |= KEY_CREATE_LINK;
}
if (RegOpenKeyEx(hk, // handle of open key
argv[2].strptr, // address of name of subkey to open
0,
access,
&hkResult // address of handle of open key
) == ERROR_SUCCESS) RETVAL((INT)hkResult) else RETC(0)
} else
if (strstr(farg,"CLOSE"))
{
hk = (HKEY) atoi(argv[1].strptr);
if (RegCloseKey(hk) == ERROR_SUCCESS) RETC(0) else RETC(1)
} else
if (strstr(farg,"DELETE"))
{
GET_HKEY(argv[1].strptr, hk);
if ((rc = RegDeleteKey(hk, argv[2].strptr)) == ERROR_SUCCESS) RETC(0) else RETVAL(rc)
} else
if (strstr(farg,"QUERY"))
{
char Class[256];
DWORD retcode, cbClass, cSubKeys, cbMaxSubKeyLen,
cbMaxClassLen, cValues, cbMaxValueNameLen, cbMaxValueLen, cbSecurityDescriptor;
FILETIME ftLastWriteTime;
SYSTEMTIME stTime;
cbClass = 256;
if ((retcode=RegQueryInfoKey ((HKEY) atoi(argv[1].strptr), // handle of key to query
Class, // address of buffer for class string
&cbClass, // address of size of class string buffer
NULL, // reserved
&cSubKeys, // address of buffer for number of subkeys
&cbMaxSubKeyLen, // address of buffer for longest subkey name length
&cbMaxClassLen, // address of buffer for longest class string length
&cValues, // address of buffer for number of value entries
&cbMaxValueNameLen, // address of buffer for longest value name length
&cbMaxValueLen, // address of buffer for longest value data length
&cbSecurityDescriptor, // address of buffer for security descriptor length
&ftLastWriteTime // address of buffer for last write time
)) == ERROR_SUCCESS)
{
if (FileTimeToSystemTime(&ftLastWriteTime, // pointer to file time to convert
&stTime)) // pointer to structure to receive system time
sprintf(retstr->strptr,"%s, %ld, %ld, %04d/%02d/%02d, %02d:%02d:%02d",
Class, cSubKeys, cValues, stTime.wYear, stTime.wMonth, stTime.wDay,
stTime.wHour, stTime.wMinute, stTime.wSecond);
else
sprintf(retstr->strptr,"%s, %ld, %ld",Class, cSubKeys, cValues);
retstr->strlength = strlen(retstr->strptr);
return 0;
} else RETC(0);
} else
if (strstr(farg,"LIST"))
{
DWORD retcode, ndx=0;
char Name[256];
char sname[64];
SHVBLOCK shvb;
GET_HKEY(argv[1].strptr, hk);
if (argv[2].strptr[argv[2].strlength-1] == '.') argv[2].strptr[argv[2].strlength-1] = '\0'; /* no trailing point */
do {
retcode = RegEnumKey(hk, // handle of key to query
ndx++, // index of subkey to query
Name, // address of buffer for subkey name
sizeof(Name)); // size of subkey buffer
if (retcode == ERROR_SUCCESS)
{
sprintf(sname,"%s.%d",argv[2].strptr,ndx);
SET_VARIABLE(sname, Name, 2);
} else if (retcode != ERROR_NO_MORE_ITEMS) RETC(1)
} while (retcode == ERROR_SUCCESS);
RETC(0)
} else
if (strstr(farg,"FLUSH"))
{
GET_HKEY(argv[1].strptr, hk);
if (RegFlushKey(hk) == ERROR_SUCCESS) RETC(0) else RETC(1)
} else
{
// MessageBox(0,"Illegal registry key command!","Error",MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
RETC(1)
}
}
ULONG APIENTRY WSRegistryValue(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
char farg[STR_BUFFER];
HKEY hk;
LONG rc;
CHECKARG(2,5);
firstarg(farg, argv[0]);
if (strstr(farg,"SET"))
{
DWORD valType;
DWORD dwNumber;
DWORD dataLen;
CONST BYTE * data;
GET_HKEY(argv[1].strptr, hk);
if (!strcmp(argv[4].strptr,"EXPAND")) valType = REG_EXPAND_SZ; else
if (!strcmp(argv[4].strptr,"MULTI")) valType = REG_MULTI_SZ; else
if (!strcmp(argv[4].strptr,"NUMBER")) valType = REG_DWORD; else
if (!strcmp(argv[4].strptr,"BINARY")) valType = REG_BINARY; else
if (!strcmp(argv[4].strptr,"LINK")) valType = REG_LINK; else
if (!strcmp(argv[4].strptr,"RESOURCELIST")) valType = REG_RESOURCE_LIST; else
if (!strcmp(argv[4].strptr,"RESOURCEDESC")) valType = REG_FULL_RESOURCE_DESCRIPTOR; else
if (!strcmp(argv[4].strptr,"RESOURCEREQS")) valType = REG_RESOURCE_REQUIREMENTS_LIST; else
if (!strcmp(argv[4].strptr,"BIGENDIAN")) valType = REG_DWORD_BIG_ENDIAN; else
if (!strcmp(argv[4].strptr,"NONE")) valType = REG_NONE; else
valType = REG_SZ;
if ((valType == REG_DWORD) || (valType == REG_DWORD_BIG_ENDIAN))
{
dwNumber = atoi(argv[3].strptr);
if (valType == REG_DWORD_BIG_ENDIAN)
Little2BigEndian((BYTE *) &dwNumber, sizeof(dwNumber));
data = (CONST BYTE *) &dwNumber;
dataLen = sizeof(dwNumber);
}
else
{
data = (CONST BYTE *) argv[3].strptr;
switch (valType)
{
case REG_BINARY:
case REG_NONE:
case REG_LINK:
case REG_RESOURCE_LIST:
case REG_FULL_RESOURCE_DESCRIPTOR:
case REG_RESOURCE_REQUIREMENTS_LIST:
dataLen = argv[3].strlength;
break;
case REG_EXPAND_SZ:
case REG_MULTI_SZ:
case REG_SZ:
dataLen = argv[3].strlength+1;
break;
}
}
if (RegSetValueEx(hk, // handle of key to set value for
argv[2].strptr, // address of value name to set
0, // reserved
valType, // flag for value type
data, // address of value data
dataLen) == ERROR_SUCCESS) RETC(0) else RETC(1)
} else
if (strstr(farg,"QUERY"))
{
DWORD valType, cbData;
char * valData, *vType;
ULONG intsize;
cbData = sizeof(valData);
GET_HKEY(argv[1].strptr, hk);
if (RegQueryValueEx(hk, // handle of key to query
argv[2].strptr, // address of name of value to query
NULL, // reserved
&valType, // address of buffer for value type
NULL, // NULL to get the size
&cbData) == ERROR_SUCCESS) { // address of data buffer size
valData = GlobalAlloc(GPTR, cbData);
if (!valData) RETERR
if (RegQueryValueEx(hk, // handle of key to query
argv[2].strptr, // address of name of value to query
NULL, // reserved
&valType, // address of buffer for value type
valData, // address of data buffer
&cbData) == ERROR_SUCCESS) { // address of data buffer size
if ((GlobalFlags(retstr->strptr) != GMEM_INVALID_HANDLE) && !GetLastError())
{
intsize = GlobalSize(retstr->strptr);
}
else intsize = STR_BUFFER;
if (intsize > retstr->strlength+1) intsize = retstr->strlength+1;
if (cbData+10 > intsize)
{
if ((GlobalFlags(retstr->strptr) != GMEM_INVALID_HANDLE) && !GetLastError()) GlobalFree(retstr->strptr);
retstr->strptr = GlobalAlloc(GMEM_FIXED, cbData + 10);
}
switch (valType) {
case REG_MULTI_SZ:
vType = "MULTI";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_DWORD:
vType = "NUMBER";
sprintf(retstr->strptr,"%s, %ld",vType, *(DWORD *)valData);
break;
case REG_BINARY:
vType = "BINARY";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_NONE:
vType = "NONE";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_SZ:
vType = "NORMAL";
sprintf(retstr->strptr,"%s, %s",vType, valData);
break;
case REG_EXPAND_SZ:
vType = "EXPAND";
sprintf(retstr->strptr,"%s, %s",vType, valData);
break;
case REG_RESOURCE_LIST:
vType = "RESOURCELIST";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_FULL_RESOURCE_DESCRIPTOR:
vType = "RESOURCEDESC";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_RESOURCE_REQUIREMENTS_LIST:
vType = "RESOURCEREQS";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_LINK:
vType = "LINK";
sprintf(retstr->strptr,"%s, ",vType);
memcpy(&retstr->strptr[strlen(retstr->strptr)], valData, cbData);
break;
case REG_DWORD_BIG_ENDIAN:
{
DWORD dwNumber;
vType = "BIGENDIAN";
dwNumber = * (DWORD *)valData;
Little2BigEndian((BYTE *) &dwNumber, sizeof(dwNumber));
sprintf(retstr->strptr,"%s, %ld",vType, dwNumber);
}
break;
default:
vType = "OTHER";
sprintf(retstr->strptr,"%s,",vType);
}
if ((valType == REG_MULTI_SZ) ||
(valType == REG_BINARY) ||
(valType == REG_RESOURCE_LIST) ||
(valType == REG_FULL_RESOURCE_DESCRIPTOR) ||
(valType == REG_RESOURCE_REQUIREMENTS_LIST) ||
(valType == REG_NONE))
retstr->strlength = strlen(vType) + 2 + cbData;
else
retstr->strlength = strlen(retstr->strptr);
GlobalFree(valData);
return 0;
} else RETC(0)
GlobalFree(valData);
} else RETC(0)
} else
if (strstr(farg,"LIST"))
{
DWORD retcode, ndx=0, valType, cbValue, cbData, initData = 1024;
char * valData, Name[256];
char sname[300];
SHVBLOCK shvb;
GET_HKEY(argv[1].strptr, hk);
if (argv[2].strptr[argv[2].strlength-1] == '.') argv[2].strptr[argv[2].strlength-1] = '\0'; /* no trailing point */
valData = GlobalAlloc(GPTR, initData);
if (!valData) RETERR
do {
cbData = initData;
cbValue = sizeof(Name);
retcode = RegEnumValue(hk, // handle of key to query
ndx++, // index of subkey to query
Name, // address of buffer for subkey name
&cbValue,
NULL, // reserved
&valType, // address of buffer for type code
valData, // address of buffer for value data
&cbData); // address for size of data buffer
if ((retcode == ERROR_MORE_DATA) && (cbData > initData)) /* we need more memory */
{
GlobalFree(valData);
initData = cbData;
valData = GlobalAlloc(GPTR, cbData);
if (!valData) RETERR
ndx--; /* try to get the previous one again */
cbValue = sizeof(Name);
retcode = RegEnumValue(hk, // handle of key to query
ndx++, // index of subkey to query
Name, // address of buffer for subkey name
&cbValue,
NULL, // reserved
&valType, // address of buffer for type code
valData, // address of buffer for value data
&cbData); // address for size of data buffer
}
if (retcode == ERROR_SUCCESS)
{
sprintf(sname,"%s.%d.Name",argv[2].strptr,ndx);
SET_VARIABLE(sname, Name, 2);
sprintf(sname,"%s.%d.Type",argv[2].strptr,ndx);
switch (valType) {
case REG_EXPAND_SZ:
SET_VARIABLE(sname, "EXPAND", 2);
break;
case REG_NONE:
SET_VARIABLE(sname, "NONE", 2);
break;
case REG_DWORD:
SET_VARIABLE(sname, "NUMBER", 2);
break;
case REG_MULTI_SZ:
SET_VARIABLE(sname, "MULTI", 2);
break;
case REG_BINARY:
SET_VARIABLE(sname, "BINARY", 2);
break;
case REG_SZ:
SET_VARIABLE(sname, "NORMAL", 2);
break;
case REG_RESOURCE_LIST:
SET_VARIABLE(sname, "RESOURCELIST", 2);
break;
case REG_FULL_RESOURCE_DESCRIPTOR:
SET_VARIABLE(sname, "RESOURCEDESC", 2);
break;
case REG_RESOURCE_REQUIREMENTS_LIST:
SET_VARIABLE(sname, "RESOURCEREQS", 2);
break;
case REG_LINK:
SET_VARIABLE(sname, "LINK", 2);
break;
case REG_DWORD_BIG_ENDIAN:
SET_VARIABLE(sname, "BIGENDIAN", 2);
break;
default:
SET_VARIABLE(sname, "OTHER", 2);
}
sprintf(sname,"%s.%d.Data",argv[2].strptr,ndx);
if ((valType == REG_MULTI_SZ) ||
(valType == REG_BINARY) ||
(valType == REG_LINK) ||
(valType == REG_RESOURCE_LIST) ||
(valType == REG_FULL_RESOURCE_DESCRIPTOR) ||
(valType == REG_RESOURCE_REQUIREMENTS_LIST) ||
(valType == REG_NONE))
{
shvb.shvnext = NULL;
shvb.shvname.strptr = sname;
shvb.shvname.strlength = strlen(sname);
shvb.shvnamelen = shvb.shvname.strlength;
shvb.shvvalue.strptr = valData;
shvb.shvvalue.strlength = cbData;
shvb.shvvaluelen = cbData;
shvb.shvcode = RXSHV_SYSET;
shvb.shvret = 0;
if (RexxVariablePool(&shvb) == RXSHV_BADN)
{
GlobalFree(valData);
RETC(2)
}
}
else
if ((valType == REG_EXPAND_SZ) ||
(valType == REG_SZ))
SET_VARIABLE(sname, valData, 2)
else
if ((valType == REG_DWORD) || (valType == REG_DWORD_BIG_ENDIAN))
{
char tmp[30];
DWORD dwNumber;
dwNumber = *(DWORD *) valData;
if (valType == REG_DWORD_BIG_ENDIAN)
Little2BigEndian((BYTE *)&dwNumber, sizeof(dwNumber));
ltoa(dwNumber, tmp, 10);
SET_VARIABLE(sname, tmp, 2)
}
else
SET_VARIABLE(sname, "", 2);
}
else if (retcode != ERROR_NO_MORE_ITEMS)
{
GlobalFree(valData);
RETC(1)
}
} while (retcode == ERROR_SUCCESS);
GlobalFree(valData);
RETC(0)
} else
if (strstr(farg,"DELETE"))
{
GET_HKEY(argv[1].strptr, hk);
if ((rc = RegDeleteValue(hk, argv[2].strptr)) == ERROR_SUCCESS) RETC(0) else RETVAL(rc)
} else
{
// MessageBox(0,"Illegal registry value command!","Error",MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
RETC(1)
}
}
ULONG APIENTRY WSRegistryFile(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
char farg[STR_BUFFER];
DWORD retc, rc;
HKEY hk;
HANDLE hToken; /* handle to process token */
TOKEN_PRIVILEGES tkp; /* ptr. to token structure */
CHECKARG(2,5);
firstarg(farg, argv[0]);
if (strstr(farg,"CONNECT"))
{
HKEY hkResult;
GET_HKEY(argv[1].strptr, hk);
if (RegConnectRegistry(argv[2].strptr, // address of name of remote computer
hk, // handle of open key
&hkResult // address of handle of open key
) == ERROR_SUCCESS) RETVAL((INT)hkResult) else RETC(0)
} else
if (strstr(farg,"SAVE"))
{
if (IsRunningNT())
{
/* set SE_BACKUP_NAME privilege. */
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
RETVAL(GetLastError())
LookupPrivilegeValue(NULL, SE_BACKUP_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/* Set SE_BACKUP_NAME privilege for this process. */
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
if ((rc = GetLastError()) != ERROR_SUCCESS)
RETVAL(rc)
}
GET_HKEY(argv[1].strptr, hk);
if ((retc = RegSaveKey(hk, // handle of open key
argv[2].strptr, // file name
NULL)) == ERROR_SUCCESS) RETC(0) else RETVAL(retc)
} else
if (strstr(farg,"LOAD") || strstr(farg,"RESTORE") || strstr(farg,"REPLACE") || strstr(farg,"UNLOAD"))
{
if (IsRunningNT())
{
/* set SE_RESTORE_NAME privilege. */
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
RETVAL(GetLastError())
LookupPrivilegeValue(NULL, SE_RESTORE_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/* Set SE_BACKUP_NAME privilege for this process. */
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
if ((rc = GetLastError()) != ERROR_SUCCESS)
RETVAL(rc)
}
if (strstr(farg,"UNLOAD"))
{
GET_HKEY(argv[1].strptr, hk);
if ((retc = RegUnLoadKey(hk, // handle of open key
argv[2].strptr)) == ERROR_SUCCESS) RETC(0) else RETVAL(retc) // address of name of subkey
} else
if (strstr(farg,"LOAD"))
{
GET_HKEY(argv[1].strptr, hk);
if ((retc = RegLoadKey(hk, // handle of open key
argv[2].strptr, // address of name of subkey
argv[3].strptr)) == ERROR_SUCCESS) RETC(0) else RETVAL(retc) // address of filename for registry information
} else
if (strstr(farg,"RESTORE"))
{
DWORD vola;
GET_HKEY(argv[1].strptr, hk);
if (!strcmp(argv[3].strptr, "VOLATILE")) vola = REG_WHOLE_HIVE_VOLATILE; else vola = 0;
if ((retc = RegRestoreKey(hk, // handle of open key
argv[2].strptr, // file name
vola)) == ERROR_SUCCESS) RETC(0) else RETVAL(retc) // address of filename for registry information
} else
if (strstr(farg,"REPLACE"))
{
char * p;
GET_HKEY(argv[1].strptr, hk);
if (!strcmp(argv[2].strptr, "%NULL%")) p = NULL; else p = argv[2].strptr;
if ((retc = RegReplaceKey(hk, // handle of open key
p, // address of name of subkey
argv[3].strptr, // address of filename for file with new data
argv[4].strptr)) == ERROR_SUCCESS) RETC(0) else RETVAL(retc) // address of filename for backup file
}
RETC(1)
}
else
{
// MessageBox(0,"Illegal registry file command!","Error",MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
RETC(1)
}
}
HDDEDATA CALLBACK DDECallback(UINT wType,
UINT wFmt,
HCONV hConv,
HSZ hsz1,
HSZ hsz2,
HDDEDATA hDDEData,
DWORD dwData1,
DWORD dwData2)
{
return NULL;
}
BOOL ProgmanCmd(LPSTR lpszCmd)
{
DWORD dwDDEInst = 0;
UINT ui;
HSZ hszProgman;
HCONV hConv;
HDDEDATA hExecData;
HDDEDATA exRes;
ui = DdeInitialize(&dwDDEInst,
DDECallback,
CBF_FAIL_ALLSVRXACTIONS,
0l);
if (ui != DMLERR_NO_ERROR) return FALSE;
hszProgman = DdeCreateStringHandle(dwDDEInst,
"PROGMAN",
CP_WINANSI);
hConv = DdeConnect(dwDDEInst,
hszProgman,
hszProgman,
NULL);
DdeFreeStringHandle(dwDDEInst, hszProgman);
if (!hConv) return FALSE;
hExecData = DdeCreateDataHandle(dwDDEInst,
lpszCmd,
lstrlen(lpszCmd)+1,
0,
NULL,
0,
0);
exRes = DdeClientTransaction((void FAR *)hExecData,
(DWORD)-1,
hConv,
NULL,
0,
XTYP_EXECUTE,
2000, // ms timeout
NULL);
DdeDisconnect(hConv);
DdeUninitialize(dwDDEInst);
if (!exRes) return FALSE;
return TRUE;
}
BOOL AddPMGroup(LPSTR lpszGroup, LPSTR lpszPath)
{
char buf[1024];
if (lpszPath && lstrlen(lpszPath))
{
wsprintf(buf,
"[CreateGroup(%s,%s)]",
lpszGroup,
lpszPath);
}
else
{
wsprintf(buf,
"[CreateGroup(%s)]",
lpszGroup);
}
return ProgmanCmd(buf);
}
BOOL DeletePMGroup(LPSTR lpszGroup)
{
char buf[512];
if (lpszGroup && lstrlen(lpszGroup))
{
wsprintf(buf,
"[DeleteGroup(%s)]",
lpszGroup);
}
return ProgmanCmd(buf);
}
BOOL ShowPMGroup(LPSTR lpszGroup, WORD wCmd)
{
char buf[512];
if (lpszGroup && lstrlen(lpszGroup))
{
wsprintf(buf,
"[ShowGroup(%s,%u)]",
lpszGroup,
wCmd);
}
return ProgmanCmd(buf);
}
BOOL AddPMItem(LPSTR lpszCmdLine,
LPSTR lpszCaption,
LPSTR lpszIconPath,
WORD wIconIndex,
LPSTR lpszDir,
BOOL bLast,
LPSTR lpszHotKey,
LPSTR lpszModifier,
BOOL bMin)
{
char buf[2048];
int Pos;
if (bLast) Pos = -1; else Pos = 0;
if (lpszIconPath && lstrlen(lpszIconPath))
{
wsprintf(buf,
"[AddItem(%s,%s,%s,%u,%d,%d,%s,%d,%d)]",
lpszCmdLine,
lpszCaption,
lpszIconPath,
wIconIndex,
Pos,Pos,
lpszDir,
MAKEWORD( atoi(lpszHotKey), atoi(lpszModifier)),
bMin);
}
else
{
wsprintf(buf,
"[AddItem(%s,%s,"","",%d,%d,%s,%d,%d)]",
lpszCmdLine,
lpszCaption,
Pos, Pos,
lpszDir,
MAKEWORD( atoi(lpszHotKey), atoi(lpszModifier)),
bMin);
}
return ProgmanCmd(buf);
}
BOOL DeletePMItem(LPSTR lpszItem)
{
char buf[512];
if (lpszItem && lstrlen(lpszItem))
{
wsprintf(buf,
"[DeleteItem(%s)]",
lpszItem);
}
return ProgmanCmd(buf);
}
BOOL LeavePM(BOOL bSaveGroups)
{
char buf[256];
wsprintf(buf,
"[ExitProgman(%u)]",
bSaveGroups ? 1 : 0);
return ProgmanCmd(buf);
}
//-----------------------------------------------------------------------------
//
// Function: BOOL GetCurrentUserDesktopLocation
//
// This function reads the CurrentUser Desktop Path from the registry.
//
// Syntax: call GetCurrentUserDesktopLocation( LPBYTE szDesktopDir, LPDWORD lpcbData )
//
// Params:
//
// szDesktopDir - Drive and Path of the Desktop to be returned
// lpcbData _ Max Size of szDesktopDir on entry, Size of szDesktopDir on exit
//
// return : TRUE - No error
// FALSE - Error
//-----------------------------------------------------------------------------
#define IDS_REGISTRY_KEY_CURRENT_SHELLFOLDER "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
#define IDS_CURRENT_DESKTOP "Desktop"
BOOL GetCurrentUserDesktopLocation ( LPBYTE szDesktopDir, LPDWORD lpcbData )
{
HKEY hKey; //handle of key
long rc;
DWORD Type ;
szDesktopDir[0] ='\0'; //initialize return
if ( (rc = RegOpenKeyEx(HKEY_CURRENT_USER,
IDS_REGISTRY_KEY_CURRENT_SHELLFOLDER,
0,
KEY_QUERY_VALUE,
&hKey)) == ERROR_SUCCESS )
{
if ( (rc = RegQueryValueEx(hKey, // handle of key to query
IDS_CURRENT_DESKTOP , // address of name of value to query
NULL, // reserved
&Type, // address of buffer for value type
szDesktopDir , // address of returned data
lpcbData)) == ERROR_SUCCESS ) // .. returned here
{
RegCloseKey ( hKey ) ;
return TRUE ;
}
RegCloseKey ( hKey ) ;
}
// Error occured
return FALSE ;
}
//-----------------------------------------------------------------------------
//
// Function: BOOL GetAllUserDesktopLocation
//
// This function reads All UsersDesktop Path from the registry.
//
// Syntax: call GetAllUserDesktopLocation( LPBYTE szDesktopDir, LPDWORD lpcbData )
//
// Params:
//
// szDesktopDir - Drive and Path of the Desktop to be returned
// lpcbData _ Max Size of szDesktopDir on entry, Size of szDesktopDir on exit
//
// return : TRUE - No error
// FALSE - Error
//-----------------------------------------------------------------------------
#define IDS_REGISTRY_KEY_ALL_NT_SHELLFOLDER "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
#define IDS_ALL_NT_DESKTOP "Common Desktop"
#define IDS_REGISTRY_KEY_ALL_9x_SHELLFOLDER ".DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
#define IDS_ALL_9x_DESKTOP "Desktop"
BOOL GetAllUserDesktopLocation ( LPBYTE szDesktopDir, LPDWORD lpcbData )
{
HKEY hKey; //handle of key
long rc;
DWORD lpType ;
LPTSTR lpValueName ;
szDesktopDir[0] ='\0'; //initialize return
// Test, if 95/98/Millenium or NT/Win2000
if ( IsRunningNT() )
{
rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
IDS_REGISTRY_KEY_ALL_NT_SHELLFOLDER,
0,
KEY_QUERY_VALUE,
&hKey) ;
lpValueName = IDS_ALL_NT_DESKTOP ;
}
else
{
rc = RegOpenKeyEx(HKEY_USERS,
IDS_REGISTRY_KEY_ALL_9x_SHELLFOLDER,
0,
KEY_QUERY_VALUE,
&hKey) ;
lpValueName = IDS_ALL_9x_DESKTOP ;
}
if ( rc == ERROR_SUCCESS )
{
if ( (rc = RegQueryValueEx(hKey, // handle of key to query
lpValueName , // address of name of value to query
NULL, // reserved
&lpType, // address of buffer for value type
szDesktopDir , // address of returned data
lpcbData)) == ERROR_SUCCESS ) // .. returned here
{
RegCloseKey ( hKey ) ;
return TRUE ;
}
RegCloseKey ( hKey ) ;
}
// Error occured
return FALSE ;
}
//-----------------------------------------------------------------------------
//
// Function: BOOL AddPMDesktopIcon
//
// This function creates a shortcut to a file on the desktop.
//
// Syntax: call AddPmDesktopIcon( lpszName, lpszProgram, lpszIcon, iIconIndex, lpszWorkDir,
// lpszLocation, lpszArguments, iscKey, iscModifier, run )
//
// Params:
//
// lpszName - Name of the short cut, displayed on the desktop below the icon
// lpszProgram - full name of the file of the shortcut referes to
// lpszIcon - full name of the icon file
// iIconIndex - index of the icon within the icon file
// lpszWorkDir - working directory of the shortcut
// lpszLocation - "PERSONAL" : icon is only on desktop for current user
// - "COMMON" : icon is displayed on desktop of all users
// - "" : it's a link and can be placed in any folder
// lpszArguments - arguments passed to the shortcut
// iScKey - shortcut key
// iScModifier - modifier of shortcut key
// run - run application in a : NORMAL
// MINIMIZED
// MAXIMIZED window
//
// return : TRUE - No error
// FALSE - Error
//-----------------------------------------------------------------------------
BOOL AddPMDesktopIcon(LPSTR lpszName,
LPSTR lpszProgram,
LPSTR lpszIcon,
int iIconIndex,
LPSTR lpszWorkDir,
LPSTR lpszLocation,
LPSTR lpszArguments,
int iScKey,
int iScModifier,
LPSTR lpszRun )
{
HRESULT hres;
IShellLink* psl;
BOOL bRc = TRUE;
int iRun = SW_NORMAL;
CoInitialize(NULL);
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target
psl->lpVtbl->SetPath(psl, lpszProgram );
// icon location. default of iIconIndex is 0, set in WINSYSTM.CLS
psl->lpVtbl->SetIconLocation(psl, lpszIcon, iIconIndex);
// command-line arguments
psl->lpVtbl->SetArguments( psl, lpszArguments );
//shortcut key, the conversion to hex is done in WINSYSTM.CLS
// modificationflag:
// The modifier flags can be a combination of the following values:
// HOTKEYF_SHIFT = SHIFT key 0x01
// HOTKEYF_CONTROL = CTRL key 0x02
// HOTKEYF_ALT = ALT key 0x04
// HOTKEYF_EXT = Extended key 0x08
psl->lpVtbl->SetHotkey( psl, MAKEWORD( iScKey, iScModifier) );
// working directory
psl->lpVtbl->SetWorkingDirectory( psl, lpszWorkDir );
// run in normal, maximized , minimized window, default is NORMAL, set in WINSYSTM.CLS
if ( !stricmp(lpszRun,"MAXIMIZED") )
iRun = SW_SHOWMAXIMIZED;
else
if ( !stricmp(lpszRun,"MINIMIZED") )
{
iRun = SW_SHOWMINNOACTIVE;
}
psl->lpVtbl->SetShowCmd( psl, iRun );
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
CHAR szShortCutName[MAX_PATH];
CHAR szDesktopDir[MAX_PATH];
DWORD dwSize = MAX_PATH;
// If strlen(lpszLocation is < 6, then lpszName contains a full qualified filename
if (strlen(lpszLocation)>5)
{
// if icon should only be created on the desktop of current user
// get current user
if (!stricmp(lpszLocation,"PERSONAL"))
{
bRc = GetCurrentUserDesktopLocation ( szDesktopDir , &dwSize ) ;
}
else
{ // Location is COMMON
bRc = GetAllUserDesktopLocation ( szDesktopDir , &dwSize ) ;
}
if ( bRc)
{
//.lnk must be added to identify the file as a shortcut
sprintf( szShortCutName, "%s\\%s.lnk", szDesktopDir, lpszName);
}
}
else /* empty specifier so it's a link */
{
sprintf( szShortCutName, "%s.lnk", lpszName); /* lpszName contains a full qualified filename */
}
// Continueonly, if bRC is TRUE
if ( bRc )
{
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, szShortCutName, -1, wsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
if (!SUCCEEDED(hres))
bRc = FALSE;
ppf->lpVtbl->Release(ppf);
}
else
bRc = FALSE;
}
else
bRc = FALSE;
psl->lpVtbl->Release(psl);
}
else
bRc = FALSE;
return bRc;
}
//-----------------------------------------------------------------------------
//
// Function: INT DelPMDesktopIcon
//
// This function deletes a shortcut created with AddPMDektopIcon
//
// Syntax: call DelPMDesktopIcon( lpszName,lpszLocation )
//
// Params:
//
// lpszName - Name of the short cut to be deleted
//
// return : 0 - No error
// others - Error codes from DeleteFile
// Note: The returncode of GetCurrentXXXDesktopLocation is not checked because
// this hould be handeled by DeleteFile. So if an error occured during this
// function, file nit found is returned
//-----------------------------------------------------------------------------
INT DelPMDesktopIcon( LPSTR lpszName,
LPSTR lpszLocation)
{
CHAR szDesktopDir[MAX_PATH];
CHAR szShortCutName[MAX_PATH];
DWORD dwSize = MAX_PATH;
// get the location (directory) of the shortcut file in dependency of
// the specified location
if (!stricmp(lpszLocation,"PERSONAL"))
{
GetCurrentUserDesktopLocation ( szDesktopDir , &dwSize );
}
else
{ // Location is COMMON
GetAllUserDesktopLocation ( szDesktopDir , &dwSize );
}
//.lnk must be added to identify the file as a shortcut
sprintf( szShortCutName, "%s\\%s.lnk", szDesktopDir, lpszName);
if (!DeleteFile(szShortCutName))
return GetLastError();
else
return 0;
}
ULONG APIENTRY WSProgManager(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
char farg[STR_BUFFER];
CHECKARG(2,11);
firstarg(farg, argv[0]);
if (strstr(farg,"ADDGROUP"))
{
RETC(!AddPMGroup(argv[1].strptr, argv[2].strptr))
} else
if (strstr(farg,"DELGROUP"))
{
RETC(!DeletePMGroup(argv[1].strptr))
} else
if (strstr(farg,"SHOWGROUP"))
{
INT stype;
if (strstr(argv[2].strptr,"MAX")) stype = 3;
else if (strstr(argv[2].strptr,"MIN")) stype = 2;
else stype = 1;
RETC(!ShowPMGroup(argv[1].strptr, (WORD)stype))
} else
if (strstr(farg,"ADDITEM"))
{
if (argc > 7)
RETC(!AddPMItem(argv[1].strptr, argv[2].strptr, argv[3].strptr, (WORD)atoi(argv[4].strptr), argv[5].strptr,\
(BOOL)atoi(argv[6].strptr), argv[7].strptr, argv[8].strptr, (BOOL)atoi(argv[9].strptr)))
else
RETC(!AddPMItem(argv[1].strptr, argv[2].strptr, argv[3].strptr, (WORD)atoi(argv[4].strptr), argv[5].strptr,\
(BOOL)atoi(argv[6].strptr), "", "", 0))
} else
if (strstr(farg,"DELITEM"))
{
RETC(!DeletePMItem(argv[1].strptr))
} else
if (strstr(farg,"LEAVE"))
{
if (!strcmp(argv[1].strptr,"SAVE"))
RETC(!LeavePM(TRUE))
else
RETC(!LeavePM(FALSE))
} else
if (strstr(farg,"ADDDESKTOPICON"))
{
RETC(!AddPMDesktopIcon( argv[1].strptr, argv[2].strptr, argv[3].strptr, atoi(argv[4].strptr),
argv[5].strptr, argv[6].strptr, argv[7].strptr, atoi(argv[8].strptr),
atoi(argv[9].strptr), argv[10].strptr ))
} else
if (strstr(farg,"DELDESKTOPICON"))
{
CHECKARG(3,3);
RETVAL(DelPMDesktopIcon( argv[1].strptr, argv[2].strptr))
}
else
{
// MessageBox(0,"Illegal program manager command!","Error",MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
RETC(0)
}
}
#define WSFTS 9
CHAR * WindowsSystemFuncTab[WSFTS] = {\
"WSRegistryKey", \
"WSRegistryValue", \
"WSRegistryFile", \
"WSProgManager", \
"WSEventLog", \
"WSCtrlWindow",
"WSCtrlSend",
"WSCtrlMenu",
"WSClipboard"
};
ULONG APIENTRY RemoveWinSysFuncs(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
INT rc, i;
BOOL err = FALSE;
for (i=0;i<WSFTS;i++)
{
rc = RexxDeregisterFunction(WindowsSystemFuncTab[i]);
if (rc) err = TRUE;
}
rc = RexxDeregisterFunction("RemoveWinSysFuncs");
if (rc) err = TRUE;
rc = RexxDeregisterFunction("InstWinSysFuncs");
if (rc) err = TRUE;
if (!err)
RETC(0)
else
RETC(1)
}
ULONG APIENTRY InstWinSysFuncs(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
INT rc, i;
BOOL err = FALSE;
retstr->strlength = 1;
rc = RexxRegisterFunctionDll(
"RemoveWinSysFuncs",
WINSYSDLL,
"RemoveWinSysFuncs");
if (rc) err = TRUE;
for (i=0;i<WSFTS;i++)
{
rc = RexxRegisterFunctionDll(WindowsSystemFuncTab[i],WINSYSDLL,WindowsSystemFuncTab[i]);
if (rc) err = TRUE;
}
if (err)
RETC(1)
else
RETC(0)
}
//Functions for reading Event Log
//-----------------------------------------------------------------------------
//
// Function GetEvPaMessageFile
//
// Gets the name of the message file from the registry
//
// char * GetEvPaMessageFile(char * pMessageFile, char * pchSource,char * pchSourceName, char * chMessageFile)
//
// pMessaegFile - (in) What to get
// "EventMessageFile" get event message file name
// "ParameterMessageFile" get parameter message file
//
// pchSource - (in) second part of the key to build
//
// pchSourceName - (in) third part of the key to build
//
// chMessageFile - (in/out) name of the message file
// string buffer with length MAX_PATH
//
//-----------------------------------------------------------------------------
void GetEvPaMessageFile(char * pMessageFile, char * pchSource,char * pchSourceName, char * chMessageFile)
{
DWORD dataSize; //size of the message file name returned from RegQueryValueEx
DWORD valType = REG_EXPAND_SZ; //type for call to RegQueryValueEx
char chKey[] = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\"; //first part of key for message files
char *pchKey; //contains name of complete key
char *valData; //value returned from RegQueryValueEx
HKEY hKey; //handle of key
int rc;
chMessageFile[0] ='\0'; //initialize return
//build the complete key --> first part + second part from input + third part from input
pchKey = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, strlen(chKey) + strlen(pchSource) + 1 + strlen(pchSourceName) +1);
sprintf(pchKey, "%s%s\\%s", chKey, pchSource, pchSourceName);
if ( (rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
pchKey,
0,
KEY_QUERY_VALUE,
&hKey)) == ERROR_SUCCESS )
{
if ( (rc = RegQueryValueEx(hKey, // handle of key to query
pMessageFile, // address of name of value to query
NULL, // reserved
&valType, // address of buffer for value type
NULL, // NULL to get the size ...
&dataSize)) == ERROR_SUCCESS )// .. returned here
{
//no error getting the size of message file name, allocate buffer and get the value
valData = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, dataSize);
if ( (rc = RegQueryValueEx(hKey, // handle of key to query
pMessageFile, // address of name of value to query
NULL, // reserved
&valType, // address of buffer for value type
valData, // address of buffer for value data
&dataSize)) == ERROR_SUCCESS )
{
ExpandEnvironmentStrings(valData,chMessageFile,MAX_PATH);
}
GlobalFree(valData);
}
}
GlobalFree(pchKey);
}
//-----------------------------------------------------------------------------
//
// Function SearchMessage
//
// Load message files.
// Search the message in message files, format and return the message
//
// BOOL SearchMessage(char * chFileNames, DWORD dwMessageID, char ** pchInsertArray, LPVOID *lpMsgBuf )
//
// chFileNames - (in) filenames, separated by semicolons
// dwMessageID - (in) message id to be searched
// pchInsertArray - (in) array with replacement strings for message formatting
// lpMsgBuf - (out) buffer containing the formatted message
//
// return : 1 - message found and returned in lpMsgBuf
// 0 - message not found or other error
//-----------------------------------------------------------------------------
BOOL SearchMessage(char * chFileNames, DWORD dwMessageID, char ** pchInsertArray, LPVOID *lpMsgBuf )
{
HINSTANCE hInstance = 0; //return value
char * pBuffer=0; //buffer where chFileNames is copied to
char * pTemp=0; //pointers for parsing ...
char * pNext=0; //the file names
int rc;
BOOL fRc=0;
if (chFileNames[0] != '\0')
{
pBuffer = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, strlen(chFileNames)+1);
strcpy(pBuffer,chFileNames);
pTemp = pBuffer;
while (pTemp != 0 && fRc != 1)
{
pNext = strchr(pTemp,';');
if (pNext != NULL )
{
*pNext = '\0';
pNext++;
}
if ( (hInstance = LoadLibrary(pTemp)) != 0 )
{
rc = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
hInstance, //message file handle
dwMessageID, //message id
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR)lpMsgBuf, //output for formatted message
0, //not needed for FORMAT_MESSAGE_ALLOCATE_BUFFER
pchInsertArray ); //replacement strings
if ( rc != 0 ) //no error
fRc = 1;
FreeLibrary(hInstance);
hInstance = 0;
}
pTemp = pNext;
}
if (pBuffer)
GlobalFree(pBuffer);
}
return fRc;
}
//-----------------------------------------------------------------------------
//
// Function BuildMessage
//
// Extracts the user name from an event log record
//
// void BuildMessager(PEVENTLOGRECORD pEvLogRecord, char * pchSource, char ** pMessage)
//
// pEvLogRecord - (in)event log record
// pchSource - (in)source within event log record
// pMessage - (out)formatted message
//
//-----------------------------------------------------------------------------
void BuildMessage(PEVENTLOGRECORD pEvLogRecord , char * pchSource, char ** pMessage )
{
char * pchString = (char*)0; //pointer to replacement string within event log record
int iStringLen = 0; //accumulation of the string length
int i, rc;
HINSTANCE hInstance=0; //handle for message files
LPVOID lpMsgBuf=0; //buffer to format the string
char *pchInsertArray[100]; //pointer array to replacement strings
char chMessageFile[MAX_PATH]; //name of message file
char *pchPercent; //pointer to "%%" within a replacement string
//initialize insert array
memset(pchInsertArray,0,sizeof(pchInsertArray));
//point to first replacement string
pchString = (char*)pEvLogRecord + pEvLogRecord->StringOffset;
//loop over all replacement strings, substitute "%%" and build an array of replacement strings
for (i=0; i < pEvLogRecord->NumStrings ; i++)
{
//If a replacement string contains "%%", the name of the parameter message file must be
//read from the registry log. "%%" is followed by an id which is the id of parameter strings
//to be loaded from the parameter message file
if ( (pchPercent = strstr(pchString,"%%")) )
{
//the replacement strings contains placeholder for parameters
//get name of parameter message file from registry
GetEvPaMessageFile("ParameterMessageFile",pchSource,(char *)pEvLogRecord + sizeof(EVENTLOGRECORD),chMessageFile);
//load strings from the parameter message file
if ( (hInstance = LoadLibrary(chMessageFile)) != 0 )
{
//load the parameters to be inserted into the replacement string
rc = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
hInstance, //handle to message file
atoi(pchPercent+2), //id of message to be loaded
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR)&pchInsertArray[i],
0,
0 );
if (rc == 0) //error getting strings
{
//use empty string
pchInsertArray[i] = LocalAlloc(LMEM_FIXED,1);
*pchInsertArray[i] ='\0';
}
FreeLibrary(hInstance);
hInstance =0;
}
else
{
//parameter message file could not be loaded, use empty string
pchInsertArray[i] = LocalAlloc(LMEM_FIXED,1);
*pchInsertArray[i] ='\0';
}
//accumulate sum of string length
iStringLen += strlen(pchInsertArray[i])+1;
}
else
{
//the replacement strings contains NO placeholder for parameters, use them as is
iStringLen += strlen(pchString)+1;
pchInsertArray[i] = LocalAlloc(LMEM_FIXED,strlen(pchString)+1);
strcpy(pchInsertArray[i],pchString);
}
//point to next replacement string within log record
pchString += strlen(pchString)+1;
}
//get name of event log message file from registry
GetEvPaMessageFile("EventMessageFile",pchSource,(char *)pEvLogRecord + sizeof(EVENTLOGRECORD),chMessageFile);
//Search the message in the all event message files
rc = SearchMessage(chMessageFile,pEvLogRecord->EventID,pchInsertArray,&lpMsgBuf);
if (rc == 0) //no message found or any other error
{
//use replacement strings for the message if available
if (pEvLogRecord->NumStrings)
{
*pMessage = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, iStringLen+1);
for (i=0;i < pEvLogRecord->NumStrings;i++)
{
strcat(*pMessage,pchInsertArray[i]);
strcat(*pMessage," ");
}
}
else //return empty string
{
*pMessage = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, 1);
*pMessage[0] = '\0';
}
}
else
{
//message formatted return it
*pMessage = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, strlen((char*)lpMsgBuf) + 1);
strcpy(*pMessage,lpMsgBuf);
//lpMsgBuf must be given free with LocalFree
LocalFree(lpMsgBuf);
}
//free replacement strings
for (i=0; i < pEvLogRecord->NumStrings ; i++)
{
LocalFree(pchInsertArray[i]);
}
}
//-----------------------------------------------------------------------------
//
// Function GetUser
//
// Extracts the user name from an event log record
//
// char * GetUser(PEVENTLOGRECORD pEvLogRecord)
//
// pEvLogRecord - (in)pointer to event log record
//
// return char * - pointer to user name (account) from event log record or
// pointer to "N/A" when no user available or in error case
// (pointer is allocated from heap)
// must be freed vy caller
//
//-----------------------------------------------------------------------------
char * GetUser(PEVENTLOGRECORD pEvLogRecord)
{
SID *psid; //points to security structure withi event log record
char * pchUserId=0; //returned userid, from heap, chDefUserId in any error case
//or when no userid available
ULONG sizeId=0; //0 to get the size of the uder id
char chDefUserId[] = "N/A"; //default userid
int rc;
SID_NAME_USE strDummy; //dummies needed for call to ...
char *pchDummy=0; //LookupAccontSid ...
DWORD sizeDummy=0; //but not subject of interest
if (pEvLogRecord->UserSidLength != 0) //no SID record avaialable return default userid
{
//get the SID record within event log record
psid = (SID *)((char*)pEvLogRecord + pEvLogRecord->UserSidOffset);
//get the size of the pchUserIid and pchDummy
rc = LookupAccountSid(NULL, // address of string for system name
psid, // address of security identifier
pchUserId, // address of string for account name
&sizeId, // address of size account string
pchDummy, // address of string for referenced domain (not needed)
&sizeDummy, // address of size domain string
&strDummy ); // address of structure for SID type
if (rc == 0) //error
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) //expected because sizes are set to 0
{
rc = max(sizeId,strlen(chDefUserId)+1);
pchUserId = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, max(sizeId,strlen(chDefUserId)+1));
pchDummy = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, sizeDummy);
//now get it ...
rc = LookupAccountSid(NULL, // address of string for system name
psid, // address of security identifier
pchUserId, // address of string for account name
&sizeId, // address of size account string
pchDummy, // address of string for referenced domain
&sizeDummy, // address of size domain string
&strDummy ); // address of structure for SID type
GlobalFree(pchDummy); //don't want this
//error: return default
if (rc == 0) strcpy(pchUserId,chDefUserId);
}
}
}
//if not already set return default
if (pchUserId == (char*)0)
{
pchUserId = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, strlen(chDefUserId)+1);
strcpy(pchUserId,chDefUserId);
}
return pchUserId;
}
//-----------------------------------------------------------------------------
//
// Function GetEvData
//
// Extracts the data name from an event log record
//
// char * GetEvData(PEVENTLOGRECORD pEvLogRecord, char ** pchData)
//
// pEvLogRecord - (in)pointer to event log record
//
// pchData - (in/out) data, allocated from heap
// empty string when no data
//
//-----------------------------------------------------------------------------
void GetEvData(PEVENTLOGRECORD pEvLogRecord, char ** pchData)
{
unsigned char * puchAct;
char pTemp[3];
DWORD i;
if (pEvLogRecord->DataLength != 0)
{
*pchData = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,(pEvLogRecord->DataLength + 1) * 2);
puchAct = (char*)pEvLogRecord + pEvLogRecord->DataOffset;
for ( i=0; i<pEvLogRecord->DataLength; i++)
{
sprintf(pTemp, "%02x", *puchAct);
strcat(*pchData, pTemp);
puchAct++;
}
}
else
*pchData = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, 1);
}
//-----------------------------------------------------------------------------
//
// Function: WSEventLog
//
// This function reads the contents of the event log file. Each event log record is
// returned in a stem.# variable.
//
// 0 1 2 3 4 5 6 7 8
// Syntax: call WSEventLog( access, [options], [hEvent], [stem], [server], [source], [start],[num],[backupFileName]
// 9 10 11 12 13
// type, category, id, data, strings
// )
//
// Params:
//
// access - What to do with the event log
// 'OPEN' returns event log handle
// 0 in error case
// 'CLOSE'
// 'READ' returns a stem containing the records
// 'NUM' returns number of record or 0 if no records available or error
// 'CLEAR" clear an backup the event log ........................
//
// options - 'FORWARDS' - The log is read in forward chronological order. (default)
// (oldest first)
// 'BACKWARDS' - The log is read in backward chronological order.
// (youngest first)
//
// hEvent - eventlog handle, returned when eventlog is opened before
//
// stem Name of stem variable to store results in.
// stem.0 contains the number of returned records
//
// server - Universal Naming Convention (UNC) name of the server on which the
// event log is to be opened. If this parameter is not spcified,
// the log is opened on the local computer.
//
// source - Name of the source. The source name must be a subkey of a logfile
// entry under the EventLog key in the registry.
// 'System' - system log
// 'Security' - security log
// 'Application' - application log (default if source is empty or when source not found
//
// start - Record number of event log record to start. The oldest record is always the first record !!!
// (independent if BACKEWRDS or FORWARDS is specified)
//
// num - Number of event log record to read
//
// backupFileName - if specified togetherr with 'CLEAR' the event log is backed up
// before it is cleared
//
//-----------------------------------------------------------------------------
ULONG APIENTRY WSEventLog(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
HANDLE hEventLog=0; //handle of event log
char * pchServer = (char *)0; //server name, must be NULL if local
char * pchSource = (char *)0; //source from input, or default
char * pchBackupFileName = (char *)0;//name of the backup file name for CLEAR
char chDefSource[] = {"Application"}; //default source if no input
char varName[MAX_VARNAME]; //name of stem variable
ULONG stemLen; //length of stem vaiable
char access; //what to do with the log
DWORD dwReadFlags; //read flags
LONG rc = 0; //return code
LONG rcGetLastError = 0; //rc from GetLastError
DWORD bufSize = 1024; //initial size for event log buffer
ULONG start,num; //start Record number and number of of event log record to read
DWORD numEvRecords; //number of all record within a event log
SHVBLOCK shvb; //shared variable block
//check parameter access(required); GET_ACESSS returns when invcalid access
if (argc >= 1 && argv[0].strlength != 0)
{
GET_ACCESS(argv[0].strptr,access )
// in case of write, a variable number of arguments (strings at the end) are possible
if ( access != 'W' )
CHECKARG(1,14); //minimum of one and maximum of 14 parameters allowed
}
else
RETERR
if (access == 'R') //read access
{
dwReadFlags = EVENTLOG_FORWARDS_READ; //default
//check parameter options
if (argv[1].strlength != 0)
{
if (stricmp(argv[1].strptr,"FORWARDS"))
{
if (!stricmp(argv[1].strptr,"BACKWARDS"))
dwReadFlags = EVENTLOG_BACKWARDS_READ;
else
RETERR
}
}
//preset defaults for start record and number of records to read
start = 1;
num = 0xffffffff;
bufSize = 64 * bufSize; //assume that when complete log should be read (64K)
//check parameter start and num
if (argc >= 7 )
{
if (argc >= 8 )
{
//start and num are available
if ( (argv[6].strlength != 0) && (argv[7].strlength != 0) )
{
//start and num contains values
dwReadFlags |= EVENTLOG_SEEK_READ;
start = atoi(argv[6].strptr);
num = atoi(argv[7].strptr);
bufSize = num * bufSize;
}
else
dwReadFlags |= EVENTLOG_SEQUENTIAL_READ;
}
else //only start available, num is then required
RETERR
}
else
dwReadFlags |= EVENTLOG_SEQUENTIAL_READ;
//check parameter hEventLog (initialized with 0 means that event log will be opened)
if ( argc >= 3 )
if (argv[2].strlength != 0) hEventLog = (HANDLE)atol(argv[2].strptr);
//check parameter stem, add dot if necesssary
if (argc >= 4 )
{
if ( RXVALIDSTRING(argv[3]) )
{
strcpy(varName, argv[3].strptr);
memupper(varName, strlen(varName));
stemLen = argv[3].strlength;
if (varName[stemLen-1] != '.')
varName[stemLen++] = '.';
}
else
RETERR
}
}
//check parameter server
//if no server, use local machine, 0 pointer required !
if ( argc >= 5 )
if (argv[4].strlength != 0) pchServer = argv[4].strptr;
//check parameter source, if empty use default source "Application"
if ( (argc >= 6) && (argv[5].strlength != 0) )
pchSource = argv[5].strptr;
else
pchSource = chDefSource;
//open
if (access == 'O')
{
if ( (hEventLog = OpenEventLog(pchServer,pchSource) ) != 0 )
RETVAL((INT)hEventLog)
else RETC(0)
}
//close
if (access == 'C')
{
if (argc >= 3 )
{
if (argv[2].strlength != 0)
hEventLog = (HANDLE)atol(argv[2].strptr);
else
RETERR
if (CloseEventLog(hEventLog))
RETC(0)
else
RETC(1)
}
else
RETERR
}
//NUM or CLEAR (and backup)
if (access == 'N' || access == 'L')
{
//check parameter hEventLog (initialized with 0 means that event log must be opened)
if ( argc >= 3 )
if (argv[2].strlength != 0) hEventLog = (HANDLE)atol(argv[2].strptr);
if (access == 'L')
{
//check parameter BackupFileName
//if no BackupFileName, no backup, 0 pointer required !, pchBackupFileName is initialized with NULL)
if ( argc >= 9 )
if (argv[8].strlength != 0) pchBackupFileName = argv[8].strptr;
}
//no handle as input, so the event log must be opened
if (hEventLog == 0)
{
if ( (hEventLog = OpenEventLog(pchServer,pchSource)) == 0 )
RETVAL(GetLastError())
else
{
if (access == 'N')
rc = GetNumberOfEventLogRecords(hEventLog,&numEvRecords);
else
rc = ClearEventLog(hEventLog,pchBackupFileName);
CloseEventLog(hEventLog);
if (access == 'N')
{
if (rc) RETVAL((INT)numEvRecords)
else RETC(0)
}
else
{
if (rc) RETC(0)
else RETVAL(GetLastError())
}
}
}
else
{
if (access == 'N')
{
if ( GetNumberOfEventLogRecords(hEventLog,&numEvRecords) )
RETVAL((INT)numEvRecords)
else RETC(0)
}
else
{
rc = ClearEventLog(hEventLog,pchBackupFileName);
if (rc) RETC(0)
else RETVAL(GetLastError())
}
}
}
if (access == 'W')
{
WORD wEventType = 1;
WORD wEventCategory = 0;
DWORD dwEventId = 0;
LPVOID lpData = (void *)0;
DWORD dwDataSize = 0;
WORD wNumStrings = 0;
char *lpStrings[100];
//LPCTSTR * lpStrings[100];
//check and prepare event log data
// check the event type (initialized to 1 = default)
// EVENTLOG_SUCCESS 0X0000
// EVENTLOG_ERROR_TYPE 0x0001
// EVENTLOG_WARNING_TYPE 0x0002
// EVENTLOG_INFORMATION_TYPE 0x0004
// EVENTLOG_AUDIT_SUCCESS 0x0008
// EVENTLOG_AUDIT_FAILURE 0x0010
if ( argc >= 10 && argv[9].strlength != 0 )
wEventType = atoi(argv[9].strptr);
// check the event category (initialized to 0 = default)
if ( argc >= 11 && argv[10].strlength != 0 )
wEventCategory = atoi(argv[10].strptr);
// check the event id (initialized to 0 = default)
if ( argc >= 12 && argv[11].strlength != 0 )
dwEventId = atoi(argv[11].strptr);
// check the event data (initialized to NULL = default)
if ( argc >= 13 && argv[12].strlength != 0 )
{
lpData = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,argv[12].strlength+1);
sprintf(lpData, "%s", argv[12].strptr);
dwDataSize = argv[12].strlength;
}
// check the event strings (initialized to NULL = default)
if ( argc >= 14 && argv[13].strlength != 0 )
{
int j=0;
ULONG i;
for ( i = 13; i < argc; i++ )
{
//count the number of strings and create the string array
wNumStrings++;
lpStrings[j] = argv[i].strptr;
j++;
}
}
//register the event source
if ( (hEventLog = RegisterEventSource( pchServer, pchSource)) == NULL )
rcGetLastError = GetLastError();
if ( !rcGetLastError )
{
//write the log
if ( ReportEvent( hEventLog,
wEventType,
wEventCategory,
dwEventId,
NULL,
wNumStrings,
dwDataSize,
lpStrings,
lpData) == NULL ) rcGetLastError = GetLastError();
//deregister the event source
DeregisterEventSource( hEventLog);
//free buffer for data
if ( lpData != (void*)0 ) GlobalFree(lpData);
}
if ( rcGetLastError )
RETVAL(rcGetLastError)
else
RETC(0)
}
//read
if (access == 'R')
{
PEVENTLOGRECORD pEvLogRecord; //pointer to one event record
PVOID pBuffer = 0; //buffer for event records
DWORD pnBytesRead; //returned from ReadEventlog
DWORD pnMinNumberOfBytesNeeded; //returned from ReadEventlog if not all read
char * pchEventSource = (char *)0; //source from event log record
char * pchComputerName = (char *)0; //computer name of event
char * pchMessage=0; //text of detail string
char * pchData=0; //data of event log record
char time[MAX_TIME_DATE]; //converted time of event
char date[MAX_TIME_DATE]; //converted date of event
struct tm * DateTime; //converted from elapsed seconds
char * pchStrBuf = (char *)0; //temp buffer for event string
DWORD bufferPos=0; //position within the eventlog buffer
BOOL cont = 1; //continue flag for while loop
char evType[5][12]={"Error","Warning","Information","Success","Failure"}; //event type strings
int evTypeIndex;
char * pchUser;
ULONG count; //number of event log records retuned in stem
//no handle as input, so the event log must be opened
if (hEventLog == 0)
{
if ( (hEventLog = OpenEventLog(pchServer,pchSource)) == 0 )
{
RETVAL(GetLastError())
}
}
GetNumberOfEventLogRecords(hEventLog,&numEvRecords);
if (start > numEvRecords) RETC(1)
pBuffer = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, bufSize);
count = 0;
while (cont)
{
while ( (rc = ReadEventLog(hEventLog,
dwReadFlags,
start,
pBuffer,
bufSize,
&pnBytesRead,
&pnMinNumberOfBytesNeeded) ) && (count < num) )
{
pEvLogRecord = pBuffer;
bufferPos = 0;
while ( (bufferPos < pnBytesRead) && (count < num) )
{
if (dwReadFlags & EVENTLOG_FORWARDS_READ)
start = pEvLogRecord->RecordNumber+1;
else
start = pEvLogRecord->RecordNumber-1;
count++; //count number of events
//get index to event type string
GET_TYPE_INDEX(pEvLogRecord->EventType, evTypeIndex)
//get time and date
//DateTime = localtime(&pEvLogRecord->TimeGenerated); // convert to local time
DateTime = localtime(&pEvLogRecord->TimeWritten); // convert to local time
strftime(date, MAX_TIME_DATE,"%x", DateTime);
strftime(time, MAX_TIME_DATE,"%X", DateTime);
pchEventSource = (char*)pEvLogRecord + sizeof(EVENTLOGRECORD);
pchComputerName = pchEventSource + strlen(pchEventSource)+1;
pchUser = GetUser(pEvLogRecord);
BuildMessage(pEvLogRecord,pchSource,&pchMessage);
GetEvData(pEvLogRecord,&pchData);
pchStrBuf = GlobalAlloc(GMEM_FIXED,
strlen(evType[evTypeIndex])+1+
strlen(date)+1+
strlen(time)+1+
strlen(pchEventSource)+3+
12+
strlen(pchUser)+1+
strlen(pchComputerName)+1+
strlen(pchMessage)+3+
strlen(pchData)+3+10);
//Type Date Time Source Id User Computer Details
sprintf( pchStrBuf,
"%s %s %s '%s' %u %s %s '%s' '%s'",
evType[evTypeIndex],
date,
time,
pchEventSource,
LOWORD(pEvLogRecord->EventID),
pchUser,
pchComputerName,
pchMessage,
pchData);
GlobalFree(pchMessage);
GlobalFree(pchData);
pchMessage = 0;
GlobalFree(pchUser);
//write record to to stem
ltoa(count, varName+stemLen, 10);
shvb.shvnext = NULL;
shvb.shvname.strptr = varName;
shvb.shvname.strlength = strlen(varName);
shvb.shvvalue.strptr = pchStrBuf;
shvb.shvvalue.strlength = strlen(pchStrBuf);
shvb.shvnamelen = shvb.shvname.strlength;
shvb.shvvaluelen = shvb.shvvalue.strlength;
shvb.shvcode = RXSHV_SET;
shvb.shvret = 0;
rc = RexxVariablePool(&shvb);
GlobalFree(pchStrBuf);
if (rc == RXSHV_BADN)
{
if (pBuffer) GlobalFree(pBuffer);
return 2;
}
bufferPos += pEvLogRecord->Length;
//position to next event record
pEvLogRecord = (PEVENTLOGRECORD)(((char*)pEvLogRecord) + pEvLogRecord->Length);
}
memset(pBuffer,0,bufSize);
}
rc = GetLastError();
// if buffer is to small, reallocate it, pnMinNumberOfBytesNeeded is returned from ReadEventLog
if ( (count == num) || (start > numEvRecords) || (start < 1) )
cont = 0;
else
{
if (rc == ERROR_INSUFFICIENT_BUFFER)
{
bufSize += pnMinNumberOfBytesNeeded;
pBuffer = GlobalReAlloc(pBuffer,bufSize,GMEM_FIXED|GMEM_ZEROINIT);
}
else
cont = 0; //stop reading, reached end or error (except ERROR_INSUFFICIENT_BUFFER)
}
}//while (cont)
//close event log only if it was not opened before (imnplicit open during read)
if ( atoi(argv[2].strptr) == 0 )
CloseEventLog(hEventLog);
if (pBuffer) GlobalFree(pBuffer);
if ( rc == ERROR_HANDLE_EOF || count == num || (start > numEvRecords) || (start < 1) ) //all OK complete log read, also true for an empty log
{
char strBuffer[8]; //string for number of records read
itoa(0, varName+stemLen, 10);
itoa(count, strBuffer, 10);
SET_VARIABLE(varName, strBuffer, 2);
RETC(0)
}
else
RETVAL(rc)
}
RETERR
}
ULONG APIENTRY WSCtrlWindow(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
HWND hW;
char *tmp_ptr;
DWORD dwResult;
LRESULT lResult;
CHECKARG(1,10);
if (!strcmp(argv[0].strptr,"FIND"))
{
CHECKARG(2,2);
hW = FindWindow(NULL, argv[1].strptr);
ltoa((ULONG)hW, retstr->strptr, 10);
retstr->strlength = strlen(retstr->strptr);
return 0;
}
else if (!strcmp(argv[0].strptr,"FINDCHILD"))
{
CHECKARG(3,3);
hW = FindWindowEx((HWND)atol(argv[1].strptr), NULL, NULL, argv[2].strptr);
ltoa((ULONG)hW, retstr->strptr, 10);
retstr->strlength = strlen(retstr->strptr);
return 0;
}
else if (!strcmp(argv[0].strptr,"SETFG"))
{
CHECKARG(2,2);
hW = GetForegroundWindow();
if (hW == (HWND)atol(argv[1].strptr)) RETVAL((ULONG)hW)
SetForegroundWindow((HWND)atol(argv[1].strptr));
RETVAL((ULONG) hW) /* return the handle of the window that had the focus before */
}
else if (!strcmp(argv[0].strptr,"GETFG"))
{
RETVAL((ULONG) GetForegroundWindow())
}
else if (!strcmp(argv[0].strptr,"TITLE"))
{
CHECKARG(2,3);
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
if (argc ==3) RETC(!SetWindowText(hW, argv[2].strptr))
else
{
// GetWindowText do not work for controls from other processes and for listboxes
// Now using WM_GETTEXT which works for all controls.
// The return string was limitted to 255 characters, now it's unlimited.
// retstr->strlength = GetWindowText(hW, retstr->strptr, 255);
// at first get the text length to determine if the default lenght (255)of strptr would be exceeded.
lResult = SendMessageTimeout(hW, WM_GETTEXTLENGTH ,0, 0, MSG_TIMEOUT_OPTS, MSG_TIMEOUT, &dwResult);
// time out or error?
if (lResult == 0) {
// an error, but we return an empty string
retstr->strlength = 0;
return 0;
}
else {
retstr->strlength = dwResult;
}
if ( retstr->strlength > (RXAUTOBUFLEN - 1) )
{
// text larger than 255, allocate a new one
// the original REXX retstr->strptr must not be released! REXX keeps track of this
tmp_ptr = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, retstr->strlength + 1);
if (!tmp_ptr) RETERR
retstr->strptr = tmp_ptr;
}
// now get the text
lResult = SendMessageTimeout(hW, WM_GETTEXT, retstr->strlength + 1, (LPARAM)retstr->strptr,
MSG_TIMEOUT_OPTS, MSG_TIMEOUT, &dwResult);
// time out or error?
if (lResult == 0) {
// an error, but we return an empty string
retstr->strlength = 0;
return 0;
}
else {
retstr->strlength = dwResult;
}
// if still no text found, the control may be a listbox, for which LB_GETTEXT must be used
if ( retstr->strlength == 0 )
{
// at first get the selected item
int curSel;
lResult = SendMessageTimeout(hW, LB_GETCURSEL, 0, 0, MSG_TIMEOUT_OPTS, MSG_TIMEOUT, &dwResult);
// time out or error?
if (lResult == 0) {
// an error, but we return an empty string
retstr->strlength = 0;
return 0;
}
else {
curSel = (int) dwResult;
}
if (curSel != LB_ERR)
{
// get the text length and allocate a new strptr if needed
lResult = SendMessageTimeout(hW, LB_GETTEXTLEN, curSel, 0, MSG_TIMEOUT_OPTS, MSG_TIMEOUT, &dwResult);
// time out or error?
if (lResult == 0) {
// an error, but we return an empty string
retstr->strlength = 0;
return 0;
}
else {
retstr->strlength = dwResult;
}
if ( retstr->strlength > (RXAUTOBUFLEN - 1) )
{
tmp_ptr = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, retstr->strlength + 1);
if (!tmp_ptr) RETERR
retstr->strptr = tmp_ptr;
}
lResult = SendMessageTimeout(hW, LB_GETTEXT, curSel, (LPARAM)retstr->strptr, MSG_TIMEOUT_OPTS, MSG_TIMEOUT, &dwResult);
// time out or error?
if (lResult == 0) {
// an error, but we return an empty string
retstr->strlength = 0;
return 0;
}
else {
retstr->strlength = dwResult;
}
}
}
}
return 0;
}
else
{
if (argc ==3) RETC(!SetConsoleTitle(argv[2].strptr))
else retstr->strlength = GetConsoleTitle(retstr->strptr, 255);
return 0;
}
}
else if (!strcmp(argv[0].strptr,"CLASS"))
{
CHECKARG(2,2);
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
retstr->strlength = GetClassName(hW, retstr->strptr, 255);
return 0;
}
}
else if (!strcmp(argv[0].strptr,"SHOW"))
{
CHECKARG(3,3);
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
INT sw;
if (!strcmp(argv[2].strptr,"HIDE")) sw = SW_HIDE; else
if (!strcmp(argv[2].strptr,"MAX")) sw = SW_SHOWMAXIMIZED; else
if (!strcmp(argv[2].strptr,"MIN")) sw = SW_MINIMIZE; else
if (!strcmp(argv[2].strptr,"RESTORE")) sw = SW_RESTORE;
else sw = SW_SHOW;
RETC(ShowWindow(hW, sw))
}
}
else if (!strcmp(argv[0].strptr,"HNDL"))
{
CHECKARG(3,3);
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
INT gw;
if (!strcmp(argv[2].strptr,"NEXT")) gw = GW_HWNDNEXT; else
if (!strcmp(argv[2].strptr,"PREV")) gw = GW_HWNDPREV; else
if (!strcmp(argv[2].strptr,"FIRSTCHILD")) gw = GW_CHILD; else
if (!strcmp(argv[2].strptr,"FIRST")) gw = GW_HWNDFIRST; else
if (!strcmp(argv[2].strptr,"LAST")) gw = GW_HWNDLAST; else
if (!strcmp(argv[2].strptr,"OWNER")) gw = GW_OWNER; else RETC(1)
RETVAL((ULONG)GetWindow(hW, gw))
}
else RETC(0)
}
else if (!strcmp(argv[0].strptr,"ID"))
{
CHECKARG(2,2);
hW = (HWND) atol(argv[1].strptr);
RETVAL(GetDlgCtrlID(hW))
}
else if (!strcmp(argv[0].strptr,"ATPOS"))
{
POINT pt;
CHECKARG(3,4);
pt.x = atol(argv[1].strptr);
pt.y = atol(argv[2].strptr);
if (argc == 4) hW = (HWND) atol(argv[3].strptr); else hW = 0;
if (hW) RETVAL((ULONG)ChildWindowFromPointEx(hW, pt, CWP_ALL))
else RETVAL((ULONG)WindowFromPoint(pt))
}
else if (!strcmp(argv[0].strptr,"RECT"))
{
RECT r;
CHECKARG(2,2);
retstr->strlength = 0;
hW = (HWND) atol(argv[1].strptr);
if (hW && GetWindowRect(hW, &r))
{
sprintf(retstr->strptr,"%d,%d,%d,%d",r.left, r.top, r.right, r.bottom);
retstr->strlength = strlen(retstr->strptr);
}
return 0;
}
else if (!strcmp(argv[0].strptr,"GETSTATE"))
{
CHECKARG(2,2);
retstr->strlength = 0;
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
retstr->strptr[0] = '\0';
if (!IsWindow(hW)) strcpy(retstr->strptr, "Illegal Handle");
else {
if (IsWindowEnabled(hW)) strcat(retstr->strptr, "Enabled ");
else strcat(retstr->strptr, "Disabled ");
if (IsWindowVisible(hW)) strcat(retstr->strptr, "Visible ");
else strcat(retstr->strptr, "Invisible ");
if (IsZoomed(hW)) strcat(retstr->strptr, "Zoomed ");
else if (IsIconic(hW)) strcat(retstr->strptr, "Minimized ");
if (GetForegroundWindow() == hW) strcat(retstr->strptr, "Foreground ");
}
retstr->strlength = strlen(retstr->strptr);
}
return 0;
}
else if (!strcmp(argv[0].strptr,"ENABLE"))
{
CHECKARG(3,3);
hW = (HWND) atol(argv[1].strptr);
if (hW) RETC(EnableWindow(hW, atoi(argv[2].strptr)))
}
else if (!strcmp(argv[0].strptr,"MOVE"))
{
CHECKARG(4,4);
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
RECT r;
if (!GetWindowRect(hW, &r)) RETC(1);
RETC(!MoveWindow(hW, atol(argv[2].strptr), atol(argv[3].strptr), r.right - r.left, r.bottom-r.top, TRUE))
}
}
else if (!strcmp(argv[0].strptr,"SIZE"))
{
CHECKARG(4,4);
hW = (HWND) atol(argv[1].strptr);
if (hW)
{
RECT r;
if (!GetWindowRect(hW, &r)) RETC(1);
RETC(!SetWindowPos(hW, NULL, r.left, r.top, atol(argv[2].strptr), atol(argv[3].strptr), SWP_NOMOVE | SWP_NOZORDER))
}
}
RETC(1) /* in this case 0 is an error */
}
ULONG APIENTRY WSCtrlSend(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
HWND hW,HwndFgWin;
CHECKARG(1,10);
if (!strcmp(argv[0].strptr,"KEY"))
{
CHECKARG(4,5);
hW = (HWND)atol(argv[1].strptr);
if (hW)
{
WPARAM k;
LPARAM l;
k = (WPARAM)atoi(argv[2].strptr);
if (argc == 5) l = strtol(argv[4].strptr,'\0',16); else l = 0;
/* Combination of keys (e.g. SHIFT END) and function keys (e.g. F1) dont worked */
/* The reson is, that the WM_KEWUP/Doen message cannot handle this ! */
/* To get the function keys working, WM_KEYDOWN/UP can be used, but the message MUST be posted! */
/* To get key combination working the keybd_event function must be use. But ..... */
/* The window must be in the Foreground and The handle must be associated: */
/* To get these working, the REXX code must be lok like: */
/* EditHandle = npe~Handle */
/* npe~ToForeground */
/* npe~AssocWindow(np~Handle) */
/* npe~SendKeyDown("SHIFT") */
/* npe~SendKeyDown("HOME") */
/* npe~SendKeyUp("SHIFT") */
/* npe~SendKeyUp("HOME") */
if (argv[3].strptr[0] == 'D')
{
HwndFgWin = GetForegroundWindow();
if ( hW == HwndFgWin)
{
if (l != 0)
keybd_event((BYTE)k,0,KEYEVENTF_EXTENDEDKEY,0);
else
keybd_event((BYTE)k,0,0,0);
retstr->strptr[0] = '1';
retstr->strptr[1] = '\0';
}
else
itoa(PostMessage(hW,WM_KEYDOWN, k,l), retstr->strptr, 10);
}
else
if (argv[3].strptr[0] == 'U')
{
HwndFgWin = GetForegroundWindow();
if ( hW == HwndFgWin )
{
if (l == 1)
keybd_event((BYTE)k,0,KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY,0);
else
keybd_event((BYTE)k,0,KEYEVENTF_KEYUP,0);
retstr->strptr[0] = '1';
retstr->strptr[1] = '\0';
}
else
{
itoa(PostMessage(hW,WM_KEYUP, k, l | 0xC0000000), retstr->strptr, 10);
retstr->strptr[0] = '1';
retstr->strptr[1] = '\0';
}
}
else
/* WM_CHAR don't works for ALT key combinations */
/* WM_SYSKEYDOWN and hW,WM_SYSKEYUP must used */
if ( l != 0 )
{
itoa(PostMessage(hW,WM_SYSKEYDOWN, k,l), retstr->strptr, 10);
itoa(PostMessage(hW,WM_SYSKEYUP, k,l), retstr->strptr, 10);
}
else
itoa(SendNotifyMessage(hW,WM_CHAR, k,l), retstr->strptr, 10);
retstr->strlength = strlen(retstr->strptr);
return 0;
}
}
else if (!strcmp(argv[0].strptr,"MSG"))
{
ULONG n[3];
CHECKARG(5,5);
hW = (HWND)atol(argv[1].strptr);
if (hW)
{
INT i;
for (i=0;i<3;i++)
{
if (ISHEX(argv[i+2].strptr))
n[i] = strtol(argv[i+2].strptr,'\0',16);
else
n[i] = atol(argv[i+2].strptr);
}
itoa(SendNotifyMessage(hW,n[0], (WPARAM)n[1], (LPARAM)n[2]), retstr->strptr, 10);
return 0;
}
}
else if (!strcmp(argv[0].strptr,"MAP"))
{
CHECKARG(2,2);
RETVAL(MapVirtualKey((UINT)atoi(argv[1].strptr), 2))
}
RETC(1) /* in this case 0 is an error */
}
ULONG APIENTRY WSCtrlMenu(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
CHECKARG(1,10);
if (!strcmp(argv[0].strptr,"GET"))
{
CHECKARG(2,2);
RETVAL((ULONG)GetMenu((HWND)atol(argv[1].strptr)))
}
else if (!strcmp(argv[0].strptr,"SYS"))
{
CHECKARG(2,2);
RETVAL((ULONG)GetSystemMenu((HWND)atol(argv[1].strptr), FALSE))
}
else if (!strcmp(argv[0].strptr,"SUB"))
{
CHECKARG(3,3);
RETVAL((ULONG)GetSubMenu((HMENU)atol(argv[1].strptr), atoi(argv[2].strptr)))
}
else if (!strcmp(argv[0].strptr,"ID"))
{
CHECKARG(3,3);
RETVAL(GetMenuItemID((HMENU)atol(argv[1].strptr), atoi(argv[2].strptr)))
}
else if (!strcmp(argv[0].strptr,"CNT"))
{
CHECKARG(2,2);
RETVAL(GetMenuItemCount((HMENU)atol(argv[1].strptr)))
}
else if (!strcmp(argv[0].strptr,"TEXT"))
{
HMENU hM;
UINT flag;
CHECKARG(4,4);
hM= (HMENU)atol(argv[1].strptr);
if (!hM) RETC(0)
if (argv[3].strptr[0] == 'C') flag = MF_BYCOMMAND; else flag = MF_BYPOSITION;
retstr->strlength = GetMenuString(hM, atol(argv[2].strptr), retstr->strptr, 255, flag);
return 0;
}
else if (!strcmp(argv[0].strptr,"STATE"))
{
CHECKARG(2,2);
RETVAL(IsMenu((HMENU)atol(argv[1].strptr)))
}
RETC(1) /* in this case 0 is an error */
}
LONG APIENTRY WSClipboard(
PUCHAR funcname,
ULONG argc,
RXSTRING argv[],
PUCHAR qname,
PRXSTRING retstr )
{
BOOL ret = FALSE;
CHECKARG(1,2);
if (!strcmp(argv[0].strptr, "COPY"))
{
HGLOBAL hmem;
char * membase;
CHECKARG(2,2);
hmem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, argv[1].strlength + 1);
membase = (char *) GlobalLock(hmem);
if (membase)
{
memcpy(membase, argv[1].strptr, argv[1].strlength + 1);
if (OpenClipboard(NULL) && EmptyClipboard())
ret = (BOOL)SetClipboardData(CF_TEXT, hmem);
GlobalUnlock(membase);
CloseClipboard();
}
RETC(!ret)
}
else
if (!strcmp(argv[0].strptr, "PASTE"))
{
HGLOBAL hmem;
char * membase;
if (IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(NULL))
{
hmem = GetClipboardData(CF_TEXT);
membase = (char *) GlobalLock(hmem);
if (membase)
{
ULONG s = GlobalSize(hmem);
if (s>255) {
void * p = GlobalAlloc(GMEM_FIXED, s);
if (!p)
{
CloseClipboard();
return -1;
}
retstr->strptr = p;
}
s = strlen(membase);
memcpy(retstr->strptr, membase, s+1);
retstr->strlength = s;
GlobalUnlock(membase);
CloseClipboard();
return 0;
} else CloseClipboard();
}
retstr->strlength = 0;
return 0;
}
else
if (!strcmp(argv[0].strptr, "EMPTY"))
{
if (IsClipboardFormatAvailable(CF_TEXT))
{
BOOL ret;
if (!OpenClipboard(NULL)) RETC(1)
ret = !EmptyClipboard();
CloseClipboard();
RETC(ret)
}
RETC(0)
}
else
if (!strcmp(argv[0].strptr, "AVAIL"))
{
RETC(IsClipboardFormatAvailable(CF_TEXT))
}
else RETVAL(-1)
}
VOID Little2BigEndian(BYTE *pbInt, INT iSize)
{
/* convert any integer number from little endian to big endian or vice versa */
BYTE bTemp[32];
INT i;
if (iSize <= 32)
{
for (i = 0; i < iSize; i++)
bTemp[i] = pbInt[iSize - i - 1];
memcpy(pbInt, bTemp, iSize);
} /* endif */
}