Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to debug Reference Count of VB ActiveX Control?

10 views
Skip to first unread message

Leo

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to

Visual Basic 6.0
As title.
Thanks for any suggestion.

Phil Jollans

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
This would interest me too, but I don't believe that there is any way to do
it!

Alexander Milenz

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
This Code will get you the Reference Count. You may even call IUnknown's
AddRef & Release methods, though I would advise against doing this until you
know PRECISELY what you are doing. Simply put this into any module and let
your Control update it's Title with the value.

----------------------------------------------------------------------------
----

Private Declare Sub ICopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)

Public Function RefCount(ByVal unk As IUnknown) As Long
Dim RefCountAddr As Long
Dim RefCounter As Long

RefCountAddr = ObjPtr(unk) + 4
If RefCountAddr = 4 Then
RefCount = 0
Exit Function
End If
ICopyMemory RefCounter, ByVal RefCountAddr, 4
RefCount = RefCounter - 2
End Function


Public Sub AddRef(ByVal unk As IUnknown)
Dim RefCountAddr As Long
Dim RefCounter As Long

RefCountAddr = ObjPtr(unk) + 4
If RefCountAddr = 4 Then
RefCount = 0
Exit Function
End If
ICopyMemory RefCounter, ByVal RefCountAddr, 4
RefCounter = RefCounter + 1
ICopyMemory ByVal RefCountAddr, RefCounter, 4
End Sub


Public Sub Release(ByVal unk As IUnknown)
Dim RefCountAddr As Long
Dim RefCounter As Long

RefCountAddr = ObjPtr(unk) + 4
If RefCountAddr = 4 Then
RefCount = 0
Exit Function
End If
ICopyMemory RefCounter, ByVal RefCountAddr, 4
RefCounter = RefCounter - 1
ICopyMemory ByVal RefCountAddr, RefCounter, 4
End Sub
----------------------------------------------------------------------------
----------------------------

I know this works for VB Classes, and it *should* work for controls as well,
though I never tested it.
If you are intersted in doing some nasty things with VB then go and read:
>> Dan Appleman's developing COM Components with Visual Basic 6.0: A Guide
to the perplexed<<

Hope this helps
Alexander Milenz


"Phil Jollans" <Phil.J...@messring.de> schrieb im Newsbeitrag
news:90gb8m$pd2$1...@ninja.muc.baycix.de...

Leo

unread,
Dec 4, 2000, 10:05:13 PM12/4/00
to
Thank you very much, Alexander.
I want to trace the reference count when AddRef or Release method of control
was called.
That is mean I want to "hook" the two method with my implement.
That is mean I want to "overload" the two method with my implement.
Only in this way, I can output debug string in my implement.
The effect is:
Class A:
Public Sub AddRef()
Debug.Print "Reference Count is " & GetRefCount()
call OldAddRef()
End Sub
How can I "overload"?
Thanks for any suggestion.


"Alexander Milenz" <Ras...@bigfoot.com> wrote in message
news:eN1aQzj...@cppssbbsa02.microsoft.com...

Alexander Milenz

unread,
Dec 8, 2000, 3:00:00 AM12/8/00
to
Honestly, I don't know exactly how overloading is done in VB.
Implement does NOT do the job. You have to copy some function tables in the
memory.
(and there comes you personal GPF)

> If you are intersted in doing some nasty things with VB then go and read:
> >> Dan Appleman's developing COM Components with Visual Basic 6.0: A Guide
> to the perplexed<<

I know that Appleman explains the technique quite well in his book, so all I
can suggest
is: go and read that book - it's worth it.

Alex


"Leo" <nk...@263.net> schrieb im Newsbeitrag
news:#1WscimXAHA.307@cppssbbsa05...

0 new messages