vb6parse 1.0.0

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
//! # `LoadResData` Function
//!
//! Returns the data stored in a resource (.res) file.
//!
//! ## Syntax
//!
//! ```vb
//! LoadResData(index, format)
//! ```
//!
//! ## Parameters
//!
//! - `index` (Required): `Integer` or `String` identifying the resource
//!   - Can be a numeric ID or string name
//!   - Must match the ID/name used when the resource was compiled
//! - `format` (Required): `Integer` specifying the format of the resource data
//!   - Custom user-defined format number (1-32767)
//!   - Cannot use predefined formats (use `LoadResPicture`/`LoadResString` instead)
//!
//! ## Return Value
//!
//! Returns a Byte array (Variant containing Byte array):
//! - Contains the raw binary data from the resource file
//! - Array is zero-based
//! - Returns Empty if resource not found (in some VB versions)
//! - May raise error 32813 if resource not found
//! - Data is returned exactly as stored in .res file
//!
//! ## Remarks
//!
//! The `LoadResData` function loads custom binary data from resources:
//!
//! - Loads custom data from compiled resource (.res) files
//! - Resource file must be linked to project at compile time
//! - Returns data as Byte array (`Variant`/`Byte()`)
//! - Used for custom binary resources (not bitmaps/icons/strings)
//! - For images, use `LoadResPicture` instead
//! - For strings, use `LoadResString` instead
//! - Resource files created with Resource Editor or RC.EXE
//! - Index can be numeric ID or string name
//! - Format must be custom type (not 1=Cursor, 2=Bitmap, 3=Icon, etc.)
//! - Useful for embedding files (sounds, data, etc.)
//! - Data embedded in compiled EXE (no external files needed)
//! - More secure than external files (can't be easily modified)
//! - Faster access than loading from disk
//! - Resource file added via Project > Add File or in .vbp
//! - Only one resource file per project
//! - Changes to .res file require recompile
//! - Error 32813: "Resource not found" if ID/format don't match
//! - Error 48: "Error loading from file" if resource file corrupt
//!
//! ## Typical Uses
//!
//! 1. **Load Binary Data**
//!    ```vb
//!    Dim data() As Byte
//!    data = LoadResData(101, 256) ' Custom format 256
//!    ```
//!
//! 2. **Load Sound File**
//!    ```vb
//!    Dim wavData() As Byte
//!    wavData = LoadResData("STARTUP_SOUND", 257)
//!    ```
//!
//! 3. **Load Configuration Data**
//!    ```vb
//!    Dim configData() As Byte
//!    configData = LoadResData("CONFIG", 300)
//!    ```
//!
//! 4. **Load Binary Template**
//!    ```vb
//!    Dim template() As Byte
//!    template = LoadResData(1001, 400)
//!    ```
//!
//! 5. **Load Custom File Type**
//!    ```vb
//!    Dim xmlData() As Byte
//!    xmlData = LoadResData("SCHEMA", 500)
//!    ```
//!
//! 6. **Load Multiple Resources**
//!    ```vb
//!    For i = 1 To 5
//!        data = LoadResData(i, 256)
//!        ProcessData data
//!    Next i
//!    ```
//!
//! 7. **Load Named Resource**
//!    ```vb
//!    Dim helpData() As Byte
//!    helpData = LoadResData("HELP_FILE", 600)
//!    ```
//!
//! 8. **Conditional Loading**
//!    ```vb
//!    If useCustomTheme Then
//!        themeData = LoadResData("DARK_THEME", 700)
//!    End If
//!    ```
//!
//! ## Basic Examples
//!
//! ### Example 1: Loading and Using Binary Data
//! ```vb
//! ' Load binary data from resource
//! Dim resourceData() As Byte
//! resourceData = LoadResData(101, 256)
//!
//! ' Use the data
//! Dim fileNum As Integer
//! fileNum = FreeFile
//! Open "C:\output.dat" For Binary As #fileNum
//! Put #fileNum, , resourceData
//! Close #fileNum
//! ```
//!
//! ### Example 2: Loading Sound Data
//! ```vb
//! ' Load WAV file from resources
//! Dim wavData() As Byte
//! wavData = LoadResData("BEEP", 257)
//!
//! ' Play using Windows API
//! Call sndPlaySound(wavData(0), SND_ASYNC Or SND_MEMORY)
//! ```
//!
//! ### Example 3: Error Handling
//! ```vb
//! On Error Resume Next
//! Dim data() As Byte
//! data = LoadResData(999, 256)
//! If Err.Number = 32813 Then
//!     MsgBox "Resource not found!", vbCritical
//!     Err.Clear
//! ElseIf Err.Number <> 0 Then
//!     MsgBox "Error loading resource: " & Err.Description, vbCritical
//!     Err.Clear
//! End If
//! ```
//!
//! ### Example 4: Converting to String
//! ```vb
//! ' Load text data stored as binary
//! Dim textData() As Byte
//! textData = LoadResData("README", 300)
//!
//! ' Convert byte array to string
//! Dim content As String
//! content = StrConv(textData, vbUnicode)
//! MsgBox content
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `SafeLoadResData`
//! ```vb
//! Function SafeLoadResData(ByVal resID As Variant, _
//!                          ByVal resFormat As Integer, _
//!                          ByRef outData() As Byte) As Boolean
//!     On Error Resume Next
//!     outData = LoadResData(resID, resFormat)
//!     SafeLoadResData = (Err.Number = 0)
//!     Err.Clear
//! End Function
//! ```
//!
//! ### Pattern 2: `LoadResDataToFile`
//! ```vb
//! Sub LoadResDataToFile(ByVal resID As Variant, _
//!                       ByVal resFormat As Integer, _
//!                       ByVal filename As String)
//!     Dim data() As Byte
//!     Dim fileNum As Integer
//!     
//!     data = LoadResData(resID, resFormat)
//!     
//!     fileNum = FreeFile
//!     Open filename For Binary As #fileNum
//!     Put #fileNum, , data
//!     Close #fileNum
//! End Sub
//! ```
//!
//! ### Pattern 3: `GetResDataSize`
//! ```vb
//! Function GetResDataSize(ByVal resID As Variant, _
//!                         ByVal resFormat As Integer) As Long
//!     On Error Resume Next
//!     Dim data() As Byte
//!     data = LoadResData(resID, resFormat)
//!     
//!     If Err.Number = 0 Then
//!         GetResDataSize = UBound(data) + 1
//!     Else
//!         GetResDataSize = 0
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 4: `LoadResDataAsString`
//! ```vb
//! Function LoadResDataAsString(ByVal resID As Variant, _
//!                              ByVal resFormat As Integer) As String
//!     Dim data() As Byte
//!     data = LoadResData(resID, resFormat)
//!     LoadResDataAsString = StrConv(data, vbUnicode)
//! End Function
//! ```
//!
//! ### Pattern 5: `ResDataExists`
//! ```vb
//! Function ResDataExists(ByVal resID As Variant, _
//!                        ByVal resFormat As Integer) As Boolean
//!     On Error Resume Next
//!     Dim data() As Byte
//!     data = LoadResData(resID, resFormat)
//!     ResDataExists = (Err.Number = 0)
//!     Err.Clear
//! End Function
//! ```
//!
//! ### Pattern 6: `LoadMultipleResources`
//! ```vb
//! Function LoadMultipleResources(startID As Integer, _
//!                                endID As Integer, _
//!                                resFormat As Integer) As Collection
//!     Dim col As New Collection
//!     Dim i As Integer
//!     Dim data() As Byte
//!     
//!     On Error Resume Next
//!     For i = startID To endID
//!         data = LoadResData(i, resFormat)
//!         If Err.Number = 0 Then
//!             col.Add data
//!         End If
//!         Err.Clear
//!     Next i
//!     
//!     Set LoadMultipleResources = col
//! End Function
//! ```
//!
//! ### Pattern 7: `CompareResData`
//! ```vb
//! Function CompareResData(ByVal resID1 As Variant, _
//!                         ByVal resID2 As Variant, _
//!                         ByVal resFormat As Integer) As Boolean
//!     Dim data1() As Byte, data2() As Byte
//!     Dim i As Long
//!     
//!     data1 = LoadResData(resID1, resFormat)
//!     data2 = LoadResData(resID2, resFormat)
//!     
//!     If UBound(data1) <> UBound(data2) Then
//!         CompareResData = False
//!         Exit Function
//!     End If
//!     
//!     For i = 0 To UBound(data1)
//!         If data1(i) <> data2(i) Then
//!             CompareResData = False
//!             Exit Function
//!         End If
//!     Next i
//!     
//!     CompareResData = True
//! End Function
//! ```
//!
//! ### Pattern 8: `CopyResDataToArray`
//! ```vb
//! Sub CopyResDataToArray(ByVal resID As Variant, _
//!                        ByVal resFormat As Integer, _
//!                        ByRef targetArray() As Byte)
//!     Dim source() As Byte
//!     Dim i As Long
//!     
//!     source = LoadResData(resID, resFormat)
//!     ReDim targetArray(LBound(source) To UBound(source))
//!     
//!     For i = LBound(source) To UBound(source)
//!         targetArray(i) = source(i)
//!     Next i
//! End Sub
//! ```
//!
//! ### Pattern 9: `LoadResDataWithFallback`
//! ```vb
//! Function LoadResDataWithFallback(ByVal primaryID As Variant, _
//!                                  ByVal fallbackID As Variant, _
//!                                  ByVal resFormat As Integer) As Byte()
//!     On Error Resume Next
//!     
//!     LoadResDataWithFallback = LoadResData(primaryID, resFormat)
//!     If Err.Number <> 0 Then
//!         Err.Clear
//!         LoadResDataWithFallback = LoadResData(fallbackID, resFormat)
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 10: `CachedResDataLoader`
//! ```vb
//! Dim resCache As New Collection
//!
//! Function LoadResDataCached(ByVal resID As Variant, _
//!                            ByVal resFormat As Integer) As Byte()
//!     Dim key As String
//!     On Error Resume Next
//!     
//!     key = CStr(resID) & "_" & CStr(resFormat)
//!     
//!     ' Try cache first
//!     LoadResDataCached = resCache(key)
//!     If Err.Number <> 0 Then
//!         ' Not in cache, load and cache it
//!         Err.Clear
//!         LoadResDataCached = LoadResData(resID, resFormat)
//!         resCache.Add LoadResDataCached, key
//!     End If
//! End Function
//! ```
//!
//! ## Advanced Examples
//!
//! ### Example 1: Resource Manager Class
//! ```vb
//! ' Class: ResourceManager
//! Private m_cache As Collection
//!
//! Private Sub Class_Initialize()
//!     Set m_cache = New Collection
//! End Sub
//!
//! Public Function LoadData(ByVal resID As Variant, _
//!                          ByVal resFormat As Integer) As Byte()
//!     Dim key As String
//!     On Error Resume Next
//!     
//!     key = CStr(resID) & "_" & CStr(resFormat)
//!     LoadData = m_cache(key)
//!     
//!     If Err.Number <> 0 Then
//!         Err.Clear
//!         LoadData = LoadResData(resID, resFormat)
//!         If Err.Number = 0 Then
//!             m_cache.Add LoadData, key
//!         Else
//!             Err.Raise vbObjectError + 1000, "ResourceManager", _
//!                       "Failed to load resource"
//!         End If
//!     End If
//! End Function
//!
//! Public Function GetAsString(ByVal resID As Variant, _
//!                             ByVal resFormat As Integer) As String
//!     Dim data() As Byte
//!     data = LoadData(resID, resFormat)
//!     GetAsString = StrConv(data, vbUnicode)
//! End Function
//!
//! Public Sub SaveToFile(ByVal resID As Variant, _
//!                       ByVal resFormat As Integer, _
//!                       ByVal filename As String)
//!     Dim data() As Byte
//!     Dim fileNum As Integer
//!     
//!     data = LoadData(resID, resFormat)
//!     fileNum = FreeFile
//!     Open filename For Binary As #fileNum
//!     Put #fileNum, , data
//!     Close #fileNum
//! End Sub
//!
//! Public Sub ClearCache()
//!     Set m_cache = New Collection
//! End Sub
//!
//! Private Sub Class_Terminate()
//!     Set m_cache = Nothing
//! End Sub
//! ```
//!
//! ### Example 2: Sound Player with Resources
//! ```vb
//! ' Module with API declarations
//! Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" ( _
//!     lpszSoundName As Any, ByVal uFlags As Long) As Long
//!
//! Private Const SND_ASYNC = &H1
//! Private Const SND_MEMORY = &H4
//!
//! ' Sound resource IDs
//! Private Const RES_SOUND_BEEP = 101
//! Private Const RES_SOUND_CLICK = 102
//! Private Const RES_SOUND_ERROR = 103
//! Private Const RES_FORMAT_WAVE = 257
//!
//! Public Sub PlayResourceSound(ByVal soundID As Integer)
//!     Dim soundData() As Byte
//!     On Error Resume Next
//!     
//!     soundData = LoadResData(soundID, RES_FORMAT_WAVE)
//!     If Err.Number = 0 Then
//!         Call sndPlaySound(soundData(0), SND_ASYNC Or SND_MEMORY)
//!     Else
//!         Debug.Print "Sound not found: " & soundID
//!     End If
//! End Sub
//!
//! Public Sub PlayBeep()
//!     PlayResourceSound RES_SOUND_BEEP
//! End Sub
//!
//! Public Sub PlayClick()
//!     PlayResourceSound RES_SOUND_CLICK
//! End Sub
//!
//! Public Sub PlayError()
//!     PlayResourceSound RES_SOUND_ERROR
//! End Sub
//! ```
//!
//! ### Example 3: Configuration Manager
//! ```vb
//! ' Class: ConfigManager
//! Private m_config As String
//!
//! Public Sub LoadConfig()
//!     Dim configData() As Byte
//!     On Error Resume Next
//!     
//!     ' Try to load from resource
//!     configData = LoadResData("CONFIG", 300)
//!     If Err.Number = 0 Then
//!         m_config = StrConv(configData, vbUnicode)
//!     Else
//!         ' Use default config
//!         m_config = GetDefaultConfig()
//!     End If
//!     Err.Clear
//! End Sub
//!
//! Public Function GetSetting(ByVal key As String) As String
//!     Dim lines() As String
//!     Dim i As Long
//!     Dim pos As Long
//!     
//!     lines = Split(m_config, vbCrLf)
//!     For i = 0 To UBound(lines)
//!         If Left(lines(i), Len(key) + 1) = key & "=" Then
//!             GetSetting = Mid(lines(i), Len(key) + 2)
//!             Exit Function
//!         End If
//!     Next i
//! End Function
//!
//! Private Function GetDefaultConfig() As String
//!     GetDefaultConfig = "Version=1.0" & vbCrLf & _
//!                       "Language=English" & vbCrLf & _
//!                       "Theme=Default"
//! End Function
//! ```
//!
//! ### Example 4: Template Engine
//! ```vb
//! ' Class: TemplateEngine
//! Private Const RES_FORMAT_TEMPLATE = 400
//!
//! Public Function ProcessTemplate(ByVal templateID As Variant, _
//!                                  ParamArray values()) As String
//!     Dim template As String
//!     Dim data() As Byte
//!     Dim i As Long
//!     Dim result As String
//!     
//!     ' Load template from resources
//!     data = LoadResData(templateID, RES_FORMAT_TEMPLATE)
//!     template = StrConv(data, vbUnicode)
//!     
//!     result = template
//!     
//!     ' Replace placeholders with values
//!     For i = LBound(values) To UBound(values)
//!         result = Replace(result, "{" & i & "}", CStr(values(i)))
//!     Next i
//!     
//!     ProcessTemplate = result
//! End Function
//!
//! Public Function LoadTemplate(ByVal templateID As Variant) As String
//!     Dim data() As Byte
//!     data = LoadResData(templateID, RES_FORMAT_TEMPLATE)
//!     LoadTemplate = StrConv(data, vbUnicode)
//! End Function
//!
//! Public Function HasTemplate(ByVal templateID As Variant) As Boolean
//!     On Error Resume Next
//!     Dim data() As Byte
//!     data = LoadResData(templateID, RES_FORMAT_TEMPLATE)
//!     HasTemplate = (Err.Number = 0)
//!     Err.Clear
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! ' Error 32813: Resource not found
//! On Error Resume Next
//! data = LoadResData(999, 256)
//! If Err.Number = 32813 Then
//!     MsgBox "Resource ID 999 not found!"
//! End If
//!
//! ' Error 48: Error loading from file
//! data = LoadResData(101, 256)
//! If Err.Number = 48 Then
//!     MsgBox "Resource file is corrupt or missing!"
//! End If
//!
//! ' Safe loading with error handling
//! Function TryLoadResData(ByVal resID As Variant, _
//!                         ByVal resFormat As Integer, _
//!                         ByRef outData() As Byte) As Boolean
//!     On Error Resume Next
//!     outData = LoadResData(resID, resFormat)
//!     TryLoadResData = (Err.Number = 0)
//!     If Err.Number <> 0 Then
//!         Debug.Print "Error loading resource: " & Err.Description
//!     End If
//!     Err.Clear
//! End Function
//! ```
//!
//! ## Performance Considerations
//!
//! - **Fast Access**: Resources are embedded in EXE (very fast loading)
//! - **Memory Overhead**: Data loaded into memory when accessed
//! - **No Caching**: Each call loads fresh copy (implement caching if needed)
//! - **Compile Time**: Large resources increase EXE size and compile time
//! - **One Resource File**: Only one .res file per project (combine all resources)
//! - **Array Copy**: Returns copy of data (not reference)
//!
//! ## Best Practices
//!
//! 1. **Always handle errors** - resource might not exist
//! 2. **Use constants** for resource IDs and formats
//! 3. **Cache frequently used data** to avoid repeated loading
//! 4. **Use meaningful names** for string-based resource IDs
//! 5. **Document resource IDs** in code or separate file
//! 6. **Keep resources organized** by format number
//! 7. **Test resource loading** during development
//! 8. **Validate data size** after loading if needed
//! 9. **Consider memory usage** for large resources
//! 10. **Use appropriate formats** - `LoadResPicture` for images, `LoadResString` for text
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Return Type | Data Type |
//! |----------|---------|-------------|-----------|
//! | **`LoadResData`** | Load custom binary data | Byte array | Any binary data |
//! | **`LoadResPicture`** | Load image from resources | `StdPicture` | BMP, ICO, CUR |
//! | **`LoadResString`** | Load string from resources | String | Text strings |
//! | **`LoadPicture`** | Load image from file | `StdPicture` | External file |
//!
//! ## `LoadResData` vs `LoadResPicture` vs `LoadResString`
//!
//! ```vb
//! ' LoadResData - custom binary data
//! Dim binData() As Byte
//! binData = LoadResData(101, 256)
//!
//! ' LoadResPicture - images
//! Picture1.Picture = LoadResPicture(101, vbResBitmap)
//!
//! ' LoadResString - strings
//! Dim msg As String
//! msg = LoadResString(101)
//! ```
//!
//! **When to use each:**
//! - **`LoadResData`**: Custom binary files, sound files, configuration data
//! - **`LoadResPicture`**: Images (bitmaps, icons, cursors)
//! - **`LoadResString`**: Localized text strings
//!
//! ## Platform Notes
//!
//! - Available in VB6 (not in early VB versions)
//! - Requires resource file (.res) linked to project
//! - Resource file created with Resource Editor or RC.EXE
//! - Only one resource file per project
//! - Resources embedded in compiled EXE/DLL
//! - Returns Variant containing Byte array
//! - Array is always zero-based (0 to n-1)
//! - Custom formats: 1-32767 (avoid predefined formats)
//! - Maximum resource size limited by available memory
//!
//! ## Limitations
//!
//! - **One resource file**: Only one .res file per project
//! - **Compile time**: Must recompile to update resources
//! - **No modification**: Cannot modify resources at runtime
//! - **Limited tools**: VB6 Resource Editor is basic
//! - **Format restrictions**: Cannot use predefined formats (1-16)
//! - **No compression**: Resources stored uncompressed in EXE
//! - **No encryption**: Resources easily extractable from EXE
//! - **Memory copy**: Always returns copy of data (not reference)
//! - **Error messages**: Limited error information
//! - **No streaming**: Entire resource loaded into memory
//!
//! ## Related Functions
//!
//! - `LoadResPicture`: Load picture from resource file
//! - `LoadResString`: Load string from resource file
//! - `LoadPicture`: Load picture from external file
//! - `StrConv`: Convert byte array to string
//! - `FreeFile`: Get file number for saving resource data

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

    #[test]
    fn loadresdata_basic() {
        let source = r"
            Dim data() As Byte
            data = LoadResData(101, 256)
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_string_id() {
        let source = r#"
            data = LoadResData("SOUND", 257)
        "#;
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_if_statement() {
        let source = r"
            If hasResource Then
                data = LoadResData(resID, resFormat)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_function_return() {
        let source = r"
            Function GetResourceData() As Byte()
                GetResourceData = LoadResData(101, 256)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_for_loop() {
        let source = r"
            For i = 1 To 10
                resData = LoadResData(i, 256)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_error_handling() {
        let source = r#"
            On Error Resume Next
            data = LoadResData(999, 256)
            If Err.Number = 32813 Then
                MsgBox "Resource not found"
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_with_statement() {
        let source = r"
            With resourceManager
                .data = LoadResData(101, 256)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_array_assignment() {
        let source = r"
            Dim resources(1 To 5) As Variant
            resources(i) = LoadResData(i, 256)
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_select_case() {
        let source = r"
            Select Case resourceType
                Case 1
                    data = LoadResData(101, 256)
                Case 2
                    data = LoadResData(102, 257)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_elseif() {
        let source = r"
            If mode = 1 Then
                data = LoadResData(101, 256)
            ElseIf mode = 2 Then
                data = LoadResData(102, 256)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_strconv() {
        let source = r#"
            Dim textData As String
            textData = StrConv(LoadResData("TEXT", 300), vbUnicode)
        "#;
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_parentheses() {
        let source = r"
            data = (LoadResData(101, 256))
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_iif() {
        let source = r"
            data = IIf(useCustom, LoadResData(101, 256), LoadResData(1, 256))
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_in_class() {
        let source = r#"
            Private Sub Class_Initialize()
                m_data = LoadResData("DEFAULT", 256)
            End Sub
        "#;
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_function_argument() {
        let source = r"
            Call ProcessData(LoadResData(101, 256))
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_property_assignment() {
        let source = r"
            MyObject.ResourceData = LoadResData(101, 256)
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_ubound() {
        let source = r"
            Dim size As Long
            size = UBound(LoadResData(101, 256)) + 1
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_while_wend() {
        let source = r"
            While index < maxResources
                data = LoadResData(index, 256)
                index = index + 1
            Wend
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_do_while() {
        let source = r"
            Do While hasMore
                currentData = LoadResData(GetNextID(), format)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_do_until() {
        let source = r"
            Do Until loaded
                On Error Resume Next
                data = LoadResData(resID, format)
                loaded = (Err.Number = 0)
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_constants() {
        let source = r"
            Const RES_FORMAT_WAVE = 257
            data = LoadResData(101, RES_FORMAT_WAVE)
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_file_write() {
        let source = r"
            Dim fileNum As Integer
            fileNum = FreeFile
            Put #fileNum, , LoadResData(101, 256)
        ";
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_concatenation() {
        let source = r#"
            Dim id As String
            id = "RES_" & resNum
            data = LoadResData(id, 256)
        "#;
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_collection_add() {
        let source = r#"
            resources.Add LoadResData(i, 256), "Resource" & 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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_debug_print() {
        let source = r#"
            Debug.Print "Size: " & UBound(LoadResData(101, 256)) + 1
        "#;
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_comparison() {
        let source = r#"
            If LoadResData(101, 256)(0) = &H4D Then
                MsgBox "Valid header"
            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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresdata_msgbox() {
        let source = r#"
            MsgBox "Loaded " & UBound(LoadResData(101, 256)) + 1 & " bytes"
        "#;
        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/resources/loadresdata",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}