-
Aug 31st, 2014, 07:20 PM
#1
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
-
Aug 31st, 2014, 08:02 PM
#2
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.
-
Aug 31st, 2014, 09:00 PM
#3
Re: Add event handler to Object
Originally Posted by TnTinMN
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
-
Sep 1st, 2014, 09:42 AM
#4
Re: Add event handler to Object
Originally Posted by i00
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.
-
Sep 1st, 2014, 09:13 PM
#5
Re: Add event handler to Object
I have don that:
vb Code:
Dim ComObject = Trycast(Control.UnderlyingComObject, Test) '---------- <ComImport(), _ Guid("BAA05248-AA87-4C45-A0CC-DD3677AF2790")> Public Interface Test Event Sort(ByVal DirectionTopDown As Boolean) End Interface
... same error though ...
Kris
-
Sep 3rd, 2014, 12:43 AM
#6
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
-
Sep 7th, 2014, 09:52 PM
#7
Re: Add event handler to Object
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|