Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing an ActiveX in C#, to be used in a legacy C++ application.
It all works ok, apart from event handling.
The problem is that when the event is raised in C# I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in AwDotNetPinchOcx.dll
	Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'AwDotNetPinchOcx.__AwPinchGrid'. 
	This operation failed because the QueryInterface call on the COM component for the interface with IID '{76F027EC-3D40-449E-B7E6-EFDF7CC63963}' 
	failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

However, the C# ActiveX works fine in a test VB6 app.
And, a native VB6 ActiveX works fine in the C++ application!

My work-around at the moment is therefore a VB6 ActiveX wrapper that contains the C# ActiveX,
and passes data and events across.

I want to get rid of this wrapper though.
Can anyone help?

=====================================
The C++ app uses the ConnectionPoint event sink method:
=====================================
DWORD EventSinkHolder::AddEventSink(REFIID iid, IUnknown* pYourEventSink)
   {
   DWORD sinkCookie = 0;
   IUnknown* pEventSource = EventSourceInterface();
   HASSERT(pEventSource);
   LPCONNECTIONPOINTCONTAINER pConnectionPointContainer = NULL;
   if (SUCCEEDED(pEventSource->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnectionPointContainer)))
      {
      HASSERT(pConnectionPointContainer);
      LPCONNECTIONPOINT pConnectionPoint = NULL;
      if (SUCCEEDED(pConnectionPointContainer->FindConnectionPoint(iid, &pConnectionPoint)))
         {
         HASSERT(pConnectionPoint);
         // connect passed event sink interface to event source
         pConnectionPoint->Advise(pYourEventSink, &sinkCookie);
         pConnectionPoint->Release();
         // record connection for benefit of RemoveAllEventSinks
         mySinkEntries.Append(new EventSinkEntry(iid, sinkCookie));
         }
      pConnectionPointContainer->Release();
      }
   return sinkCookie;
   }

=====================================
=====================================
The C# ActiveX follows the standard method used in the Codeplex sample "CSActiveX"
http://1code.codeplex.com/SourceControl/changeset/view/45838#394692
Posted

1 solution

Because of the different calling conventions you are dependant of the wrapper. If you search with google you soon will find proof of this.
 
Share this answer
 
Comments
Ant Waters 27-Jul-10 7:46am    
Can you point to somewhere in Google that shows that. I have looked a lot myself but am no expert.
E.F. Nijboer 27-Jul-10 13:56pm    
Here are some links:
http://cratonica.wordpress.com/2010/02/19/implementing-c-net-events-in-c/

By the way, maybe the statement I made about not being able to lose the vb wrapper could be incorrect... ;-)

http://msdn.microsoft.com/en-us/library/hdcxwbd5%28vs.71%29.aspx
http://www.eggheadcafe.com/software/aspnet/35188455/howto-exposing-c-events-to-activex.aspx
http://bytes.com/topic/c-sharp/answers/262833-mfc-activex-control-c-windows-service

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900