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
说明:
Mac 版本的 CopyMemory 函数返回结果,而 Win 版本不返回。 (结果是 dest 指针,除非发生错误。请参阅 memmove 引用 here。)但是,两个版本都可以以完全相同的方式使用,无需括号。
声明的区别如下:
-
64 位 Mac/Win VBA7:
- 使用
PtrSafe 关键字
- 对所有
ByRef参数使用Any类型
- 对
ByVal 句柄/指针参数/返回值使用LongPtr 类型
- 将
LongLong 类型适当地用于其他返回值/参数
-
32 位 Win VBA7:
- 使用
PtrSafe关键字
-
所有
ByRef参数使用Any类型
- 对
ByVal句柄/指针参数/返回值使用LongPtr类型
- 对其他返回值/参数适当使用
Long(not LongLong)类型
-
32 位 Mac/Win VBA6:
- 没有
PtrSafe关键字
- 对所有
ByRef参数使用Any类型
- 为
ByVal句柄/指针参数/返回值使用Long类型
- 将
Long 类型适当地用于其他返回值/参数
注意事项:
注意:如果你对我的变量命名约定感到好奇,它是基于RVBA。
*正确的方式。