LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Resolve object pointer any use for this?

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to use Formula to resolve Sky Excel Worksheet Functions 5 May 7th 09 01:07 AM
#REF! Error Resolve? Dan the Man[_2_] Excel Worksheet Functions 2 July 30th 07 03:28 AM
Did you ever resolve this? [email protected] Excel Discussion (Misc queries) 1 April 24th 07 02:34 PM
how to resolve a printer name Bert van den Brink Excel Programming 1 July 30th 06 10:39 AM
Another issue to resolve Pat Excel Programming 11 February 20th 05 09:01 PM


All times are GMT +1. The time now is 10:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"