Home |
Search |
Today's Posts |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Bart,
In essence the code is about assigning an object reference from a long object pointer (aka re-hydrating). An example of it in use was posted by Rob Bruce for handling a circular reference scenario, bottom of this page - http://www.dailydoseofexcel.com/arch...#comment-29661 The originator is attributed as Bruce McKinney both by Rob and in the comments in the VB6 example you linked to. However it was published in Matthew Curland's book "Advanced Visual Basic 6" p103. Not sure if copyright allows me to post MC's comments though Rob includes his own. As it happens, I had a discussion about this with Mike Rosenblum. He thought it worthwhile to explain the big picture about this fascinating approach, and he has done so superbly well - Function ObjFromPtr(ByVal Ptr As Long) As Object Dim tmp As Object ''' * comments by Mike Rosenblum * ' Directly Copy the pointer value into a ' temporary object variable. The result ' creates a strong reference to the object, ' but *without* incrementing the reference ' count. CopyMemory tmp, Ptr, 4 ' However, we *do* need to increment the ' reference count or else the object could ' *still* go out of scope on us at some ' point and cause a crash later! ' Therefore, we now use a normal Set ' statement to place a strong reference ' in the function name. This increments the ' reference count by one: Set ObjFromPtr = tmp ' At this point we have created TWO strong ' references, but have only incremented the ' reference count for the object by ONE. ' We need to rectify this by clearing the ' value held in the 'tmp' variable. ' By setting the long value held in the 'tmp' ' variable to zero, we make the variable null ' (aka "Nothing"). Otherwise, if a non-null ' tmp' variable exits this function, VB would ' automatically decrement the reference count ' by one, negating our attempt to increase the ' reference count to match the 1 strong ' reference that this function is creating. CopyMemory tmp, 0&, 4 End Function Perhaps I should also clarify, the function was assumed to be dealing with Class objects rather than controls, hence the bit about could "still go out of scope". "Any good use for it" - apart from dealing with circular reference issues, as in the book and the link above, I'm not sure. Also not sure what the purpose of combining with the timer is for in the VB6 example you linked to. Great stuff though! Regards, Peter T "RB Smissaert" wrote in message ... 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 |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Is there a way to use Formula to resolve | Excel Worksheet Functions | |||
#REF! Error Resolve? | Excel Worksheet Functions | |||
Did you ever resolve this? | Excel Discussion (Misc queries) | |||
how to resolve a printer name | Excel Programming | |||
Another issue to resolve | Excel Programming |