View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default Resolve object pointer any use for this?

Stumbled upon this interesting code and wonder if anybody knows any good use
for this.
This example needs a userform with a treeview on it, but you can do the same
with any other control.
The interesting bit is that it allows you to store controls and forms as
simple Long variables in arrays
or collections. The example I saw used the SetTimer API, but it looks this
is not needed:
http://www.mvps.org/vbvision/_sample...nters_Demo.zip


Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (lpDest As Any, _
lpSource As Any, _
ByVal lBytes As Long)

Sub test()

Dim lObjPtr As Long

With UserForm1.TreeView1.Nodes
.Clear
.Add , , "key1", "top node"
.Add "key1", tvwChild, "key2", "child node"
End With

lObjPtr = ObjPtr(UserForm1.TreeView1.Nodes(2))

'to demonstrate that we have a fully qualified object reference here
'-------------------------------------------------------------------
MsgBox ObjectFromObjectPointer(lObjPtr).Parent.Child.Text

End Sub


Private Function ObjectFromObjectPointer(ByVal lObjectPointer As Long) As
Object

Dim lpObject As Object

'use the CopyMemory API to copy the
'long pointer into the object variable
'-------------------------------------
CopyMemory lpObject, lObjectPointer, 4&
Set ObjectFromObjectPointer = lpObject
CopyMemory lpObject, 0&, 4&

End Function


RBS