Link to home
Start Free TrialLog in
Avatar of Adept
Adept

asked on

Events from multiple COM objects into same form


I have a com object that has a callback interface.  I would like to have multiple com objects instanced in a form with all of them having their callback interfaces sinked on that form.  

The way I have tried to do it so far is to have a tmp variable defined public with events on the form and I new multiple objects onto that varaible (being sure to store the objects onto a collection).  The problem I am having is that as soon as I new a new object, the old object releases.  

How do I keep multiple objects firing events into the same form?  Im guessing I need to up the ref count on the object so it doesn't go away?  How do I do that?

Avatar of AzraSound
AzraSound
Flag of United States of America image

It sounds like maybe you are doing something like:

Public WithEvents t As COMObject


Private Sub Command_Click()
    Dim x As New COMObject

    Set t = x
End Sub


Each time you do that, you reset t to a new COMObject, regardless of whether you are adding to a collection or not.  The t object only points to one place in memory, and it cannot reference multiple places.  You need to create an additional "collection" type class that handles your events.  You will need some value to distinguish each COM object so that you know which one is firing the event.

BTW, sample at the following link demonstrating this idea (called objArrays.zip)

http://www.mvps.org/vb/index2.html?samples.htm
Avatar of Adept
Adept

ASKER


either I don't fully understand your answer or I didn't phrase the question very well.  I'm not having a hard time sinking the event, that is working fine.  I'm having a hard time sinking multiple events to one form from muiltple instances of a COM object.  Currently this is what I'm doing (that isn't working)


Dim WithEvents casTmp As CasFileTransfer

Public Sub formTransfers_InitiateTransfer(tmpResult As CasSearchResult)

    Dim strKey As String
    strKey = tmpResult.bstrFileName
   
   
    mapTransfers.Add casTmp, strKey
    listDownloads.ListItems.Add 1, strKey, strKey

    Set casTmp = New CasFileTransfer
    casTmp.AddSource tmpResult.bstrIP, tmpResult.nPort, tmpResult.nFileSize, tmpResult.nHashCode, tmpResult.bstrFileName

End Sub





>>Set casTmp = New CasFileTransfer

This "resets" the variable each time it is called.  Any sources you have added via the AddSource method have been lost.

It looks like you have your collection, mapTransfers, so that is somewhat ok, but you still cannot use a plain collection.  You must define your OWN collection type class, that keeps track of each COM object instantiated, and it is this custom collection class that will raise events, not an individual CasFileTransfer object.
Avatar of Adept

ASKER


k, I'm modestly new to VB but I'm experienced at c++ (so please bear with me).  :)

how could I make a collection class that could sink these events?  is that what you mean that I create a seperate collection class that accepts the events and then pass them on (if so how would I do this)?  any code examples would help too.

ty
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
Hi Adept,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept AzraSound's comment(s) as an answer.

Adept, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Avatar of Adept

ASKER

I did forget about this question.  I had set my email filter to look for answers proposed and missed the comment/answer.

thanks
No blame attached :)  That's why i say "it appears..."
-- Dan