Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 24,523 times

Contents

Related Categories

Circular Referencing to COM Objects - How to Soft Reference an Object

webjose

How to Soft Reference an Object

Having explained a bunch of things you probably knew already, lets show the solution.

Option Explicit

'API declaration of CopyMemory
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        pDest As Any, pSrc As Any, ByVal ByteLen As Long)

'Private variable to hold the pointer to the parent
Private mvarParent as Long

'Now you add the property code
Public Property Get Parent() as SuperListBox

dim oTemp as SuperListBox

    If (mvarParent = 0) Then
        Set Parent = Nothing
    Else
        'Copy the pointer stored in mvarParent to the temporal object variable
        CopyMemory oTemp, mvarParent, 4
        'Now set the parent property from the temporal object
        Set Parent = oTemp
        'Now clear the temporal object
        'WARNING: Failure to do this will make your program crash.
        'Why? VB will try to reduce the objects reference count to a negative number
        'because the use of CopyMemory did not increment the COM reference count.
        CopyMemory oTemp, 0&, 4
    End If
End Property

'You make the Set part of the property a Friend procedure,
'just to make sure the end programmer will not mess with it
Friend Property Set Parent(ByVal oNewParent as SuperListBox)
    mvarParent = ObjPtr(oNewParent)
End Property

The method shown here is called soft referencing. Basically, what we are doing here is tricking VB and COM by not using the provided methods for referencing an object. Instead, we get the pointer value for the object with the use of the ObjPtr() function and store it in a Long variable. Later, when we need the reference back, we use CopyMemory to make a VB-friendly version of the pointer we had stored, and then set the Parent property to equal this VB-friendly version. Of course, there is a price we pay: You will get a GPF (General Protection Failure) if you do not clear the variable yourself, so make sure you always clear the variable.

The example above uses CopyMemory again to clear the temporal variable. And just in case you are wondering, I wrote 0& in the CopyMemory call to force this zero to be a long value (4 bytes). If yo do not do it, you may get a GPF because VB may only allocate 2 bytes (Integer value) for this zero.

And finally, another way to clear the temporal object variable could be this:

Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" ( _
        Destination As Any, ByVal Length As Long)

    ZeroMemory oTemp, 4

I hope you have found this useful. If you would like to say something about this article, then please post a comment.

This code is for everyone's use and, although I have tested it the best I can, I cannot be held responsible for its use or misuse, or for any other type of damage WHATSOEVER. Use this code at your own risk. If you find an error or can improve it, feel

Comments

  • Great Tutorial!!(Previous &this)

    Posted by ZanalKhan on 21 Feb 2003

    Great tutorial.This will definitely change the way i look up on these Matters..

  • Yes, can be done

    Posted by webjose on 09 Feb 2003

    There is no secret as to how you decrease the reference count of a COM object. All you have to do is call the Release method of the IUnknown interface. But VB will hide this. However, you can cheat...

  • How About Reducing the Reference Count?

    Posted by GrahamEpps on 20 Jan 2003

    I have a project with Splitter controls that require references to pairs of controls that are proportionally resized by the user.
    The application GPF's (occasionally) upon termination. If run inside...

  • No problem

    Posted by webjose on 20 Dec 2002

    Glad it helped.:cool:

  • excellent

    Posted by igitur on 10 Dec 2002

    I struggled for a long time with my app hanging after it quits. It turned out to be a circular reference in the (approximately) 180 000 objects.

    Thnx for the great explanation and work-around. Ev...