forked from WNKLER/RefTypes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefTypes.bas
More file actions
2133 lines (1958 loc) · 62.7 KB
/
RefTypes.bas
File metadata and controls
2133 lines (1958 loc) · 62.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Attribute VB_Name = "RefTypes"
''Attribute VB_Name = "RefTypes"
''================================================================================================================================'
'' RefTypes '
''-------------------------------------------------------- '
'' https://github.com/WNKLER/RefTypes '
''-------------------------------------------------------- '
'' A VBA/VB6 Library for reading/writing intrinsic types at arbitrary memory addresses. '
'' Its defining feature is that this is achieved using truly native, built-in language features. '
'' It uses no API declarations and has no external dependencies. '
''================================================================================================================================'
'' MIT License '
'' '
'' Copyright (c) 2025 Benjamin Dovidio (WNKLER) '
'' Edited by Alexey Leonov (testuser2) 08.2025
'Option Private Module
Option Explicit
#Const SafeMode = True
'#Const PreInitMode = True
Enum Context
#If Win64 Then
[_] '// 0 on x64; undefined on x86.
#End If
Win64 '// 0 when [_] is undefined; otherwise, [_] + 1.
ptrSz = 4 + (4 * Win64)
varSz = 8 + (2 * ptrSz)
End Enum
Private Const FADF_AUTO As Integer = &H1
Private Const FADF_FIXEDSIZE As Integer = &H10
#If VBA7 = 0 Then
Private Enum LONG_PTR: [_]: End Enum
Public Enum LongPtr: [_]: End Enum '// Must be Public for Enum-typed Public Property
#End If
#If Not Win64 Then
Public Type LongLong
L0x0 As Long
L0x4 As Long
End Type
#End If
Private Type HalfPtr
HalfPtr(-Win64 To 0) As Integer
End Type
Private Type Initializer
Initializer(-1 To 0) As HalfPtr
End Type
'Private Type Vector '// Declare Static Fields individually. This avoids defining
' Element() As Any '// a separate Vector UDT for each `Element()` Type.
' Descriptor As SA1D
'End Type
Public Type SA1D '(SAFEARRAY1D)
Dims As Integer
Features As Integer
cbElem As Long
Locks As Long
#If Win64 Then
padding As Long
#End If
pData As LongPtr
Count As Long
lBound As Long
End Type
Private Type B3
b1 As Byte
b2 As Byte
B3 As Byte
End Type
Private Type lpVariant
vt As Integer
iunuse As Integer
lunuse As Long
val As LongPtr
lpunuse As LongPtr
End Type
Private Type sVariant
vt As Integer
iunuse As Integer
lunuse As Long
val As String
lpunuse As LongPtr
End Type
Private Type dVariant
vt As Integer
iunuse As Integer
lunuse As Long
val As Double
#If Win64 Then
lpunuse As LongPtr
#End If
End Type
Private Type dtVariant
vt As Integer
iunuse As Integer
lunuse As Long
val As Date
#If Win64 Then
lpunuse As LongPtr
#End If
End Type
Private Type iVariant
vt As Integer
iunuse As Integer
lunuse As Long
val As Integer
#If Win64 Then
lppadding As LongPtr
#End If
End Type
Public Enum SortOrder
Descending = -1
Ascending = 1
End Enum
'##################################################################'
' <Memory proxied by `Initializer`>
Public lpRef() As LongPtr, lpRef_SA As SA1D
Public lpRef2() As LongPtr, lpRef2_SA As SA1D
Public iRef() As Integer, iRef_SA As SA1D
Public iRef2() As Integer, iRef2_SA As SA1D
Public lRef() As Long, lRef_SA As SA1D
Public lRef2() As Long, lRef2_SA As SA1D
Public snRef() As Single, snRef_SA As SA1D
Public dRef() As Double, dRef_SA As SA1D
Public cRef() As Currency, cRef_SA As SA1D
Public cRef2() As Currency, cRef2_SA As SA1D
Public dtRef() As Date, dtRef_SA As SA1D
Public sRef() As String, sRef_SA As SA1D
Public sRef2() As String, sRef2_SA As SA1D
Public oRef() As Object, oRef_SA As SA1D
Public blRef() As Boolean, blRef_SA As SA1D
Public vRef() As Variant, vRef_SA As SA1D
'Union tVariant{
Public vlpRef() As lpVariant
Public vdRef() As dVariant
Public vdtRef() As dtVariant
Public viRef() As iVariant
Public vsRef() As sVariant, pvsRef As LongPtr '.Locks = 1 does not work, pvsRef = ArrPtr(vsRef)
'End Union
Public vRef2() As Variant, vRef2_SA As SA1D
Public unkRef() As IUnknown, unkRef_SA As SA1D
Public bRef() As Byte, bRef_SA As SA1D
Public bRef2() As Byte, bRef2_SA As SA1D
Public llRef() As LongLong, llRef_SA As SA1D
Public iMap1() As Integer, iMap1_SA As SA1D 'ìàïïåðû ñòðîê (ñ èíäåêñàöèåé îò 1)
Public iMap2() As Integer, iMap2_SA As SA1D
Public bMap1() As Byte, bMap1_SA As SA1D
Public bMap2() As Byte, bMap2_SA As SA1D
Public b3Ref1() As B3, b3Ref1_SA As SA1D
Public b3Ref2() As B3, b3Ref2_SA As SA1D '26
Public saRef() As SA1D, saRef_SA As SA1D '27
' <End of proxied memory block>
'##################################################################'
Private iMapDyn_SA As SA1D, bMapDyn_SA As SA1D
Public IsInitialized As Boolean, islpRefInit As Boolean
Sub MakeRef(Descriptor As SA1D, ByVal ptRef As LongPtr, ByVal cbElem As Long)
Static This As Initializer '// Proxy for `Init_Element()`
Static Init_Element() As LongPtr '// Static Init As Vector
Static Init_Descriptor As SA1D
With Init_Descriptor
If .Locks Then
Else
InitInitializer This.Initializer '// Point `Init_Element()` to `Init_Descriptor`
.Count = 1
.Features = FADF_AUTO Or FADF_FIXEDSIZE
.Dims = 1
.Locks = 1
.cbElem = ptrSz
End If
.pData = ptRef
Init_Element(0) = VarPtr(Descriptor)
End With
Descriptor = Init_Descriptor
Descriptor.cbElem = cbElem
Descriptor.pData = 0
End Sub
Private Sub InitInitializer(ByRef Initializer() As LONG_PTR)
Const First As Long = -1
Const Last As Long = -0
Initializer(Last) = VarPtr(Initializer(First)) + (2 * ptrSz)
End Sub
Sub Initialize()
Dim pArr As LongPtr
If IsInitialized Then Exit Sub
MakeRef lpRef_SA, VarPtr(lpRef_SA) - ptrSz, ptrSz
islpRefInit = True
MakeRef lpRef2_SA, VarPtr(lpRef2_SA) - ptrSz, ptrSz
' lpRef2 = RefPtr(lpRef2_SA)
MakeRef bRef_SA, VarPtr(bRef_SA) - ptrSz, 1
MakeRef bRef2_SA, VarPtr(bRef2_SA) - ptrSz, 1
MakeRef iRef_SA, VarPtr(iRef_SA) - ptrSz, 2
MakeRef iRef2_SA, VarPtr(iRef2_SA) - ptrSz, 2
MakeRef blRef_SA, VarPtr(blRef_SA) - ptrSz, 2
MakeRef lRef_SA, VarPtr(lRef_SA) - ptrSz, 4
MakeRef lRef2_SA, VarPtr(lRef2_SA) - ptrSz, 4
MakeRef cRef_SA, VarPtr(cRef_SA) - ptrSz, 8
MakeRef cRef2_SA, VarPtr(cRef2_SA) - ptrSz, 8
MakeRef snRef_SA, VarPtr(snRef_SA) - ptrSz, 4
MakeRef dRef_SA, VarPtr(dRef_SA) - ptrSz, 8
MakeRef dtRef_SA, VarPtr(dtRef_SA) - ptrSz, 8
MakeRef sRef_SA, VarPtr(sRef_SA) - ptrSz, ptrSz
MakeRef sRef2_SA, VarPtr(sRef2_SA) - ptrSz, ptrSz
MakeRef vRef_SA, VarPtr(vRef_SA) - ptrSz, varSz 'vRef()
pArr = VarPtr(vRef_SA) + LenB(vRef_SA)
PutPtr(pArr) = VarPtr(vRef_SA) 'vlpRef()
pArr = pArr + ptrSz
PutPtr(pArr) = VarPtr(vRef_SA) 'vdRef()
pArr = pArr + ptrSz
PutPtr(pArr) = VarPtr(vRef_SA) 'vdtRef()
pArr = pArr + ptrSz
PutPtr(pArr) = VarPtr(vRef_SA) 'viRef()
pvsRef = pArr + ptrSz
MakeRef vRef2_SA, VarPtr(vRef2_SA) - ptrSz, varSz
MakeRef oRef_SA, VarPtr(oRef_SA) - ptrSz, ptrSz
MakeRef unkRef_SA, VarPtr(unkRef_SA) - ptrSz, ptrSz
MakeRef llRef_SA, VarPtr(llRef_SA) - ptrSz, 8
MakeRef iMap1_SA, VarPtr(iMap1_SA) - ptrSz, 2: iMap1_SA.lBound = 1 'ìàïïåðû ñòðîê
MakeRef iMap2_SA, VarPtr(iMap2_SA) - ptrSz, 2: iMap2_SA.lBound = 1
MakeRef bMap1_SA, VarPtr(bMap1_SA) - ptrSz, 1: bMap1_SA.lBound = 1
MakeRef bMap2_SA, VarPtr(bMap2_SA) - ptrSz, 1: bMap2_SA.lBound = 1
MakeRef b3Ref1_SA, VarPtr(b3Ref1_SA) - ptrSz, 3 'ññûëêà 3-áàéòíîãî òèïà
MakeRef b3Ref2_SA, VarPtr(b3Ref2_SA) - ptrSz, 3
MakeRef saRef_SA, VarPtr(saRef_SA) - ptrSz, LenB(saRef_SA) 'ññûëêà íà ñòðóêòóðó SafeArray
iMapDyn_SA = iRef_SA: iMapDyn_SA.Locks = 0: iMapDyn_SA.Features = 128
bMapDyn_SA = bRef_SA: bMapDyn_SA.Locks = 0: bMapDyn_SA.Features = 128
IsInitialized = True
End Sub
Property Get GetInt(ByVal Target As LongPtr) As Integer
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
iRef_SA.pData = Target
GetInt = iRef(0)
End Property
Property Let PutInt(ByVal Target As LongPtr, ByVal PutInt As Integer)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
iRef_SA.pData = Target
iRef(0) = PutInt
End Property
Property Get GetLng(ByVal Target As LongPtr) As Long
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lRef_SA.pData = Target
GetLng = lRef(0)
End Property
Property Let PutLng(ByVal Target As LongPtr, ByVal PutLng As Long)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lRef_SA.pData = Target
lRef(0) = PutLng
End Property
Property Get GetSng(ByVal Target As LongPtr) As Single
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
snRef_SA.pData = Target
GetSng = snRef(0)
End Property
Property Let PutSng(ByVal Target As LongPtr, ByVal PutSng As Single)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
snRef_SA.pData = Target
snRef(0) = PutSng
End Property
Property Get GetDbl(ByVal Target As LongPtr) As Double
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
dRef_SA.pData = Target
GetDbl = dRef(0)
End Property
Property Let PutDbl(ByVal Target As LongPtr, ByVal PutDbl As Double)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
dRef_SA.pData = Target
dRef(0) = PutDbl
End Property
Property Get GetCur(ByVal Target As LongPtr) As Currency
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
cRef_SA.pData = Target
GetCur = cRef(0)
End Property
Property Let PutCur(ByVal Target As LongPtr, ByVal PutCur As Currency)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
cRef_SA.pData = Target
cRef(0) = PutCur
End Property
Property Get GetDate(ByVal Target As LongPtr) As Date
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
dtRef_SA.pData = Target
GetDate = dtRef(0)
End Property
Property Let PutDate(ByVal Target As LongPtr, ByVal PutDate As Date)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
dtRef_SA.pData = Target
dtRef(0) = PutDate
End Property
Property Get GetStr(ByVal Target As LongPtr) As String
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
sRef_SA.pData = Target
GetStr = sRef(0)
End Property
Property Let PutStr(ByVal Target As LongPtr, ByRef PutStr As String)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
sRef_SA.pData = Target
sRef(0) = PutStr
End Property
Function RefStr(SA As SA1D, Optional ByVal pData As LongPtr) As String()
Dim lpArTmp() As String, pTmp As LongPtr
If islpRefInit Then Else Initialize
' pTmp = lpRef_SA.pData
SA = sRef_SA
If pData > 0 Then SA.pData = pData
lpRef_SA.pData = VarPtr(pTmp) + ptrSz
lpRef(0) = VarPtr(SA)
' lpRef_SA.pData = pTmp
RefStr = lpArTmp
End Function
Property Get GetObj(ByVal Target As LongPtr) As Object
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
oRef_SA.pData = Target
Set GetObj = oRef(0)
End Property
Property Set SetObj(ByVal Target As LongPtr, ByVal SetObj As Object)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
oRef_SA.pData = Target
Set oRef(0) = SetObj
End Property
Property Get GetBool(ByVal Target As LongPtr) As Boolean
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
blRef_SA.pData = Target
GetBool = blRef(0)
End Property
Property Let PutBool(ByVal Target As LongPtr, ByVal PutBool As Boolean)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
blRef_SA.pData = Target
blRef(0) = PutBool
End Property
Property Get GetVar(ByVal Target As LongPtr) As Variant
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
vRef_SA.pData = Target
GetVar = vRef(0)
End Property
Property Let PutVar(ByVal Target As LongPtr, ByRef PutVar As Variant)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
vRef_SA.pData = Target
vRef(0) = PutVar
End Property
Property Set SetVar(ByVal Target As LongPtr, ByRef SetVar As Variant)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
vRef_SA.pData = Target
Set vRef(0) = SetVar
End Property
Property Get GetUnk(ByVal Target As LongPtr) As IUnknown
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
unkRef_SA.pData = Target
Set RefUnk = unkRef(0)
End Property
Property Set SetUnk(ByVal Target As LongPtr, ByVal RefUnk As IUnknown)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
unkRef_SA.pData = Target
Set unkRef(0) = RefUnk
End Property
'Property Get GetDec(ByVal Target As LongPtr) As Variant
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
' dcRef_SA.pData = Target
' RefDec = dcRef(0)
'End Property
'Property Let PutDec(ByVal Target As LongPtr, ByVal RefDec As Variant)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
' dcRef_SA.pData = Target
' dcRef(0) = RefDec
'End Property '_
Property Get GetByte(ByVal Target As LongPtr) As Byte
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
bRef_SA.pData = Target
RefByte = bRef(0)
End Property
Property Let PutByte(ByVal Target As LongPtr, ByVal RefByte As Byte)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
bRef_SA.pData = Target
bRef(0) = RefByte
End Property
Property Get GetLngLng(ByVal Target As LongPtr) As LongLong
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
llRef_SA.pData = Target
RefLngLng = llRef(0)
End Property
#If Win64 = 0 Then
Property Let PutLngLng(ByVal Target As LongPtr, ByRef RefLngLng As LongLong)
#Else
Property Let PutLngLng(ByVal Target As LongPtr, ByVal RefLngLng As LongLong)
#End If
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
llRef_SA.pData = Target
llRef(0) = RefLngLng
End Property
Property Get GetPtr(ByVal Target As LongPtr) As LongPtr
If islpRefInit Then Else Initialize
lpRef_SA.pData = Target
GetPtr = lpRef(0)
End Property
Property Let PutPtr(ByVal Target As LongPtr, ByVal PutPtr As LongPtr)
If islpRefInit Then Else Initialize
lpRef_SA.pData = Target
lpRef(0) = PutPtr
End Property
Function RefPtr(SA As SA1D, Optional ByVal Target As LongPtr) As LongPtr()
Dim lpArTmp() As LongPtr, pTmp As LongPtr
If islpRefInit Then Else Initialize
' pTmp = lpRef_SA.pData
SA = lpRef_SA
SA.pData = Target
lpRef_SA.pData = VarPtr(pTmp) + ptrSz
lpRef(0) = VarPtr(SA)
' lpRef_SA.pData = pTmp
RefPtr = lpArTmp
End Function
Property Get GetSA(ByVal Target As LongPtr) As SA1D
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
saRef_SA.pData = Target
GetSA = saRef(0)
End Property
Property Let PutSA(ByVal Target As LongPtr, RefSA As SA1D)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
saRef_SA.pData = Target
saRef(0) = PutSA
End Property
Private Sub Test_ArrPtr_()
Dim bAr() As Byte, iAr%()
ReDim bAr(2), iAr(2)
Debug.Print ArrPtr(bAr), GetPtr(ArrPtr(iAr))
Debug.Print ArrPtrB(bAr), ArrPtrI(iAr, True)
End Sub
Function ArrPtrB(bAry() As Byte, Optional ByVal GetDesc As Boolean) As LongPtr
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lpRef2_SA.pData = VarPtr(GetDesc) - ptrSz
If GetDesc Then lpRef2_SA.pData = lpRef2(0)
ArrPtrB = lpRef2(0)
End Function
Function ArrPtrI(iAry() As Integer, Optional ByVal GetDesc As Boolean) As LongPtr
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lpRef2_SA.pData = VarPtr(GetDesc) - ptrSz
If GetDesc Then lpRef2_SA.pData = lpRef2(0)
ArrPtrI = lpRef2(0)
End Function
Function ArrPtrV(vAry(), Optional ByVal GetDesc As Boolean) As LongPtr
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lpRef2_SA.pData = VarPtr(GetDesc) - ptrSz
If GetDesc Then lpRef2_SA.pData = lpRef2(0)
ArrPtrV = lpRef2(0)
End Function
'ïåðåìåùåíèå óêàçàòåëÿ (ïåðåäà÷à âëàäåíèÿ)
Sub MovePtr(ByVal pDst As LongPtr, ByVal pSrc As LongPtr)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lpRef_SA.pData = pDst
lpRef2_SA.pData = pSrc
lpRef(0) = lpRef2(0)
lpRef2(0) = 0
End Sub
'ïåðåìåùåíèå óêàçàòåëÿ ñòðîêè èç Variant â String
Function VarMoveStr(vStr) As String
If VarType(vStr) = vbString Then
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lpRef_SA.pData = VarPtr(VarMoveStr)
lpRef2_SA.pData = VarPtr(vStr) + 8
lpRef(0) = lpRef2(0)
lpRef2(0) = 0
End If
End Function
Function StrMoveVar(sStr$)
StrMoveVar = vbNullString
If LenB(sStr) Then
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lpRef_SA.pData = VarPtr(sStr)
lpRef2_SA.pData = VarPtr(StrMoveVar) + 8
lpRef2(0) = lpRef(0) 'StrPtr(sStr)
lpRef(0) = 0
End If
End Function
Private Sub TestMoveStr()
Dim s1$, s2$
s1 = "sdfa"
s2 = "1122"
MoveStr s1, s2
End Sub
'áåçîïàñíîå ïåðåìåùåíèå óêàçàòåëÿ ñòðîêè
Sub MoveStr(sDst$, sSrc$)
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If StrPtr(sDst) Then sDst = vbNullString
lpRef_SA.pData = VarPtr(sDst)
lpRef2_SA.pData = VarPtr(sSrc)
lpRef(0) = lpRef2(0)
lpRef2(0) = 0
End Sub
'îáìåí óêàçàòåëÿìè
Sub SwapPtr(ByVal p1 As LongPtr, ByVal p2 As LongPtr)
Dim pTmp As LongPtr
lpRef_SA.pData = p1
lpRef2_SA.pData = p2
pTmp = lpRef(0)
lpRef(0) = lpRef2(0)
lpRef2(0) = pTmp
End Sub
'Àíàëîã CopyMemory
Sub MemLSet(ByVal pDst As LongPtr, ByVal pSrc As LongPtr, ByVal size As Long)
Dim sDst$, sSrc$, lTmp&
Dim s1$, s2$
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If size > 3 Then
Else
MiniCopy pDst, pSrc, size
Exit Sub
End If
size = size - 4
lRef_SA.pData = pSrc
lTmp = lRef(0)
lRef(0) = size
lRef2_SA.pData = pDst
lRef2(0) = size
pSrc = pSrc + 4
pDst = pDst + 4
sRef_SA.pData = VarPtr(pSrc)
sRef2_SA.pData = VarPtr(pDst)
LSet sRef2(0) = sRef(0)
lRef(0) = lTmp
lRef2(0) = lTmp
End Sub
'âñïîìîãàòåëüíàÿ ïðîöåäóðà äëÿ MemLSet äëÿ êîïèðîâàíèÿ ðàçìåðà ìåíüøå 4 áàéò.
Sub MiniCopy(ByVal pDst As LongPtr, ByVal pSrc As LongPtr, ByVal size As Long)
On size GoTo 1, 2, 3
Exit Sub
If False Then
1:
bRef_SA.pData = pSrc
bRef2_SA.pData = pDst
bRef2(0) = bRef(0)
ElseIf False Then
2:
iRef_SA.pData = pSrc
iRef2_SA.pData = pDst
iRef2(0) = iRef(0)
ElseIf False Then
3:
b3Ref1_SA.pData = pSrc
b3Ref2_SA.pData = pDst
b3Ref2(0) = b3Ref1(0)
End If
End Sub
Function VbaMemRealloc(ByVal pMem As LongPtr, ByVal newSize As Long) As LongPtr
Dim bMap() As Byte, lp As LongPtr
If newSize < 1 Then Exit Function
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If pMem Then
Else
ReDim bMap(newSize - 1)
lpRef_SA.pData = VarPtr(lp) + ptrSz
saRef_SA.pData = lpRef(0)
VbaMemRealloc = saRef(0).pData ' = VarPtr(bMap(0))
saRef(0).pData = 0
Exit Function
End If
bMapDyn_SA.pData = pMem
bMapDyn_SA.Count = newSize
lpRef_SA.pData = VarPtr(lp) + ptrSz
lpRef(0) = VarPtr(bMapDyn_SA)
ReDim Preserve bMap(newSize - 1)
lpRef(0) = 0
VbaMemRealloc = bMapDyn_SA.pData
End Function
Function VbaMemAlloc(ByVal size As LongPtr) As LongPtr
Dim bMap() As Byte, lp As LongPtr
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
ReDim bMap(size - 1)
lpRef_SA.pData = VarPtr(lp) + ptrSz
saRef_SA.pData = lpRef(0)
With saRef(0)
VbaMemAlloc = .pData ' = VarPtr(bMap(0))
.pData = NullPtr
' .Count = 0&
End With
End Function
'Sub VbaMemFree2(ByVal ptr As LongPtr)
' Dim bMap() As Byte, lp As LongPtr
' bMap = vbNullString 'Array()
' lpRef_SA.pData = VarPtr(lp) + ptrSz
' saRef_SA.pData = lpRef(0)
' saRef(0).pData = ptr
'End Sub
Sub VbaMemFree(ByVal Ptr As LongPtr)
Dim s$
lpRef_SA.pData = VarPtr(s)
lpRef(0) = Ptr + 4
End Sub
Function GetBytMap(SA As SA1D, ByVal Ptr As LongPtr, ByVal cbCnt As LongPtr) As Byte()
Dim bMap() As Byte, lp As LongPtr
SA = bMap2_SA
SA.pData = Ptr
SA.Count = cbCnt
lpRef_SA.pData = VarPtr(lp) + ptrSz
lpRef(0) = VarPtr(SA)
GetBytMap = bMap
End Function
Function GetIntMap(SA As SA1D, ByVal Ptr As LongPtr, ByVal ciCnt As LongPtr) As Integer()
Dim iMap() As Integer, lp As LongPtr
SA = iMap2_SA
SA.pData = Ptr
SA.Count = ciCnt
lpRef_SA.pData = VarPtr(lp) + ptrSz
lpRef(0) = VarPtr(SA)
GetIntMap = iMap
End Function
Sub PutStrBuf(ByVal pBuf As LongPtr, sSrc$)
If pBuf > 0 Then Else GoTo errArgum
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
Dim lTmp&
lRef_SA.pData = pBuf - 4
lTmp = lRef(0)
lRef(0) = LenB(sSrc)
sRef_SA.pData = VarPtr(pBuf)
LSet sRef(0) = sSrc
lRef(0) = lTmp
Exit Sub
errArgum:
Err.Raise 5, , "Bad argument!!"
End Sub
'>>>>>>>>>>>>>>>STRINGS SECTION<<<<<<<<<<<<<<<<<<<'
Private Function StrCompVBA(str1$, str2$) As Long
Dim len1&, len2&, lenMin&
Dim i&, dif&
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
len1 = Len(str1) + 1: len2 = Len(str2) + 1
If len1 > len2 Then lenMin = len2 Else lenMin = len1
iRef1_SA.pData = StrPtr(str1)
iRef1_SA.Count = lenMin
iRef2_SA.pData = StrPtr(str2)
iRef2_SA.Count = lenMin
For i = 1 To lenMin
dif = iRef1(i) - iRef2(i)
If dif Then Exit For
Next
StrCompVBA = dif
End Function
'àíàëîã instr$() ñ äîïîëíèòåëíûì ïàðàìåòðîì endFind, ÷òîáû óêàçûâàòü ïîçèöèþ îêîí÷àíèÿ ïîèñêà.
Function InStrEnd(sCheck$, sMatch$, Optional ByVal Start As Long = 1, _
Optional ByVal Compare As VbCompareMethod, Optional ByVal endFind As Long = -1) As Long
Dim i&, j&, k&, lenCheck&, lenMatch&, iMatch%
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If Compare = vbBinaryCompare Then
iMap1_SA.pData = StrPtr(sCheck)
iMap2_SA.pData = StrPtr(sMatch)
Else
Dim sTmp1$, sTmp2$
sTmp1 = UCase$(sCheck)
sTmp2 = UCase$(sMatch)
iMap1_SA.pData = StrPtr(sTmp1)
iMap2_SA.pData = StrPtr(sTmp2)
End If
lenCheck = Len(sCheck)
lenMatch = Len(sMatch)
iMap1_SA.Count = lenCheck
iMap2_SA.Count = lenMatch
If endFind = -1 Then endFind = lenCheck
iMatch = iMap2(1) 'v2
For i = Start To endFind - lenMatch + 1
If iMap1(i) <> iMatch Then
Else
k = i
For j = 2 To lenMatch
k = k + 1
If iMap1(k) = iMap2(j) Then Else GoTo skip
Next
InStrEnd = i: Exit Function
End If
skip:
Next
End Function
Function InStrEndB(sCheck$, sMatch$, Optional ByVal Start As Long = 1, _
Optional ByVal Compare As VbCompareMethod, Optional ByVal endFind As Long = -1) As Long
Dim i&, j&, k&, lenCheck&, lenMatch&, bMatch As Byte
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If Compare = vbBinaryCompare Then
bMap1_SA.pData = StrPtr(sCheck)
bMap2_SA.pData = StrPtr(sMatch)
Else
Dim sTmp1$, sTmp2$
sTmp1 = UCase$(sCheck)
sTmp2 = UCase$(sMatch)
bMap1_SA.pData = StrPtr(sTmp1)
bMap2_SA.pData = StrPtr(sTmp2)
End If
lenCheck = LenB(sCheck)
lenMatch = LenB(sMatch)
bMap1_SA.Count = lenCheck
bMap2_SA.Count = lenMatch
If endFind = -1 Then endFind = lenCheck
bMatch = bMap2(1) 'v2
For i = Start To endFind - lenMatch + 1
If bMap1(i) <> bMatch Then
Else
k = i
For j = 2 To lenMatch
k = k + 1
If bMap1(k) = bMap2(j) Then Else GoTo skip
Next
InStrEndB = i: Exit Function
End If
skip:
Next
End Function
Function InStrEndRev(sCheck$, sMatch$, Optional ByVal Start As Long = -1, _
Optional ByVal Compare As VbCompareMethod, Optional ByVal endFind As Long = 1) As Long
Dim i&, j&, k&, lenCheck&, lenMatch&, iMatch%
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If Compare = vbBinaryCompare Then
iMap1_SA.pData = StrPtr(sCheck)
iMap2_SA.pData = StrPtr(sMatch)
Else
Dim sTmp1$, sTmp2$
sTmp1 = UCase$(sCheck)
sTmp2 = UCase$(sMatch)
iMap1_SA.pData = StrPtr(sTmp1)
iMap2_SA.pData = StrPtr(sTmp2)
End If
lenCheck = Len(sCheck)
lenMatch = Len(sMatch)
iMap1_SA.Count = lenCheck
iMap2_SA.Count = lenMatch
If Start = -1 Then Start = lenCheck
' Dim bgnIter& ' 'v1
' j = lenMatch
' iMatch = iMap2(lenMatch)
' bgnIter = lenMatch - 1
' For i = Start To endFind + lenMatch - 1 Step -1
' If iMap1(i) <> iMatch Then
' Else
' k = i
' For j = bgnIter To 1 Step -1
' k = k - 1
' If iMap1(k) = iMap2(j) Then Else GoTo skip
' Next
' InStrEndRev = k: Exit Function
' End If
'skip:
' Next
iMatch = iMap2(1) 'v2
For i = Start - lenMatch + 1 To endFind Step -1
If iMap1(i) <> iMatch Then
Else
k = i
For j = 2 To lenMatch
k = k + 1
If iMap1(k) = iMap2(j) Then Else GoTo skip
Next
InStrEndRev = i: Exit Function
End If
skip:
Next
End Function
Function InStrEndRevB(sCheck$, sMatch$, Optional ByVal Start As Long = -1, _
Optional ByVal endFind As Long = 1, Optional ByVal Compare As VbCompareMethod) As Long
Dim i&, j&, k&, lenCheck&, lenMatch&, bMatch As Byte
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
If Compare = vbBinaryCompare Then
bMap1_SA.pData = StrPtr(sCheck)
bMap2_SA.pData = StrPtr(sMatch)
Else
Dim sTmp1$, sTmp2$
sTmp1 = UCase$(sCheck)
sTmp2 = UCase$(sMatch)
bMap1_SA.pData = StrPtr(sTmp1)
bMap2_SA.pData = StrPtr(sTmp2)
End If
lenCheck = LenB(sCheck)
lenMatch = LenB(sMatch)
bMap1_SA.Count = lenCheck
bMap2_SA.Count = lenMatch
If Start = -1 Then Start = lenCheck
bMatch = bMap2(1) 'v2
For i = Start - lenMatch + 1 To endFind Step -1
If bMap1(i) <> bMatch Then
Else
k = i
For j = 2 To lenMatch
k = k + 1
If bMap1(k) = bMap2(j) Then Else GoTo skip
Next
InStrEndRevB = i: Exit Function
End If
skip:
Next
End Function
Function InStrLenRevB(sCheck$, sMatch$, Optional ByVal Start As Long = -1, _
Optional ByVal Length As Long = 1, Optional ByVal Compare As VbCompareMethod) As Long
If Start = -1 Then Start = LenB(sCheck)
InStrLenRevB = InStrEndRevB(sCheck, sMatch, Start, Start - Length + 1, Compare)
End Function
Function InStrLen(ByVal Start As Long, sCheck$, sMatch$, ByVal lenFind As Long, _
Optional ByVal Compare As VbCompareMethod) As Long
Dim szCheck&, newSize&, pCheck As LongPtr
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
pCheck = StrPtr(sCheck)
If pCheck Then Else Exit Function
lRef_SA.pData = pCheck - 4
szCheck = lRef(0)
newSize = (Start + lenFind - 1) * 2
If newSize < szCheck Then
If newSize > -1 Then Else GoTo errArgum
lRef(0) = newSize
InStrLen = InStr(Start, sCheck, sMatch, Compare)
lRef(0) = szCheck
Else: InStrLen = InStr(Start, sCheck, sMatch, Compare)
End If
Exit Function
errArgum:
Err.Raise 5, , "invalid function argumenct"
End Function
Function InStrLenB(ByVal Start As Long, sCheck$, sMatch$, ByVal lenFind As Long, _
Optional ByVal Compare As VbCompareMethod) As Long
Dim szCheck&, newSize&, pCheck As LongPtr
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
pCheck = StrPtr(sCheck)
If pCheck Then Else Exit Function
lRef_SA.pData = pCheck - 4
szCheck = lRef(0)
newSize = Start + lenFind - 1
If newSize < szCheck Then
If newSize > -1 Then Else GoTo errArgum
lRef(0) = newSize
InStrLenB = InStrB(Start, sCheck, sMatch, Compare)
lRef(0) = szCheck
Else: InStrLenB = InStrB(Start, sCheck, sMatch, Compare)
End If
Exit Function
errArgum:
Err.Raise 5, , "invalid function argumenct"
End Function
'No safe version without any checks NS = Not Safe
Function InStrEndNS(ByVal Start As Long, sCheck$, sMatch$, ByVal endFind As Long, _
Optional ByVal Compare As VbCompareMethod) As Long
Dim lTmp&
#If Not PreInitMode Then
If IsInitialized Then Else Initialize
#End If
lRef_SA.pData = StrPtr(sCheck) - 4