在Mac OS X上的等效操作是什么?
简短回答:
Private Declare PtrSafe Function CopyMemory Lib "libc.dylib" Alias "memmove" _
( _
ByVal dest As LongPtr _
, ByVal src As LongPtr _
, ByVal size As LongLong _
) _
As LongPtr
长话短说:要看情况而定 ;)
下面是一个完整的例子,它使用条件编译*来允许在任何Mac/Windows/32位/64位计算机上运行。它还演示了声明和调用函数的两种不同方式(通过指针和变量)。
#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
解释:
CopyMemory函数的Mac版本返回一个结果,而Win版本则没有。(该结果是目标指针,除非发生错误。请查看这里memmove参考文献。)但是,两个版本都可以以相同的方式使用,无需括号。
声明差异如下:
64位 Mac/Win VBA7:
- 使用
PtrSafe
关键字
- 对于所有
ByRef
参数,使用Any
类型
- 对于
ByVal
句柄/指针参数/返回值,使用LongPtr
类型
- 适当使用
LongLong
类型用于其他返回值/参数
32位 Win VBA7:
- 使用
PtrSafe
关键字
- 对于所有
ByRef
参数,使用Any
类型
- 对于
ByVal
句柄/指针参数/返回值,使用LongPtr
类型
- 适当使用
Long
(不是 LongLong
)类型用于其他返回值/参数
32位 Mac/Win VBA6:
- 没有
PtrSafe
关键字
- 对于所有
ByRef
参数,使用Any
类型
- 对于
ByVal
句柄/指针参数/返回值,使用Long
类型
- 适当使用
Long
类型用于其他返回值/参数
注意事项:
注意:如果您对我的变量命名约定感到好奇,它基于RVBA。
*正确的方式。
alloc
。这在这里没有帮助,因为RtlMoveMemory
可以访问所有进程内存。 - LS_ᴅᴇᴠ