我指的是来自子类的父类,而不是简单的do,用于示例
Public Property Set Parent(obj As ClassProperties)
Set this.ParentColl = obj
End Property当我循环遍历和创建类的实例时,我更倾向于避免对类的引用,避免出现纠缠和“内存不足”错误,因此我使用了基于这的那。
它具有32位的魅力,但在64位时,我得到的运行时错误“53”文件未找到:内核.
在一个模块中:
#If VBA7 Then
Private Declare PtrSafe Sub CopyMemory Lib "kernel" Alias "RtlMoveMemory" _
(dest As Any, Source As Any, ByVal Length As LongPtr)
#Else
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(dest As Any, Source As Any, ByVal Length As Long)
#End If
'Returns an object given its pointer.
Public Function ObjFromPtr(ByVal pObj As Long) As Object
Dim obj As Object
CopyMemory obj, pObj, 4
Set ObjFromPtr = obj
CopyMemory obj, 0&, 4
End Function
Public Function ObjFromPtrVBA7(ByVal pObj As LongPtr) As Object
Dim obj As Object
CopyMemory obj, pObj, 4 <== here is the error
Set ObjFromPtrVBA7 = obj
CopyMemory obj, 0&, 4
End Function在儿童班:
#If VBA7 Then 'Uses modParentChildDereference
Private mlParentPtr As LongPtr
#Else
Private mlParentPtr As Long
#End If
Public Property Get Parent() As ClassProperties
#If VBA7 Then 'Uses modParentChildDereference
Set Parent = modParentChildDereference.ObjFromPtrVBA7(mlParentPtr)
#Else
Set Parent = modParentChildDereference.ObjFromPtr(mlParentPtr)
#End If
End Property
Public Property Set Parent(obj As ClassProperties)
mlParentPtr = ObjPtr(obj)
End Property在家长班:
Set newItem.Parent = Me在CopyMemory obj,pObj,4我可以看到LongPtr,比如1234567789^,但是CopyMemory无法在内核中找到LongPtr。
我在CopyMemory 这里和这里上读到了一些深层次的线程。
基于这些,我和CopyMemory obj、pObj、4玩了一会儿,给出了不同的数字,比如8和16,但都没有用。
有什么方向或解决办法吗?提亚
发布于 2020-04-01 03:29:27
上面注释中的@UnhandledException提供的链接将我指向一个令人惊叹的线
我实现了它,适应了64位( 看我的答案 ),并且得到了完全不同的错误,甚至崩溃了VBE、视频驱动程序和窗口。
但OP的一条评论让我睁大了眼睛:
请不要硬编码大小.永远不会。LenB是VBA的VBA,大小是C的VBA。使用它来调整拷贝内存API的分配。-这个
重返工作岗位:
Public Function ObjFromPtr(ByVal pObj As Long) As Object
Dim obj As Object
CopyMemory obj, pObj, LenB(obj) 'not size 4!
Set ObjFromPtr = obj
CopyMemory obj, 0&, LenB(obj) 'not size 4!
End Function
Public Function ObjFromPtrVBA7(ByVal pObj As LongPtr) As Object
Dim obj As Object
CopyMemory obj, pObj, LenB(obj) 'not size 4! <== here the error no more!!!!
Set ObjFromPtrVBA7 = obj
CopyMemory obj, 0&, LenB(obj) 'not size 4!
End Function所以,回答。
非常感谢社区!
发布于 2022-05-12 11:08:57
即使在64位Windows和64位Office (VBA)上,内核dll仍然是"kernel32"!这就是“运行时错误'53‘文件找不到的地方:内核。”显然是这么说的。
事实上,所有64位系统库都位于%SystemRoot%\System32下,而32位子系统的库都位于%SystemRoot%\SysWOW64中。(是的,这是对的,即使命名暗示相反)
所以
#If VBA7 Then
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(dest As Any, Source As Any, ByVal Length As LongPtr)
#End If只要Office32和Office64系统已经使用了VBA7引擎,它们就能同时工作。
但是Marcelo也是正确的,应该使用LenB(pObj) (pObj或任何LongPtr变量)作为length参数。但是,正如Marcelo所建议的,LenB( obj )不适用于我(Office2013),因为obj不是什么,LenB试图删除obj实例。
https://stackoverflow.com/questions/60943510
复制相似问题