Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling COM events in C++ (ATL, MFC or pure C++) for Java interop
    text
    copied!<p>I'm currently trying to build a C++ library (DLL file) that interfaces with a COM component, to make it usable in Java. The idea was that I'd build a very small C++ DLL with a class that "wraps" around the COM component, and then export it using SWIG. I got pretty far by using an #import statement:</p> <pre><code>#import "ComponentName.dll" </code></pre> <p>And calling CoInitialize() and creating an instance of the component (via the IComponentNamePtr class that was generated by Visual Studio). This worked for all the normal COM method calls, which was good.</p> <p>However, I can't work out how to get events working. I see there's a IComponentNameEventsPtr that complements the main "smart pointer" class, but I couldn't work out what to do to get it working</p> <p>I've tried all the following to get events working:</p> <ul> <li><strong>Pure C++</strong> &mdash; I couldn't work out how to do this. I tried creating a new class that inherited from the IComponentNameEvents class, making stubs for all the abstract functions, and overriding the functions, except the functions are not marked as virtual, so that didn't work.</li> <li><strong>MFC</strong> &mdash; I couldn't get the call to <em>AfxOleInit</em> working correctly. Googling told me that the call fails when called from a DLL as it assumes OLE is already initialised. I wasn't too sure how to fix this. My library crashed every time I tried to create an instance of the COM component (I assume because COM wasn't initialised properly)</li> <li><strong>ATL</strong> &mdash; I couldn't work out how to do the events in ATL. I could create the class (via the "Simple ATL" wizard, then the "Implement Interface" wizard) but couldn't work out how to use it. I read <a href="http://msdn.microsoft.com/en-us/library/06wtz4t5.aspx" rel="noreferrer">Using IDispEventImpl on MSDN</a> but couldn't work out how to use the class produced by that howto. Do I need to use the COM object via ATL too (or can I use the #import autogenerated classes)? How do I "attach" the event listener class?</li> <li>I read <a href="http://msdn.microsoft.com/en-us/library/hdcxwbd5.aspx" rel="noreferrer">Event Handling in COM</a> which uses the event_receiver attribute (part of the new Visual C++ unified event model). Originally I couldn't work out how to combine that with usage of the COM component created via the #import statement. I finally worked out (and it was mentioned on the page!) that I need to use the "embedded_idl" flag on the #import statement, but that broke other things (I got a bunch of "expecting a type specification near" errors in the .tlh file)</li> </ul> <p>Does anyone know how to do this? What's the easiest approach to take? My background is in C# and PHP so I don't have much experience with using COM in C++.</p> <p>tldr: What's the easiest way to use COM events in a C++ DLL?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload