PtrSafe
使用 PtrSafe 仅在 64 位系统上启用 32 位 API 调用,如下所示:
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Public Sub TestScreenResolution()
Debug.Print ScreenResolution
End Sub
Private Function ScreenResolution() As Double
Dim hDC As LongPtr
hDC = GetDC(0)
ScreenResolution = GetDeviceCaps(hDC, 88)
ReleaseDC 0, hDC
End Function
条件编译
您可以通过条件编译同时使用两者
#If Win64 Then
Private Declare PtrSafe Function MakeSureDirectoryPathExists _
Lib "imagehlp.dll" (ByVal DirPath As String) As Boolean
#Else
Private Declare Function MakeSureDirectoryPathExists Lib _
"imagehlp.dll" (ByVal DirPath As String) As Boolean
#End If
您自己的带有 64 位变量的子或函数
可以这样声明:
Public Sub TestMySub()
Call MySub(123456789, 123456789)
End Sub
Private Sub MySub(ByVal x As LongLong, ByVal y As LongPtr)
Dim z As LongLong
z = x * y
MsgBox z
End Sub
我更喜欢Private 尽可能长的时间,而Public 只有在需要“外部”访问权限的情况下。
数据类型 LongLong vs. LongPtr vs. Decimal
在 64 位上,LongLong 和 LongPtr 都是 8 字节的“LongLong 整数”:
-9.223.372.036.854.775.808 到 9.223.372.036.854.775.807
但请注意:但如果您使用 LongLong,它将仅适用于 64 位,其中 32 位上的 LongPtr 将被简单地处理为 4 个字节的 Long,这会导致
-2.147.483.648 到 2.147.483.647
因此,如果您确实需要在两个系统上都具有较高的值,而 Long 还不够,请考虑使用 Double(8 字节,包括舍入效果!)或 Decimal(14 字节,必须声明为 @ 987654333@先):
Private Sub DataTypeDecimal()
' Decimal only via declaration as Variant and type cast as Decimal
Dim d As Variant
d = CDec("79.228.162.514.264.337.593.543.950.335")
Debug.Print d
d = CDec("-79.228.162.514.264.337.593.543.950.335")
Debug.Print d
End Sub