Skip to content

Instantly share code, notes, and snippets.

@wqweto
Created August 19, 2026 08:58
Show Gist options
  • Select an option

  • Save wqweto/c68db39cea59d93907f0dc9dc99d8bc5 to your computer and use it in GitHub Desktop.

Select an option

Save wqweto/c68db39cea59d93907f0dc9dc99d8bc5 to your computer and use it in GitHub Desktop.
Problem with overlaying arrays
[Description("")]
[FormDesignerId("EEE61188-FE4E-4F4F-9274-D6373E8C5008")]
[PredeclaredId]
Class Form1
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
Private Declare PtrSafe Function ArrPtr Lib "vbe7" Alias "VarPtr" (Ptr() As Any) As LongPtr
Private Type SAFEARRAY1D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As LongPtr
cElements As Long
lLbound As Long
End Type
Private Type ArrayLong4
Item(0 To 3) As Long
End Type
Sub New()
Dim baBuffer() As Byte
baBuffer = "This should remain unmodified. This should remain unmodified"
Debug.Print baBuffer
pvTest baBuffer
Debug.Print baBuffer
End Sub
Private Sub pvTest(baInput() As Byte)
Dim aBlock() As ArrayLong4
Dim uPeekBlock As SAFEARRAY1D
Dim uTag As ArrayLong4
Dim pDummy As LongPtr
With uPeekBlock
.cDims = 1
.fFeatures = 1 ' FADF_AUTO
.cbElements = LenB(uTag)
.cLocks = 1
.pvData = VarPtr(baInput(0))
.cElements = (UBound(baInput) + 1) \ .cbElements
End With
Call CopyMemory(ByVal ArrPtr(aBlock), VarPtr(uPeekBlock), LenB(pDummy))
' uPeekBlock.pvData = 0
' uPeekBlock.cElements = 0
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment