Results 1 to 7 of 7

Thread: Add event handler to Object

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Add event handler to Object

    How can I add an event handler to an object that I know the structure of, but visual studio does not .... it is a System.__ComObject

    You can use AddHandler for known object structures ... but if I want to add one purely on an object how do I do this?

    Thanks,
    Kris

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Add event handler to Object

    Any chance that this object can be cast to an interface that defines the event? Writing a managed version of the interface and then casting against it is the only way I know of accomplishing this.

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Add event handler to Object

    Quote Originally Posted by TnTinMN View Post
    Any chance that this object can be cast to an interface that defines the event? Writing a managed version of the interface and then casting against it is the only way I know of accomplishing this.
    Nope doesn't work ... set up the interface with the same event in it and when trying to cast to the interface get:

    Code:
    InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'iTest'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{BAA05248-AA87-4C45-A0CC-DD3677AF2790}' failed
    due to the following error: No such interface supported (Exception
    from HRESULT: 0x80004002 (E_NOINTERFACE)).
    > 0026 - i00CodeLib.Drawing+_Closure$__1+_Closure$__2+_Closure$__3._Lambda$__1 (frmPerson)
    Kris

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Add event handler to Object

    Quote Originally Posted by i00 View Post
    Nope doesn't work ... set up the interface with the same event in it and when trying to cast to the interface get:

    Code:
    InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'iTest'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{BAA05248-AA87-4C45-A0CC-DD3677AF2790}' failed
    due to the following error: No such interface supported (Exception
    from HRESULT: 0x80004002 (E_NOINTERFACE)).
    > 0026 - i00CodeLib.Drawing+_Closure$__1+_Closure$__2+_Closure$__3._Lambda$__1 (frmPerson)
    Kris
    Did you set the ComImport and Guid attributes on your managed interface? Guid should be the same Guid as the unmanaged Interface. Look at the example include in the ComImport link.

    I have assumed that you have already tried to add a COM reference (Project Menu->Add Reference->COM Tab) to the library that defines the object and it could not import it. If not please try that first and let VS generate the code for you.

  5. #5

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Add event handler to Object

    I have don that:

    vb Code:
    1. Dim ComObject = Trycast(Control.UnderlyingComObject, Test)
    2. '----------
    3.      <ComImport(), _
    4.         Guid("BAA05248-AA87-4C45-A0CC-DD3677AF2790")>
    5.         Public Interface Test
    6.             Event Sort(ByVal DirectionTopDown As Boolean)
    7.         End Interface

    ... same error though ...
    Kris

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Add event handler to Object

    Is this COM class written in VB6? If so, based on my understanding of the below quote, that may be the issue. Note that I am not a VB6 programmer, so I may be wrong.
    The Implements feature does not support outgoing interfaces. Thus, any events you declare in the class module will be ignored.
    from this: Creating Standard Interfaces with Visual Basic

  7. #7

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Add event handler to Object

    *shameless bump

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width