vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
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
//! `GetAutoServerSettings` Function
//!
//! Returns information about the security settings for a `DCOM` (`Distributed Component Object Model`) server.
//!
//! # Syntax
//!
//! ```vb
//! GetAutoServerSettings(progid, clsid, machine)
//! ```
//!
//! # Parameters
//!
//! - `progid` - Required. String expression that specifies the programmatic identifier (`ProgID`) of the server.
//! - `clsid` - Required. String expression that specifies the class identifier (`CLSID`) of the server.
//! - `machine` - Required. String expression that specifies the name of the machine where the server is located.
//!
//! # Return Value
//!
//! Returns a `Long` value containing security settings information for the specified `DCOM` server.
//!
//! # Remarks
//!
//! - This function is specific to `DCOM` (`Distributed Component Object Model`) automation servers.
//! - Used primarily in distributed computing scenarios.
//! - Returns security configuration information from the Windows registry.
//! - The function is part of VB6's `DCOM` support infrastructure.
//! - Typically used in enterprise applications with distributed components.
//! - Requires appropriate `DCOM` permissions on the target machine.
//! - The progid and clsid must correspond to a registered `COM`/`DCOM` server.
//! - Machine name can be a `NetBIOS` name, `DNS` name, or `IP` address.
//! - Returns 0 if the server settings cannot be retrieved.
//!
//! # Typical Uses
//!
//! - Querying DCOM server security configurations
//! - Validating remote server accessibility
//! - Auditing distributed component settings
//! - Troubleshooting DCOM connection issues
//! - Enterprise application deployment verification
//! - Remote component diagnostics
//!
//! # Basic Usage Examples
//!
//! ```vb
//! ' Check DCOM server settings
//! Dim settings As Long
//! settings = GetAutoServerSettings("MyServer.Application", _
//!                                   "{12345678-1234-1234-1234-123456789012}", _
//!                                   "SERVER01")
//!
//! If settings <> 0 Then
//!     Debug.Print "Server settings retrieved: " & settings
//! Else
//!     Debug.Print "Unable to retrieve server settings"
//! End If
//!
//! ' Verify remote component availability
//! Dim result As Long
//! result = GetAutoServerSettings("Excel.Application", _
//!                                "{00024500-0000-0000-C000-000000000046}", _
//!                                "REMOTE-PC")
//!
//! If result <> 0 Then
//!     MsgBox "Remote Excel server is configured"
//! End If
//!
//! ' Query local server settings
//! Dim localSettings As Long
//! localSettings = GetAutoServerSettings("MyApp.Server", _
//!                                       "{ABCDEF01-2345-6789-ABCD-EF0123456789}", _
//!                                       ".")
//! ```
//!
//! # Common Patterns
//!
//! ## 1. DCOM Server Validation
//!
//! ```vb
//! Function ValidateDCOMServer(progID As String, _
//!                             clsID As String, _
//!                             serverName As String) As Boolean
//!     Dim settings As Long
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     settings = GetAutoServerSettings(progID, clsID, serverName)
//!     
//!     If settings <> 0 Then
//!         Debug.Print "DCOM server validated on " & serverName
//!         ValidateDCOMServer = True
//!     Else
//!         Debug.Print "DCOM server not accessible on " & serverName
//!         ValidateDCOMServer = False
//!     End If
//!     
//!     Exit Function
//!     
//! ErrorHandler:
//!     Debug.Print "Error validating DCOM server: " & Err.Description
//!     ValidateDCOMServer = False
//! End Function
//! ```
//!
//! ## 2. Multi-Server Configuration Check
//!
//! ```vb
//! Sub CheckServersConfiguration()
//!     Dim servers() As String
//!     Dim i As Long
//!     Dim settings As Long
//!     
//!     servers = Array("SERVER01", "SERVER02", "SERVER03")
//!     
//!     For i = LBound(servers) To UBound(servers)
//!         settings = GetAutoServerSettings("MyApp.DataServer", _
//!                                          "{11111111-2222-3333-4444-555555555555}", _
//!                                          servers(i))
//!         
//!         If settings <> 0 Then
//!             Debug.Print servers(i) & " - Configured: " & settings
//!         Else
//!             Debug.Print servers(i) & " - Not configured"
//!         End If
//!     Next i
//! End Sub
//! ```
//!
//! ## 3. Server Discovery and Verification
//!
//! ```vb
//! Function FindAvailableServer(progID As String, _
//!                              clsID As String, _
//!                              servers As Collection) As String
//!     Dim server As Variant
//!     Dim settings As Long
//!     
//!     For Each server In servers
//!         On Error Resume Next
//!         settings = GetAutoServerSettings(progID, clsID, CStr(server))
//!         On Error GoTo 0
//!         
//!         If settings <> 0 Then
//!             FindAvailableServer = CStr(server)
//!             Exit Function
//!         End If
//!     Next server
//!     
//!     FindAvailableServer = ""
//! End Function
//!
//! ' Usage
//! Dim servers As New Collection
//! servers.Add "PRIMARY-SERVER"
//! servers.Add "BACKUP-SERVER"
//! servers.Add "FAILOVER-SERVER"
//!
//! Dim activeServer As String
//! activeServer = FindAvailableServer("MyApp.Service", _
//!                                    "{FEDCBA98-7654-3210-FEDC-BA9876543210}", _
//!                                    servers)
//!
//! If activeServer <> "" Then
//!     Debug.Print "Using server: " & activeServer
//! End If
//! ```
//!
//! ## 4. Settings Comparison Across Servers
//!
//! ```vb
//! Sub CompareServerSettings(progID As String, _
//!                           clsID As String, _
//!                           server1 As String, _
//!                           server2 As String)
//!     Dim settings1 As Long
//!     Dim settings2 As Long
//!     
//!     settings1 = GetAutoServerSettings(progID, clsID, server1)
//!     settings2 = GetAutoServerSettings(progID, clsID, server2)
//!     
//!     Debug.Print "Server: " & server1 & " - Settings: " & settings1
//!     Debug.Print "Server: " & server2 & " - Settings: " & settings2
//!     
//!     If settings1 = settings2 Then
//!         Debug.Print "Servers have identical settings"
//!     Else
//!         Debug.Print "Warning: Server settings differ"
//!     End If
//! End Sub
//! ```
//!
//! ## 5. Dynamic Server Connection
//!
//! ```vb
//! Function ConnectToServer(progID As String, _
//!                          clsID As String, _
//!                          preferredServer As String, _
//!                          fallbackServer As String) As Object
//!     Dim settings As Long
//!     Dim targetServer As String
//!     
//!     ' Try preferred server first
//!     settings = GetAutoServerSettings(progID, clsID, preferredServer)
//!     
//!     If settings <> 0 Then
//!         targetServer = preferredServer
//!     Else
//!         ' Fall back to alternate server
//!         settings = GetAutoServerSettings(progID, clsID, fallbackServer)
//!         
//!         If settings <> 0 Then
//!             targetServer = fallbackServer
//!         Else
//!             Err.Raise vbObjectError + 1000, , "No available servers"
//!         End If
//!     End If
//!     
//!     Debug.Print "Connecting to: " & targetServer
//!     Set ConnectToServer = CreateObject(progID, targetServer)
//! End Function
//! ```
//!
//! ## 6. Server Health Monitoring
//!
//! ```vb
//! Type ServerStatus
//!     ServerName As String
//!     Settings As Long
//!     LastChecked As Date
//!     IsAvailable As Boolean
//! End Type
//!
//! Function CheckServerHealth(progID As String, _
//!                           clsID As String, _
//!                           serverName As String) As ServerStatus
//!     Dim status As ServerStatus
//!     
//!     status.ServerName = serverName
//!     status.LastChecked = Now
//!     
//!     On Error Resume Next
//!     status.Settings = GetAutoServerSettings(progID, clsID, serverName)
//!     On Error GoTo 0
//!     
//!     status.IsAvailable = (status.Settings <> 0)
//!     
//!     CheckServerHealth = status
//! End Function
//!
//! Sub MonitorServers()
//!     Dim servers() As String
//!     Dim i As Long
//!     Dim status As ServerStatus
//!     
//!     servers = Array("SERVER-A", "SERVER-B", "SERVER-C")
//!     
//!     For i = LBound(servers) To UBound(servers)
//!         status = CheckServerHealth("MyApp.Service", _
//!                                   "{12345678-ABCD-EFGH-IJKL-123456789ABC}", _
//!                                   servers(i))
//!         
//!         Debug.Print status.ServerName & ": " & _
//!                     IIf(status.IsAvailable, "Online", "Offline") & _
//!                     " (" & status.Settings & ")"
//!     Next i
//! End Sub
//! ```
//!
//! ## 7. Configuration Auditing
//!
//! ```vb
//! Sub AuditDCOMConfiguration(progID As String, clsID As String)
//!     Dim servers() As String
//!     Dim i As Long
//!     Dim settings As Long
//!     Dim fileNum As Integer
//!     
//!     servers = Array("PROD-01", "PROD-02", "TEST-01", "DEV-01")
//!     
//!     fileNum = FreeFile
//!     Open "C:\Audit\DCOM_Audit.txt" For Output As #fileNum
//!     
//!     Print #fileNum, "DCOM Configuration Audit Report"
//!     Print #fileNum, "Date: " & Now
//!     Print #fileNum, "ProgID: " & progID
//!     Print #fileNum, "CLSID: " & clsID
//!     Print #fileNum, String(50, "=")
//!     
//!     For i = LBound(servers) To UBound(servers)
//!         settings = GetAutoServerSettings(progID, clsID, servers(i))
//!         
//!         Print #fileNum, "Server: " & servers(i)
//!         Print #fileNum, "  Settings Value: " & settings
//!         Print #fileNum, "  Status: " & IIf(settings <> 0, "Available", "Unavailable")
//!         Print #fileNum, ""
//!     Next i
//!     
//!     Close #fileNum
//!     Debug.Print "Audit complete"
//! End Sub
//! ```
//!
//! ## 8. Load Balancing Server Selection
//!
//! ```vb
//! Function SelectLeastLoadedServer(progID As String, _
//!                                  clsID As String, _
//!                                  servers As Collection) As String
//!     Dim server As Variant
//!     Dim settings As Long
//!     Dim minSettings As Long
//!     Dim selectedServer As String
//!     
//!     minSettings = 2147483647  ' Max Long value
//!     
//!     For Each server In servers
//!         On Error Resume Next
//!         settings = GetAutoServerSettings(progID, clsID, CStr(server))
//!         On Error GoTo 0
//!         
//!         If settings <> 0 And settings < minSettings Then
//!             minSettings = settings
//!             selectedServer = CStr(server)
//!         End If
//!     Next server
//!     
//!     SelectLeastLoadedServer = selectedServer
//! End Function
//! ```
//!
//! ## 9. Deployment Verification
//!
//! ```vb
//! Function VerifyDeployment(progID As String, _
//!                          clsID As String, _
//!                          targetServers As Variant) As Boolean
//!     Dim i As Long
//!     Dim settings As Long
//!     Dim allConfigured As Boolean
//!     
//!     allConfigured = True
//!     
//!     For i = LBound(targetServers) To UBound(targetServers)
//!         settings = GetAutoServerSettings(progID, clsID, targetServers(i))
//!         
//!         If settings = 0 Then
//!             Debug.Print "Deployment failed on: " & targetServers(i)
//!             allConfigured = False
//!         Else
//!             Debug.Print "Deployment verified on: " & targetServers(i)
//!         End If
//!     Next i
//!     
//!     VerifyDeployment = allConfigured
//! End Function
//!
//! ' Usage in deployment script
//! Sub DeploymentCheck()
//!     Dim productionServers As Variant
//!     
//!     productionServers = Array("WEB-01", "WEB-02", "APP-01", "APP-02")
//!     
//!     If VerifyDeployment("MyApp.BusinessLogic", _
//!                        "{AAAABBBB-CCCC-DDDD-EEEE-FFFF00001111}", _
//!                        productionServers) Then
//!         MsgBox "Deployment successful on all servers"
//!     Else
//!         MsgBox "Deployment incomplete - check logs"
//!     End If
//! End Sub
//! ```
//!
//! ## 10. Regional Server Discovery
//!
//! ```vb
//! Type RegionalServer
//!     Region As String
//!     ServerName As String
//!     Settings As Long
//! End Type
//!
//! Function GetRegionalServer(progID As String, _
//!                           clsID As String, _
//!                           region As String) As String
//!     Dim regionalServers(1 To 3) As RegionalServer
//!     Dim i As Long
//!     
//!     ' Define regional servers
//!     regionalServers(1).Region = "US-EAST"
//!     regionalServers(1).ServerName = "US-EAST-SVR01"
//!     
//!     regionalServers(2).Region = "US-WEST"
//!     regionalServers(2).ServerName = "US-WEST-SVR01"
//!     
//!     regionalServers(3).Region = "EUROPE"
//!     regionalServers(3).ServerName = "EU-SVR01"
//!     
//!     ' Check settings for each regional server
//!     For i = 1 To 3
//!         regionalServers(i).Settings = GetAutoServerSettings(progID, _
//!                                                              clsID, _
//!                                                              regionalServers(i).ServerName)
//!     Next i
//!     
//!     ' Find matching region
//!     For i = 1 To 3
//!         If regionalServers(i).Region = region And regionalServers(i).Settings <> 0 Then
//!             GetRegionalServer = regionalServers(i).ServerName
//!             Exit Function
//!         End If
//!     Next i
//!     
//!     GetRegionalServer = ""
//! End Function
//! ```
//!
//! # Advanced Usage
//!
//! ## 1. DCOM Server Manager Class
//!
//! ```vb
//! ' Class: DCOMServerManager
//! Private m_ProgID As String
//! Private m_CLSID As String
//! Private m_Servers As Collection
//! Private m_CurrentServer As String
//!
//! Public Sub Initialize(progID As String, clsID As String)
//!     m_ProgID = progID
//!     m_CLSID = clsID
//!     Set m_Servers = New Collection
//! End Sub
//!
//! Public Sub AddServer(serverName As String)
//!     m_Servers.Add serverName
//! End Sub
//!
//! Public Function FindActiveServer() As String
//!     Dim server As Variant
//!     Dim settings As Long
//!     
//!     For Each server In m_Servers
//!         On Error Resume Next
//!         settings = GetAutoServerSettings(m_ProgID, m_CLSID, CStr(server))
//!         On Error GoTo 0
//!         
//!         If settings <> 0 Then
//!             m_CurrentServer = CStr(server)
//!             FindActiveServer = m_CurrentServer
//!             Exit Function
//!         End If
//!     Next server
//!     
//!     FindActiveServer = ""
//! End Function
//!
//! Public Function GetServerSettings(serverName As String) As Long
//!     GetServerSettings = GetAutoServerSettings(m_ProgID, m_CLSID, serverName)
//! End Function
//!
//! Public Function ValidateAllServers() As Collection
//!     Dim results As New Collection
//!     Dim server As Variant
//!     Dim settings As Long
//!     Dim result As String
//!     
//!     For Each server In m_Servers
//!         settings = GetAutoServerSettings(m_ProgID, m_CLSID, CStr(server))
//!         result = CStr(server) & ":" & CStr(settings)
//!         results.Add result
//!     Next server
//!     
//!     Set ValidateAllServers = results
//! End Function
//! ```
//!
//! ## 2. Failover Connection Handler
//!
//! ```vb
//! Type FailoverConfig
//!     PrimaryServer As String
//!     SecondaryServer As String
//!     TertiaryServer As String
//!     RetryCount As Integer
//!     RetryDelay As Long
//! End Type
//!
//! Function ConnectWithFailover(progID As String, _
//!                              clsID As String, _
//!                              config As FailoverConfig) As Object
//!     Dim servers(1 To 3) As String
//!     Dim i As Integer
//!     Dim attempt As Integer
//!     Dim settings As Long
//!     
//!     servers(1) = config.PrimaryServer
//!     servers(2) = config.SecondaryServer
//!     servers(3) = config.TertiaryServer
//!     
//!     For i = 1 To 3
//!         For attempt = 1 To config.RetryCount
//!             On Error Resume Next
//!             settings = GetAutoServerSettings(progID, clsID, servers(i))
//!             On Error GoTo 0
//!             
//!             If settings <> 0 Then
//!                 Debug.Print "Connected to: " & servers(i)
//!                 Set ConnectWithFailover = CreateObject(progID, servers(i))
//!                 Exit Function
//!             End If
//!             
//!             If attempt < config.RetryCount Then
//!                 Sleep config.RetryDelay
//!             End If
//!         Next attempt
//!     Next i
//!     
//!     Err.Raise vbObjectError + 1001, , "All servers unavailable"
//! End Function
//! ```
//!
//! ## 3. Configuration Cache Manager
//!
//! ```vb
//! Type CachedServerInfo
//!     ServerName As String
//!     Settings As Long
//!     CacheTime As Date
//!     TTL As Long  ' Time to live in seconds
//! End Type
//!
//! Private m_Cache As Collection
//!
//! Sub InitializeCache()
//!     Set m_Cache = New Collection
//! End Sub
//!
//! Function GetCachedServerSettings(progID As String, _
//!                                  clsID As String, _
//!                                  serverName As String, _
//!                                  Optional cacheTTL As Long = 300) As Long
//!     Dim cacheKey As String
//!     Dim cached As CachedServerInfo
//!     Dim i As Long
//!     Dim found As Boolean
//!     
//!     cacheKey = serverName
//!     
//!     ' Check cache
//!     For i = 1 To m_Cache.Count
//!         cached = m_Cache(i)
//!         If cached.ServerName = cacheKey Then
//!             If DateDiff("s", cached.CacheTime, Now) < cached.TTL Then
//!                 GetCachedServerSettings = cached.Settings
//!                 Exit Function
//!             Else
//!                 m_Cache.Remove i
//!                 Exit For
//!             End If
//!         End If
//!     Next i
//!     
//!     ' Not in cache or expired, fetch fresh
//!     cached.ServerName = serverName
//!     cached.Settings = GetAutoServerSettings(progID, clsID, serverName)
//!     cached.CacheTime = Now
//!     cached.TTL = cacheTTL
//!     
//!     m_Cache.Add cached
//!     GetCachedServerSettings = cached.Settings
//! End Function
//! ```
//!
//! ## 4. Server Pool Manager
//!
//! ```vb
//! Type ServerPool
//!     PoolName As String
//!     Servers() As String
//!     ProgID As String
//!     CLSID As String
//! End Type
//!
//! Function GetHealthyServersFromPool(pool As ServerPool) As Collection
//!     Dim healthyServers As New Collection
//!     Dim i As Long
//!     Dim settings As Long
//!     
//!     For i = LBound(pool.Servers) To UBound(pool.Servers)
//!         On Error Resume Next
//!         settings = GetAutoServerSettings(pool.ProgID, pool.CLSID, pool.Servers(i))
//!         On Error GoTo 0
//!         
//!         If settings <> 0 Then
//!             healthyServers.Add pool.Servers(i)
//!         End If
//!     Next i
//!     
//!     Set GetHealthyServersFromPool = healthyServers
//! End Function
//!
//! Function GetPoolStatistics(pool As ServerPool) As String
//!     Dim total As Long
//!     Dim healthy As Long
//!     Dim i As Long
//!     Dim settings As Long
//!     
//!     total = UBound(pool.Servers) - LBound(pool.Servers) + 1
//!     healthy = 0
//!     
//!     For i = LBound(pool.Servers) To UBound(pool.Servers)
//!         settings = GetAutoServerSettings(pool.ProgID, pool.CLSID, pool.Servers(i))
//!         If settings <> 0 Then healthy = healthy + 1
//!     Next i
//!     
//!     GetPoolStatistics = pool.PoolName & ": " & healthy & "/" & total & " servers available"
//! End Function
//! ```
//!
//! # Error Handling
//!
//! ```vb
//! Function SafeGetAutoServerSettings(progID As String, _
//!                                    clsID As String, _
//!                                    serverName As String) As Long
//!     On Error GoTo ErrorHandler
//!     
//!     SafeGetAutoServerSettings = GetAutoServerSettings(progID, clsID, serverName)
//!     Exit Function
//!     
//! ErrorHandler:
//!     Select Case Err.Number
//!         Case 429  ' ActiveX component can't create object
//!             Debug.Print "Server not available: " & serverName
//!         Case 462  ' Remote server machine does not exist
//!             Debug.Print "Machine not found: " & serverName
//!         Case 70   ' Permission denied
//!             Debug.Print "Access denied to server: " & serverName
//!         Case Else
//!             Debug.Print "Error " & Err.Number & ": " & Err.Description
//!     End Select
//!     
//!     SafeGetAutoServerSettings = 0
//! End Function
//! ```
//!
//! Common errors:
//! - **Error 429**: `ActiveX` component can't create object - server not registered or accessible.
//! - **Error 462**: Remote server machine does not exist or is unavailable.
//! - **Error 70**: Permission denied - insufficient `DCOM` permissions.
//! - **Error 5**: Invalid procedure call - invalid `ProgID` or `CLSID` format.
//!
//! # Performance Considerations
//!
//! - Network latency affects remote server queries
//! - Consider caching results for frequently checked servers
//! - Use timeouts for unresponsive servers
//! - Parallel checks may improve performance for multiple servers
//! - DCOM configuration on both client and server affects response time
//! - Firewall settings can cause delays or failures
//!
//! # Best Practices
//!
//! 1. **Always use error handling** - network and `DCOM` issues are common
//! 2. **Validate `ProgID` and `CLSID` format** before calling
//! 3. **Use descriptive server names** for better diagnostics
//! 4. **Implement retry logic** for transient failures
//! 5. **Cache results** to reduce network overhead
//! 6. **Log all queries** for auditing and troubleshooting
//! 7. **Test connectivity** before production deployment
//! 8. **Configure `DCOM` security** appropriately on all servers
//!
//! # Comparison with Other Functions
//!
//! ## `GetAutoServerSettings` vs `CreateObject`
//!
//! ```vb
//! ' GetAutoServerSettings - Query server settings
//! settings = GetAutoServerSettings(progID, clsID, serverName)
//!
//! ' CreateObject - Actually create server instance
//! Set obj = CreateObject(progID, serverName)
//! ```
//!
//! ## `GetAutoServerSettings` vs `GetObject`
//!
//! ```vb
//! ' GetAutoServerSettings - Check DCOM configuration
//! settings = GetAutoServerSettings(progID, clsID, serverName)
//!
//! ' GetObject - Connect to existing instance
//! Set obj = GetObject(, progID)
//! ```
//!
//! # Limitations
//!
//! - Windows-specific functionality (DCOM is Windows-only)
//! - Requires DCOM to be enabled and properly configured
//! - Network connectivity required for remote servers
//! - Security settings may block access
//! - Return value interpretation is not well documented
//! - Limited to COM/DCOM servers
//! - May not work with modern .NET components
//! - Deprecated in favor of newer technologies (WCF, REST APIs)
//!
//! # DCOM Configuration
//!
//! For this function to work properly:
//!
//! 1. **Component Services** (dcomcnfg) must be configured
//! 2. **DCOM permissions** must allow remote access
//! 3. **Firewall rules** must permit DCOM traffic
//! 4. **Authentication level** must be set appropriately
//! 5. **Launch and activation permissions** must be granted
//!
//! # Related Functions
//!
//! - `CreateObject` - Creates an instance of a `COM` object
//! - `GetObject` - Returns a reference to an `ActiveX` object
//! - `CallByName` - Executes methods on objects dynamically
//! - `TypeName` - Returns type information about an object
//! - `GetSetting` - Retrieves application settings from registry

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn getautoserversettings_basic() {
        let source = r#"settings = GetAutoServerSettings("MyServer.Application", "{12345678-1234-1234-1234-123456789012}", "SERVER01")"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_with_variables() {
        let source = r"result = GetAutoServerSettings(progID, clsID, serverName)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_in_if() {
        let source =
            r#"If GetAutoServerSettings(progID, clsID, "SERVER01") <> 0 Then MsgBox "Available""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_in_function() {
        let source = r"Function ValidateServer() As Boolean
    ValidateServer = (GetAutoServerSettings(m_ProgID, m_CLSID, m_Server) <> 0)
End Function";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_assignment() {
        let source = r#"Dim settings As Long
settings = GetAutoServerSettings("App.Server", "{AAAABBBB-CCCC-DDDD-EEEE-FFFF00001111}", "REMOTE-PC")"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_comparison() {
        let source = r#"If GetAutoServerSettings(progID, clsID, server1) = GetAutoServerSettings(progID, clsID, server2) Then
    Debug.Print "Same settings"
End If"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_for_loop() {
        let source = r"For i = 1 To serverCount
    settings = GetAutoServerSettings(progID, clsID, servers(i))
Next i";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_select_case() {
        let source = r#"Select Case GetAutoServerSettings(progID, clsID, serverName)
    Case Is > 0
        Debug.Print "Configured"
End Select"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_error_handling() {
        let source = r"On Error GoTo ErrorHandler
settings = GetAutoServerSettings(progID, clsID, serverName)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_debug_print() {
        let source =
            r#"Debug.Print "Settings: " & GetAutoServerSettings(progID, clsID, serverName)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_array_element() {
        let source = r"settings(i) = GetAutoServerSettings(progID, clsID, servers(i))";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_class_member() {
        let source = r"m_Settings = GetAutoServerSettings(m_ProgID, m_CLSID, m_ServerName)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_type_field() {
        let source = r"serverInfo.Settings = GetAutoServerSettings(progID, clsID, serverInfo.Name)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_do_loop() {
        let source = r"Do While retry < maxRetries
    settings = GetAutoServerSettings(progID, clsID, serverName)
    If settings <> 0 Then Exit Do
Loop";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_local_server() {
        let source = r#"localSettings = GetAutoServerSettings(progID, clsID, ".")"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_concatenation() {
        let source =
            r#"result = "Settings: " & CStr(GetAutoServerSettings(progID, clsID, serverName))"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_msgbox() {
        let source = r#"MsgBox "Settings: " & GetAutoServerSettings(progID, clsID, serverName)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_collection() {
        let source = r"results.Add GetAutoServerSettings(progID, clsID, serverName)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_file_output() {
        let source = r#"Print #fileNum, "Server: " & serverName & " Settings: " & GetAutoServerSettings(progID, clsID, serverName)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_iif() {
        let source = r#"status = IIf(GetAutoServerSettings(progID, clsID, serverName) <> 0, "Online", "Offline")"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_with_statement() {
        let source = r"With serverConfig
    .Settings = GetAutoServerSettings(.ProgID, .CLSID, .ServerName)
End With";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_on_error_resume() {
        let source = r"On Error Resume Next
settings = GetAutoServerSettings(progID, clsID, serverName)
On Error GoTo 0";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_min_comparison() {
        let source = r"If GetAutoServerSettings(progID, clsID, serverName) < minSettings Then
    minSettings = GetAutoServerSettings(progID, clsID, serverName)
End If";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_for_each() {
        let source = r"For Each server In servers
    settings = GetAutoServerSettings(progID, clsID, CStr(server))
Next server";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_property() {
        let source = r"Property Get ServerSettings() As Long
    ServerSettings = GetAutoServerSettings(m_ProgID, m_CLSID, m_ServerName)
End Property";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_exit_condition() {
        let source = r"If GetAutoServerSettings(progID, clsID, serverName) = 0 Then Exit Function";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getautoserversettings_listbox() {
        let source = r#"lstServers.AddItem serverName & " - " & GetAutoServerSettings(progID, clsID, serverName)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/environment/getautoserversettings",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}