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
| 'Attribute VB_Name = "RefTypes"
'================================================================================================================================'
' RefTypes (edited By testuser2 07.25) '
'-------------------------------------------------------- '
' 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) '
' '
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated '
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation '
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, '
' and to permit persons to whom the Software is furnished to do so, subject to the following conditions: '
' '
' The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. '
' '
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO '
' THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE '
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, '
' TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. '
'================================================================================================================================'
' Option Private Module
Option Explicit
#If Win64 = 1 Then
Private Const Win64 As Integer = 1
#Else
Private Const Win64 As Integer = 0
Public Type LongLong
L0x0 As Long
L0x4 As Long
End Type
#End If
#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
Private Const LPTR_SIZE As Long = 4 + (Win64 * 4)
Private Const FADF_FIXEDSIZE_AUTO = &H11&
'*********************************************************************************'
' This section just sets up the array bounds according to the four `UserDefined` '
' parameters. If you already know the correct bounds, you can just hardcode them. '
Private Const LPROXY_SIZE As Long = 1 '// UserDefined (should be an integer divisor of <PTR_SIZE>)
Private Const LELEMENT_SIZE As Long = LPTR_SIZE '// UserDefined (usually should be <PTR_SIZE>)
Private Const LSIZE_PROXIED As Long = LELEMENT_SIZE - LPROXY_SIZE
Private Const LBLOCK_STEP_SIZE As Long = LELEMENT_SIZE * LSIZE_PROXIED
Private Const Proxy_Count = 19
Private Enum BOUNDS_HELPER:
[_BLOCK_ALLOCATION_SIZE] = Proxy_Count * LPTR_SIZE '// UserDefined (the size of the region being proxied)
[_0x0_ADDRESS_ALIGNMENT] = 0 '// UserDefined (<BLOCK_ALLOCATION_ADDRESS> Mod <PTR_SIZE>)
[_SIZE_OF_ALIGNED_BLOCK] = ([_0x0_ADDRESS_ALIGNMENT] - LPTR_SIZE) * ([_0x0_ADDRESS_ALIGNMENT] > 0) + [_BLOCK_ALLOCATION_SIZE]
[_ELEMENT_SIZE_OF_PROXY] = ([_SIZE_OF_ALIGNED_BLOCK] + LBLOCK_STEP_SIZE - 1) \ LBLOCK_STEP_SIZE
[_ELEMENT_SIZE_OF_BLOCK] = ([_ELEMENT_SIZE_OF_PROXY] * (LELEMENT_SIZE \ LPROXY_SIZE)) - [_ELEMENT_SIZE_OF_PROXY]
End Enum
Private Const LELEMENTS_LBOUND As Long = [_ELEMENT_SIZE_OF_PROXY] * -1
Private Const LELEMENTS_UBOUND As Long = [_ELEMENT_SIZE_OF_BLOCK] + (LELEMENTS_LBOUND < 0)
Private Const LBLOCK_OFFSET As Long = [_ELEMENT_SIZE_OF_PROXY] * LELEMENT_SIZE '// Relative to VarPtr(<MemoryProxyVariable>)
Private Const LBLOCK_SIZE As Long = [_ELEMENT_SIZE_OF_BLOCK] * LELEMENT_SIZE '
'*********************************************************************************'
Private Type ProxyElement '// This could be anything. I have it as an array of bytes only for the
ProxyAlloc(LPROXY_SIZE - 1) As Byte '// convenience of binding its size to a constant, and for maximum granularity.
End Type '// Although, be aware that this structure determines MemoryProxy alignment.
Private Type MemoryProxy '// The declared type of `Elements()` can be any of
Elements(LELEMENTS_LBOUND To LELEMENTS_UBOUND) As ProxyElement '// the following: Enum, UDT, or Alias (typedef)
End Type '// NOTE: A ProxyElement's Type must be smaller
'// than the Type of the Element it represents.
'******************************************************************'
' When passed to `InitByProxy()`, the `Initializer.Elements` array '
' provides access to fourteen, pointer-sized elements immmediately '
' following the `Initializer` variable's memory allocation. '
Public Initializer As MemoryProxy
' <Memory proxied by `Initializer`>
Public m_RefInt() As Integer '1 m_SA1
Public m_RefLng() As Long
Public m_RefSng() As Single
Public m_RefDbl() As Double
Public m_RefCur() As Currency
Public m_RefDate() As Date
Public m_RefStr() As String
Public m_RefObj() As Object
Public m_RefBool() As Boolean
Public m_RefVar() As Variant
Public m_RefUnk() As IUnknown
'Public m_RefDec() As Variant
Public m_RefByte() As Byte
Public m_RefLngLng() As LongLong
Public m_RefLngPtr() As LongPtr '14
Public m_RefLngPtr2() As LongPtr '15 m_SA2
Public iMap1() As Integer '16 iMap1_SA мапперы строк
Public iMap2() As Integer '17 iMap2_SA
Public bMap1() As Byte '18 iMap1_SA
Public bMap2() As Byte '19 iMap2_SA
' <End of proxied memory block>
'*******************************************************************'
'*************************************************************************************************'
' Inspired by Cristian Buse's `VBA-MemoryTools` <https://github.com/cristianbuse/VBA-MemoryTools> '
' Arbitrary memory access is achieved via a carefully constructed SAFEARRAY `Descriptor` struct. '
Public m_SA As SAFEARRAY1D, m_SA2 As SAFEARRAY1D
Public iMap1_SA As SAFEARRAY1D, iMap2_SA As SAFEARRAY1D, bMap1_SA As SAFEARRAY1D, bMap2_SA As SAFEARRAY1D
'*************************************************************************************************'
Private IsInitialized As Boolean
Sub Initialize()
If IsInitialized Then Exit Sub
With m_SA
.cDims = 1
.fFeatures = FADF_FIXEDSIZE_AUTO
.cLocks = 1
.cbElements = 1
.Bounds.cCount = 1
End With
With m_SA2
.cDims = 1
.fFeatures = FADF_FIXEDSIZE_AUTO
.cLocks = 1
.cbElements = ptrSz
.Bounds.cCount = 1
End With
With iMap1_SA
.cDims = 1
.fFeatures = FADF_FIXEDSIZE_AUTO
.cLocks = 1
.cbElements = 2
.Bounds.lBound = 1
End With
With iMap2_SA
.cDims = 1
.fFeatures = FADF_FIXEDSIZE_AUTO
.cLocks = 1
.cbElements = 2
.Bounds.lBound = 1
End With
With bMap1_SA
.cDims = 1
.fFeatures = FADF_FIXEDSIZE_AUTO
.cLocks = 1
.cbElements = 1
.Bounds.lBound = 1
End With
With bMap2_SA
.cDims = 1
.fFeatures = FADF_FIXEDSIZE_AUTO
.cLocks = 1
.cbElements = 1
.Bounds.lBound = 1
End With
InitByProxy Initializer.Elements, VarPtr(m_SA), 14
InitOneByProxy Initializer.Elements, 14, VarPtr(m_SA2)
InitOneByProxy Initializer.Elements, 15, VarPtr(iMap1_SA)
InitOneByProxy Initializer.Elements, 16, VarPtr(iMap2_SA)
InitOneByProxy Initializer.Elements, 17, VarPtr(bMap1_SA)
InitOneByProxy Initializer.Elements, 18, VarPtr(bMap2_SA)
IsInitialized = True
End Sub
'*********************************************************************************'
' This is only possible because the compiler does not (or cannot?) discriminate '
' between <Non-Intrinsic Array Argument> types passed to <Array Parameters> whose '
' <Declared Type> is an <Enum> or an <Alias> (a non-struct typdef). '
' Such Array Parameters will accept any <UDT/Enum/Alias>-typed array argument. '
' '
' Another key behavior is that (except for cDims, pvData, and Bounds) the array '
' descriptor has no effect on indexing/reading/writing the array elements within '
' the scope of the receiving procedure; indexing/reading/writing align with the '
' declared type of the Array Parameter. (this behavior is not critical, but it '
' greatly simplifies the implementation) NOTE: You cannot pass an element ByRef '
' from inside the procedure. Doing so passes the address of its proxy. '
' '
' Similarly, Array Parameters whose <Declared Type> is <Fixed-Length-String> will '
' accept ANY <Fixed-Length-String> array argument, regardless of Declared Length. '
' However, since Fixed-Length-Strings have no alignment, the starting position of '
' an element and the starting position of its proxy will always be the same. '
'*********************************************************************************'
Private Sub InitByProxy(ProxyElements() As LONG_PTR, ByVal pSA As LongPtr, ByVal proxyCount&)
Dim i As Long
For i = 0& To proxyCount - 1
ProxyElements(i) = pSA
Next i
End Sub
Private Sub InitOneByProxy(ProxyElements() As LONG_PTR, ByVal num As Long, ByVal pSA As LongPtr)
ProxyElements(num) = pSA
End Sub
Property Get RefInt(ByVal Target As LongPtr) As Integer
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefInt = m_RefInt(0&)
End Property
Property Let RefInt(ByVal Target As LongPtr, ByVal RefInt As Integer)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefInt(0&) = RefInt
End Property
Property Get RefLng(ByVal Target As LongPtr) As Long
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefLng = m_RefLng(0&)
End Property
Property Let RefLng(ByVal Target As LongPtr, ByVal RefLng As Long)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefLng(0&) = RefLng
End Property
Property Get RefSng(ByVal Target As LongPtr) As Single
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefSng = m_RefSng(0&)
End Property
Property Let RefSng(ByVal Target As LongPtr, ByVal RefSng As Single)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefSng(0&) = RefSng
End Property
Property Get RefDbl(ByVal Target As LongPtr) As Double
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefDbl = m_RefDbl(0&)
End Property
Property Let RefDbl(ByVal Target As LongPtr, ByVal RefDbl As Double)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefDbl(0&) = RefDbl
End Property
Property Get RefCur(ByVal Target As LongPtr) As Currency
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefCur = m_RefCur(0&)
End Property
Property Let RefCur(ByVal Target As LongPtr, ByVal RefCur As Currency)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefCur(0&) = RefCur
End Property
Property Get RefDate(ByVal Target As LongPtr) As Date
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefDate = m_RefDate(0&)
End Property
Property Let RefDate(ByVal Target As LongPtr, ByVal RefDate As Date)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefDate(0&) = RefDate
End Property
Property Get RefStr(ByVal Target As LongPtr) As String
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefStr = m_RefStr(0&)
End Property
Property Let RefStr(ByVal Target As LongPtr, ByRef RefStr As String)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
m_RefStr(0&) = RefStr
End Property
Property Get RefObj(ByVal Target As LongPtr) As Object
If IsInitialized Then Else Initialize
m_SA.pvData = Target
Set RefObj = m_RefObj(0&)
End Property
Property Set RefObj(ByVal Target As LongPtr, ByVal RefObj As Object)
If IsInitialized Then Else Initialize
m_SA.pvData = Target
Set m_RefObj(0&) = RefObj
End Property
Property Get RefBool(ByVal Target As LongPtr) As Boolean
If IsInitialized Then Else Initialize
m_SA.pvData = Target
RefBool = m_RefBool(0&)
End Property |