gpt4 book ai didi

VBA 32 位和 64 位类型不匹配

转载 作者:行者123 更新时间:2023-12-04 12:48:19 24 4
gpt4 key购买 nike

已更新请查看问题的最后部分以获取更新

完整源代码在这里:

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=30761&lngWId=1

我有一段代码不起作用,可能是由于 32 位和 64 位的差异:

部分代码:(我添加了 PtrSafe,我不应该为第一个)

Private Declare PtrSafe Function ArrPtr& Lib "msvbvm60.dll" Alias "VarPtr" (ptr() As Any)
Private Declare PtrSafe Sub RtlMoveMemory Lib "kernel32" (dst As Any, src As Any, ByVal nBytes&)

Private Header1(5) As Long
Private Header2(5) As Long
Private SafeArray1() As Integer
Private SafeArray2() As Integer
Private LUT(8482) As Long

Private Sub Class_Initialize()
Dim i As Long

' Set up our template for looking at strings
Header1(0) = 1 ' Number of dimensions
Header1(1) = 2 ' Bytes per element (integer = 2)
Header1(4) = &H7FFFFFFF ' Array size, 2.1+ billion should cover us

' Force SafeArray1 to use Header1 as its own header
RtlMoveMemory ByVal ArrPtr(SafeArray1), VarPtr(Header1(0)), 4

我搜索了一段时间,发现 msvbvm60.dll 似乎是旧的 32 位 VBA,已被 VBA7.dll 取代。尝试重新注册该文件,因为它位于 SysWOW64 文件夹中,之后 VBE 仍然报告找不到该文件。所以我注释了第一行,并将最后一行更改为:

RtlMoveMemory ByVal VarPtr(SafeArray1), VarPtr(Header1(0)), 4

我相信 ArrPtr 只是带有别名的 VarPtr。

现在我收到类型不匹配错误。我的理解是代码将 SafeArray1 的 header 指向 Header1(0) 的地址。我假设这意味着 SafeArray1[0]~SafeArray[9] 应该包含 Header1(0)~Header1(4) 中的信息,因为 Header1() 是 Long 而 SafeArray() 是 Integer?但是 VarPtr 不应该在 32 位 Office 中返回 Long 吗?为什么可以在 Integer 数组上使用它?

但重点是如何让它在64位Office上运行。由于 VarPtr 现在返回 LongPtr 而不是 Long,我不确定如何修改代码。

更新我找到了另一段代码,它为我提供了指向数组的指针:

Private Declare PtrSafe Function VarPtrArray Lib "VBE7" Alias _
"VarPtr" (ByRef Var() As Any) As LongPtr

然后我写一个测试模块:

Private Declare PtrSafe Function VarPtrArray Lib "VBE7" Alias _
"VarPtr" (ByRef Var() As Any) As LongPtr
Private Declare PtrSafe Sub RtlMoveMemory Lib "kernel32" (dst As Any, src As Any, ByVal nBytes&)
Private Header1(5) As Long
Private SafeArray1() As Integer

Sub test()

Header1(0) = 1 ' Number of dimensions
Header1(1) = 2 ' Bytes per element (integer = 2)
Header1(4) = &H7FFFFFFF ' Array size, 2.1+ billion should cover us

RtlMoveMemory ByVal VarPtrArray(SafeArray1), VarPtr(Header1(0)), 4
Debug.Print SafeArray1(0)

End Sub

但 VBE 在 RtlMoveMemory 行崩溃。

最佳答案

以下示例应显示如何处理 32 位和 64 位版本的 Declare 语句:

Option Explicit

#If VBA7 Then '64 bit
Private Declare PtrSafe Function DrawMenuBar Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
#Else '32 bit
Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Long) As Long
#End If

关于VBA 32 位和 64 位类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50431565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com