On Windows the declared function RtlMoveMemory
provides a way to copy a block of bytes from one address to another:
Private Declare PtrSafe Sub RtlMoveMemory Lib "kernel32" ( _
ByVal dest As LongPtr, _
ByVal src As LongPtr, _
ByVal size As LongPtr)
What is the equivalent on Mac OS X ?
Use VBA add-ins and macros that you developed for Office for Windows with Office for Mac. If you are authoring Macros for Office for Mac, you can use most of the same objects that are available in VBA for Office.
Using Excel VBA for Mac enables you to streamline processes and automate mundane tasks. Depending on the Excel versions you're running, one way to do this is by adding Excel VBA macros in the VBA editor dialogue box/code window.
Select the Developer tab from the toolbar at the top of the screen. Then click on the Editor option in the Visual Basic group. Now the Microsoft Visual Basic editor should appear and you can view your VBA code.
What is the equivalent on Mac OS X ?
Short answer:
Private Declare PtrSafe Function CopyMemory Lib "libc.dylib" Alias "memmove" _
( _
ByVal dest As LongPtr _
, ByVal src As LongPtr _
, ByVal size As LongLong _
) _
As LongPtr
Long answer: Depends ;)
The following is a fully fledged example which uses conditional compilation* to allow it to work on any Mac/Windows/32-bit/64-bit computer. It also demonstrates the two different ways to declare and call the function (by Pointer and by Variable).
#If Mac Then
#If Win64 Then
Private Declare PtrSafe Function CopyMemory_byPtr Lib "libc.dylib" Alias "memmove" (ByVal dest As LongPtr, ByVal src As LongPtr, ByVal size As Long) As LongPtr
Private Declare PtrSafe Function CopyMemory_byVar Lib "libc.dylib" Alias "memmove" (ByRef dest As Any, ByRef src As Any, ByVal size As Long) As LongPtr
#Else
Private Declare Function CopyMemory_byPtr Lib "libc.dylib" Alias "memmove" (ByVal dest As Long, ByVal src As Long, ByVal size As Long) As Long
Private Declare Function CopyMemory_byVar Lib "libc.dylib" Alias "memmove" (ByRef dest As Any, ByRef src As Any, ByVal size As Long) As Long
#End If
#ElseIf VBA7 Then
#If Win64 Then
Private Declare PtrSafe Sub CopyMemory_byPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As LongPtr, ByVal src As LongPtr, ByVal size As LongLong)
Private Declare PtrSafe Sub CopyMemory_byVar Lib "kernel32" Alias "RtlMoveMemory" (ByRef dest As Any, ByRef src As Any, ByVal size As LongLong)
#Else
Private Declare PtrSafe Sub CopyMemory_byPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As LongPtr, ByVal src As LongPtr, ByVal size As Long)
Private Declare PtrSafe Sub CopyMemory_byVar Lib "kernel32" Alias "RtlMoveMemory" (ByRef dest As Any, ByRef src As Any, ByVal size As Long)
#End If
#Else
Private Declare Sub CopyMemory_byPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As Long, ByVal src As Long, ByVal size As Long)
Private Declare Sub CopyMemory_byVar Lib "kernel32" Alias "RtlMoveMemory" (ByRef dest As Any, ByRef src As Any, ByVal size As Long)
#End If
Public Sub CopyMemoryTest()
Dim abytDest(0 To 11) As Byte
Dim abytSrc(0 To 11) As Byte
Dim ¡ As Long
For ¡ = LBound(abytSrc) To UBound(abytSrc)
abytSrc(¡) = AscB("A") + ¡
Next ¡
MsgBox "Dest before copy = #" & ToString(abytDest) & "#"
CopyMemory_byVar abytDest(0), abytSrc(0), 4
MsgBox "Dest during copy = #" & ToString(abytDest) & "#"
CopyMemory_byPtr VarPtr(abytDest(0)) + 4, VarPtr(abytSrc(0)) + 4, 4
MsgBox "Dest during copy = #" & ToString(abytDest) & "#"
CopyMemory_byPtr VarPtr(abytDest(8)), VarPtr(abytSrc(8)), 4
MsgBox "Dest after copy = #" & ToString(abytDest) & "#"
End Sub
Public Function ToString(ByRef pabytBuffer() As Byte) As String
Dim ¡ As Long
For ¡ = LBound(pabytBuffer) To UBound(pabytBuffer)
ToString = ToString & Chr$(pabytBuffer(¡))
Next ¡
End Function
Explanation:
The Mac version of the CopyMemory function returns a result whilst the Win version does not. (The result is the dest pointer, unless an error occurred. See the memmove reference here.) Both versions, however, can be used in exactly the same way, without brackets.
The declare differences are as follows:
64-bit Mac/Win VBA7:
PtrSafe
keywordAny
type for all ByRef
parametersLongPtr
type for ByVal
handle/pointer parameters/return valuesLongLong
type appropriately for other return values/parameter32-bit Win VBA7:
PtrSafe
keywordAny
type for all ByRef
parametersLongPtr
type for ByVal
handle/pointer parameters/return valuesLong
(not LongLong
) type appropriately for other return values/parameter32-bit Mac/Win VBA6:
PtrSafe
keywordAny
type for all ByRef
parametersLong
type for ByVal
handle/pointer parameters/return valuesLong
type appropriately for other return values/parameterCaveats:
Tested on Windows Excel 2007 32-bit.
Seems Excel 2007 has issues related to double word alignment. In this example:
CopyMemory_byVar abytDest(0), abytSrc(0), 4 '-> ABCD
CopyMemory_byVar abytDest(1), abytSrc(1), 8 '-> ABCDEFGHI
CopyMemory()
skips all copying until a double word alignment is
reached (3 skips in this case), then continues copying from the 4th
byte.
Note: If you are curious about my variable naming convention, it is based on RVBA.
*The correct way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With