-1

This is the problem I am trying to solve.

My C++ console application is interacting with a C++ COM DLL.

Upon certain conditions, the C++ COM DLL needs to send some data back to the console application. I was thinking to pass a callback (std::function) as a parameter of one of the DLL API, but I do not know if COM supports that.

How could I implement such callbacks?

Thank you for your help!

Romain
  • 103
  • 2
  • 11
  • You can do it but you need to provide some plumbing. Can you show code that represents the COM interface? – Richard Hodges Aug 29 '18 at 07:16
  • A COM call back is just an COM interface that you pass to the dll. – Richard Critten Aug 29 '18 at 07:36
  • @RichardHodges: yeah let me see if I can come up with some sample code to share (the existing code is a big machinery....). – Romain Aug 29 '18 at 09:07
  • @RichardCritten: I am not that familiar with COM. So essentially, I would need to : 1) Pass the interface as a parameter to the COM API. 2) In the DLL, call an API from that interface to callback into the application. Is that correct? – Romain Aug 29 '18 at 09:10
  • If it is C# rather than C++, there seems to be an article. [How to handle COM events from a console application?](https://stackoverflow.com/questions/6185610/how-to-handle-com-events-from-a-console-application) – kunif Aug 29 '18 at 09:55
  • @kunif: my applications are in C++. – Romain Aug 30 '18 at 02:43

1 Answers1

1

There is a sample program that is likely to be helpful for your problem.
This is an example of specialized interface called IPortableDevice/IPortableDeviceEventCallback.
Project root Portable Devices COM API Sample
CallBack registration/release part DeviceEvents.cpp

As a general purpose principle, the COM-DLL you use must implement the IConnectionPoint interface.
The application implements the IConnectionPointContainer interface and registers CallBack(=Event Handler) by calling IConnectionPoint::Advice with that object as a parameter.

Please search variously with IConnectionPoint/IConnectionPointContainer.

There is no other code example, but there is also this article.
What is IconnectionPoint and EventHandling

kunif
  • 4,060
  • 2
  • 10
  • 30
  • Thank you. Yes I have been reading about the connection points and it seems to be the right approach for my problem. However, I have another issue now. I do not have control over the lifecycle of the object where I am supposed to "fire" the callback to the C++ application. That COM object is created by a VBscript execution - and it will be destroyed upon script completion. My C++ application will never be aware on when that script executes. I am now investigating if there is a way to have a singleton for that COM object - so both the VBscript and the C++ application share the same reference. – Romain Sep 03 '18 at 02:49
  • Although it is unknown whether it relates to your new problem, in general, the COM object should remain even if one of several clients ends, as long as the AddRef and Release call counts are handled correctly. If it is not a simple problem, please issue a new question. – kunif Sep 03 '18 at 03:28