I have an ATL project which exposes a COM object. I would like my COM object to call a function specified by the consumer from time to time, or via some other means of notifying the consumer from time to time.
I have tried adding a method in CerberusNative.idl
in order to take void function pointers which I would like to call from the COM instance side of things so that the consumer would just need to tell the COM object upon initialization what its callbacks are:
interface ICerberusSession : IDispatch {
[id(5)] HRESULT SetCallbacks([in] void(*userExit)(int, char *), [in] void(*userAttemptingReconnection)(), [in] void(*userReconnected)());
};
However, there are some problems with my void function pointers:
Severity Code Description Project File Line Suppression State Error MIDL2269 procedures in an object interface must return an HRESULT : [( Parameter 'userExit' ) ] Error MIDL2131 parameter must not be a function : [ Parameter 'userExit' of Procedure 'SetCallbacks' ( Interface 'ICerberusSession' ) ]
In what ways can I get a client application to subscribe to callback functions in my COM object?
The correct way to do it is to define a separate interface with methods on it, and to pass that interface into my setter method:
[
object,
uuid(AECE8D0C-F902-4311-A374-ED3A0EBB6B49),
dual,
nonextensible,
pointer_default(unique)
]
interface ICallbacks : IUnknown
{
[id(1)] HRESULT UserExit([in] int errorCode, [in] BSTR errorMessage);
[id(2)] HRESULT UserAttemptingReconnection();
[id(3)] HRESULT UserReconnected();
};
[
object,
uuid(B98A7D3F-651A-49BE-9744-2B1D8C896E9E),
dual,
nonextensible,
pointer_default(unique)
]
interface ICerberusSession : IDispatch {
...
[id(5)] HRESULT SetCallbacks([in] ICallbacks* callbacks);
};